POJ3278 Catch That Cow
2014-07-17 21:04
441 查看
/*algorithm : BFS*/ #include <iostream> #include <cstring> #include <queue> #include <algorithm> #include <fstream> using namespace std; const int maxn = 100005; int n, k; int vis[maxn], step[maxn]; void BFS() { queue<int> q; int head, next; vis = true; step = 0; q.push(n); while (!q.empty()) { head = q.front(); q.pop(); for (int i = 0; i < 3; ++i) { if (i == 0) next = head - 1; if (i == 1) next = head + 1; if (i == 2) next = head * 2; if (next < 0 || next >= maxn) continue; if (!vis[next]) { vis[next] = true; step[next] = step[head] + 1; q.push(next); } if (next == k) { cout << step[next] << endl; return; } } } } int main() { while (cin >> n >> k) { memset(vis, 0, sizeof(vis)); memset(step, 0, sizeof(step)); BFS(); } return 0; }
相关文章推荐
- poj 3278 Catch That Cow
- POJ3278-Catch That Cow
- 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
- POJ 3278 Catch That Cow
- POJ 3278 Catch That Cow (BFS)
- POJ 3278 Catch That Cow(BFS)
- POJ 3278 Catch That Cow(模板——BFS)
- POJ 3278-Catch That Cow(bfs)
- 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 (BFS入门题
- POJ - 3278 Catch That Cow (BFS)
- Poj_3278 Catch That Cow(BFS)
- POJ 3278 Catch That Cow