POJ3278 Catch That Cow

来源:互联网 发布:淘宝客微信怎么加人 编辑:程序博客网 时间:2024/06/05 19:10

本来以为是一道很简单的题 然后后来RE了10次
看了看讨论区才知道是数组越界问题
就是一定要先判断数组有没有越界
附AC BFS写法

#include<iostream>#include<queue>using namespace std;#define MAX 1000001#define INF 1000000void bfs(int n, int k);int d[MAX];bool sta[MAX];int main(){    int n, k;    cin >> n >> k;    {        memset(d, 0, sizeof(d));        memset(sta, false, sizeof(sta));        bfs(n, k);        cout << d[k] << endl;    }}void bfs(int n, int k){    queue<int>que;    que.push(n);    while (que.size())    {        int T = que.front();        int a, b, c;        a = T-1;        b =T+1;        if(n<k)            c = T * 2;        if (n >= k)        {            d[k] = n - k;            return;        }        que.pop();        if (a >= 0&&a<=MAX-5 && sta[a] == false)        {            que.push(a);            d[a] = d[T] + 1;            sta[a] = true;        }        if (b <= MAX - 5&&sta[b]==false)        {            que.push(b);            d[b] = d[T] + 1;            sta[b] = true;        }        if (c <= MAX - 5&&sta[c]==false)        {            que.push(c);            d[c] = d[T] + 1;            sta[c] = true;        }        if (sta[k]==true)            return;     }}
原创粉丝点击