POJ3278 Catch That Cow

来源:互联网 发布:淘宝网雪地靴女靴 编辑:程序博客网 时间:2024/05/05 03:14
/*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[n] = true;step[n] = 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;}

0 0
原创粉丝点击