您的位置:首页 > 其它

POJ 3278 Catch that cow 广度优先搜索bfs

2018-03-05 23:05 621 查看
Catch That CowDescriptionFarmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤ 100,000) on the same number line. Farmer John has two modes of transportation: walking and teleporting.* Walking: FJ can move from any point X to the points X - 1 or X + 1 in a single minute* Teleporting: FJ can move from any point X to the point 2 × X in a single minute.If the cow, unaware of its pursuit, does not move at all, how long does it take for Farmer John to retrieve it?InputLine 1: Two space-separated integers: N and KOutputLine 1: The least amount of time, in minutes, it takes for Farmer John to catch the fugitive cow.Sample Input5 17Sample Output4HintThe fastest way for Farmer John to reach the fugitive cow is to move along the following path: 5-10-9-18-17, which takes 4 minutes.  广度优先搜索实现方法(非递归): 1.设置一个队列Q,从顶点出发,遍历该顶点后让其进队; 2.出队一个顶点元素,求该顶点的所有邻接点(对应于此题即三种走法),    对于没有遍历过的邻接点遍历之,并 让其进队; 3.若队空停止,队不空时继续第2步。C++Stl<queue>版本:
//Memory: 1020K		Time : 141MS#include<iostream>#include<queue>using namespace std;struct node {int num;int step;node(int n = 0, int s = 0) {num = n;step = s;}};const int maxn = 100010;bool vis[maxn * 2];queue<node>que;int bfs(int n, int k){if (n >= k) return n - k;    //特例memset(vis, false, sizeof(vis));que.push(node(n, 0));vis= true;  //访问标记while (!que.empty()){node temp = que.front();que.pop();int next;for (int i = 0; i < 3; i++){if (i == 0) next = temp.num + 1;if (i == 1) next = temp.num - 1;if (i == 2) next = temp.num * 2;if (next<0 || next>maxn) continue;if (!vis[next]){if (next == k) return temp.step + 1;vis[next] = true;que.push(node(next, temp.step + 1));}}}}int main(){int n, k;cin >> n >> k;cout << bfs(n, k) << endl;return 0;}
另一种剪枝:while (!que.empty()){node temp = que.front();int next;que.pop();if (temp.num - 1 >= 0 && !vis[temp.num - 1]){next = temp.num - 1;vis[next] = true;if (next == k) return temp.step + 1;que.push(node(next, temp.step + 1));}if (temp.num <= k){if (!vis[temp.num + 1]){next = temp.num + 1;vis[next] = true;if (next == k) return temp.step + 1;que.push(node(next, temp.step + 1));}if (!vis[temp.num * 2]){next = temp.num * 2;vis[next] = true;if (next == k) return temp.step + 1;que.push(node(next, temp.step + 1));}}}模拟队列版本://Memory: 1244K Time : 0MS#include<iostream>using namespace std;struct node {int num;int step;node(int n = 0, int s = 0){num = n; step = s;}};const int maxn = 100010;bool vis[maxn * 2];node queue[maxn];int bfs(int n, int k){if (n >= k) return n - k; //特例memset(vis, false, sizeof(vis));memset(queue, 0, sizeof(queue));int head, tail;head = tail = 0;queue[tail++] = node(n, 0);vis= true; //访问标记while (head != tail){node temp = queue[head++];int next;for (int i = 0; i < 3; i++){if (i == 0) next = temp.num + 1;if (i == 1) next = temp.num - 1;if (i == 2) next = temp.num * 2;if (next<0 || next>maxn) continue;if (!vis[next]){if (next == k) return temp.step + 1;vis[next] = true;queue[tail++] = node(next, temp.step + 1);}}}}int main(){int n, k;while (cin >> n >> k){cout << bfs(n, k) << endl;}return 0;}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  算法 bfs