您的位置:首页 > 其它

POJ-3278(Catch That Cow)

2018-03-04 21:02 302 查看
POJ-3278

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>

using namespace std;

int visit[1000010];
struct _Node{
int pos, step;

_Node(){
}
_Node(int _pos, int _step)
{
pos = _pos;
step = _step;
}
};
int bfs(int start, int end)
{
queue<_Node> q;
q.push(_Node(start, 0));
visit[start] = 1;

while(!q.empty())
{
_Node x = q.front(); q.pop();

if(x.pos == end)
{
return x.step;
}

int curpos = x.pos;

curpos = x.pos - 1;
if(!visit[curpos] && x.pos >= 1)
{
visit[curpos] = 1;
q.push(_Node(curpos, x.step + 1));
}

curpos = x.pos + 1;
if(!visit[curpos] && x.pos < end)
{
visit[curpos] = 1;
q.push(_Node(curpos, x.step + 1));
}

curpos = x.pos * 2;
if(!visit[curpos] && x.pos < end )
{
visit[curpos] = 1;
q.push(_Node(curpos, x.step + 1));
}

}
return -1;
}
int main()
{
int n, m;
while(cin >> n >> m)
{
memset(visit, 0, sizeof(visit));
int t = bfs(n, m);
printf("%d\n", t);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: