题目链接:http://i.cnblogs.com/EditPosts.aspx?opt=1
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7909 Accepted Submission(s):
2498
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <cmath> 5 #include <queue> 6 7 using namespace std; 8 9 int dir[3]= {1,-1}; 10 11 struct node 12 { 13 int x,step; 14 } s,ss; 15 16 int bfs(int n,int k) 17 { 18 queue<node>q,qq; 19 s.x=n; 20 s.step=0; 21 int vis[100010]= {0}; 22 q.push(s); 23 while (!q.empty()) 24 { 25 s=q.front(); 26 q.pop(); 27 if (s.x==k) 28 return s.step; 29 for (int i=0; i<2; i++) 30 { 31 ss.x=s.x+dir[i]; 32 ss.step=s.step+1; 33 if (ss.x>=0&&ss.x<=100000) 34 if (!vis[ss.x]) 35 { 36 vis[ss.x]=1; 37 q.push(ss); 38 } 39 } 40 ss.x=s.x*2; 41 ss.step=s.step+1; 42 if (ss.x>=0&&ss.x<=100000) 43 { 44 if (!vis[ss.x]) 45 { 46 vis[ss.x]=1; 47 q.push(ss); 48 } 49 } 50 } 51 return 0; 52 } 53 54 int main () 55 { 56 int n,k; 57 while (~scanf("%d%d",&n,&k)) 58 { 59 printf ("%d\n",bfs(n,k)); 60 } 61 return 0; 62 }
hdu 2717 Catch That Cow(广搜bfs)
原文:http://www.cnblogs.com/qq-star/p/4295862.html