POJ 3278 catch that cow

暑假里的一道题了,经典BFS。正好开始学C++,于是用queue实现一下,还要练习培养一下代码风格。

最近用ubuntu用的有点不习惯win7了。囧,ubuntu下没有熟悉的IDE。ubuntu下的codeblocks感觉有点难用,向马学长学习,学习Vim中,用的不是很习惯,要努力。

View Code
 1 #include <iostream>
 2 #include <queue>
 3 #define size 100005
 4 using namespace std;
 5 queue <int> x;
 6 int visit[size],step[size];
 7 int bfs(int n,int k)
 8 {
 9     int head,next;
10     x.push(n);
11     visit[n] = 1;
12     step[n] = 0;
13     while(!x.empty())
14     {
15         head = x.front();
16         x.pop();
17         for(int i = 0;i < 3;i++)
18         {
19             if(i == 0)
20             next = head - 1;
21             else if(i == 1)
22             next = head + 1;
23             else next = head * 2;
24             if(next > size || next < 0)
25             continue;
26             if(!visit[next])
27             {
28                 x.push(next);
29                 step[next] = step[head] + 1;
30                 visit[next] = 1;
31             }
32             if(next == k)
33             return step[next];
34         }
35     }
36     return 0;
37 }
38 int main()
39 {
40     int n,k;
41     cin >> n >> k;
42     if(n >= k)
43     cout << n-k << endl;
44     else
45     cout << bfs(n,k) << endl;
46     return 0;
47 }
原文地址:https://www.cnblogs.com/timeship/p/2684029.html