HDOJ 2717 Catch That Cow (BFS)
2012-08-16 10:53
441 查看
http://acm.hdu.edu.cn/showproblem.php?pid=2717
题意:从N到K有3中走法:坐标加1、减1、乘2。求从N到K的最短步数。
思路:BFS
题意:从N到K有3中走法:坐标加1、减1、乘2。求从N到K的最短步数。
思路:BFS
#include<cstdio> #include<cstring> #include<queue> using namespace std; int n,k,cnt[111111]; void bfs() { queue <int> q; q.push(n); cnt =0; while(!q.empty()) { int now=q.front(); q.pop(); if(k==now) break; int next=now+1; if(!cnt[next]) { q.push(next); cnt[next]=cnt[now]+1; } next=now-1; if(next>=0&&!cnt[next]) { q.push(next); cnt[next]=cnt[now]+1; } next=2*now; if(next<=100000&&!cnt[next]&&next-k<k-now) { q.push(next); cnt[next]=cnt[now]+1; } } } int main() { while(scanf("%d%d",&n,&k)==2) { memset(cnt,0,sizeof(cnt)); if(n>=k) cnt[k]=n-k; else bfs(); printf("%d\n",cnt[k]); } return 0; }
相关文章推荐
- hdoj 2717 Catch That Cow 【bfs】
- HDOJ 题目2717 Catch That Cow(BFS)
- HDOJ 2717 Catch That Cow (BFS)
- hdoj 2717 Catch That Cow【bfs】
- HDOJ 2717 Catch That Cow(BFS)
- HDOJ-2717-Catch That Cow【BFS】
- HDOJ 2717 Catch That Cow【BFS】
- HDoj-2717-Catch That Cow-BFS
- 【BFS】【HDOJ-2717】Catch That Cow
- HDOJ 2717 Catch That Cow
- HDOJ/HDU 2717 Catch That Cow 一维广度优先搜索 so easy..............
- hdu 2717:Catch That Cow(bfs广搜,经典题,一维数组搜索)
- HDU 2717 Catch That Cow(bfs)
- hdu 2717 Catch That Cow bfs
- HDOJ/HDU 2717 Catch That Cow 一维广度优先搜索 so easy..............
- HDOJ-2717(POJ-3278) Catch That Cow
- HDU 2717 Catch That Cow (BFS)
- HDU 2717 Catch That Cow BFS
- HDU 2717 Catch That Cow(BFS,每次有3种走路方式,问最少的步数到达目的地)
- HDU 2717 Catch That Cow (BFS)