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; }
相关文章推荐
- POJ 3278 Catch That Cow
- POJ 3278 Catch That Cow(追赶母牛)
- poj3278 Catch That Cow(BFS)
- POJ 3278 Catch That Cow
- POJ 3278 catch that cow
- POJ 3278----Catch that cow(广搜) 注意一维及方向数组
- hdu 2717 (poj 3278)Catch That Cow
- poj 3278 Catch That Cow
- POJ 3278 Catch That Cow
- POJ 3278 Catch That Cow(基础题)
- POJ 3278 Catch That Cow(模板——BFS)
- POJ - 3278 Catch That Cow
- C - Catch That Cow POJ - 3278
- poj 3278 Catch That Cow
- poj-3278-Catch That Cow
- POJ 3278 Catch That Cow
- poj 3278 Catch That Cow bfs
- poj 3278 Catch That Cow
- POJ 3278 Catch That Cow(BFS 剪枝)
- POJ 3278 Catch That Cow