POJ 3278Catch That Cow(简单广搜)

题目链接:https://cn.vjudge.net/problem/POJ-3278

 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 #include <queue>
 5 #include <algorithm>
 6 #define mem(a,b) memset(a,b,sizeof(a));
 7 using namespace std;
 8 #define INF 0x3f3f3f3f
 9 typedef long long ll;
10 int dir[4][2] = {0,1,0,-1,1,0,-1,0};
11 const int maxn = 100005;
12 int n,k,vis[maxn],ans;
13 struct node {
14     int loc,val;
15     node(int l,int v):loc(l),val(v){};
16 };
17 void bfs(int m) {//(z,x,y)
18     vis[m] = 1;
19     queue<node>q;
20     q.push(node(m,0));
21     while(!q.empty()){
22         node temp = q.front();
23         q.pop();
24         if(temp.loc == k) {
25             ans = temp.val;
26             break;
27         }
28         int fx = temp.loc - 1;
29         if(fx >= 0&& fx <= 100000 &&!vis[fx])
30             {
31                 vis[fx] = 1;
32                 q.push(node(fx,temp.val+1));
33             }
34         fx = temp.loc + 1;
35         if(fx >= 0&& fx <= 100000 &&!vis[fx]){
36             vis[fx] = 1;
37             q.push(node(fx,temp.val+1));
38             }
39         fx = temp.loc*2;
40         if(fx >= 0&& fx <= 100000 &&!vis[fx]){
41             vis[fx] = 1;
42             q.push(node(fx,temp.val+1));
43         }
44     }
45 }
46 int main()
47 {
48     cin >> n >> k;
49     bfs(n);
50     cout << ans << endl;
51     return 0;
52 }

 

posted on 2019-08-14 17:43  一只小毛球  阅读(162)  评论(0编辑  收藏  举报

导航