Poj-3278
来源:互联网 发布:数据恢复精灵好用吗 编辑:程序博客网 时间:2024/05/16 12:12
一个农夫和一头牛,已知坐标两者坐标,从X可移动X-1,X+1,X*2步,最少几步抓到牛。
#include <iostream>#include<cstdio>#include<algorithm>#include<cstring>using namespace std;struct node{ int num; int count; node() {} node(int n,int c) { num=n; count = c; }};node queue[1000000];int vis[2000010];int bfs(int n,int k){ if(n==k) return 0; node n0(n,0); memset(vis,0,sizeof(vis)); int t=0,e=0; queue[e++]=n0; vis[n] = 1; while(t!=e) { node temp=queue[t++]; if(t>=1000000) t=0; node n1(temp.num -1,temp.count+1); node n2(temp.num +1,temp.count+1); node n3(temp.num *2,temp.count+1); if(n1.num==k) return n1.count; if(n2.num==k) return n2.count; if(n3.num==k) return n3.count; if(n1.num<150005&&n1.num>=0&&vis[n1.num]==0) { queue[e++]=n1; if(e>=1000000) e=0; vis[n1.num]=1; } if(n2.num<150005&&n2.num>=0&&vis[n2.num]==0) { queue[e++]=n2; if(e>=1000000) e=0; vis[n2.num]=1; } if(n3.num<150005&&n3.num>=0&&vis[n3.num]==0) { queue[e++]=n3; if(e>=1000000) e=0; vis[n3.num]=1; } }}int main(){ int n,k; while(~scanf("%d%d",&n,&k)) { printf("%d\n",bfs(n,k)); } return 0;}
0 0
- POJ 3278
- POJ 3278
- POJ 3278
- POJ 3278
- poj 3278
- poj 3278
- POJ 3278
- poj 3278
- POJ 3278
- poj 3278
- poj 3278
- poj 3278
- poj-3278
- poj--3278
- poj-3278
- poj 3278
- poj 3278
- poj 3278
- LeetCode 141:Linked List Cycle 官方解答翻译
- [BZOJ 3676][APIO 2014]回文串
- hdu 4501 小明系列故事――买年货
- 火药,X射线和青霉素的发现
- 局部排序
- Poj-3278
- android 学习之ExpandableListView
- js问题
- 关于Eclipse使用OkHttp
- HDU3605 Escape(最大流)
- 设计模式之适配器模式
- HTTPS到底是个啥玩意儿?
- 标题:吃一碗荠菜馄饨,享受阳光的味道
- Hdu--4255 螺旋矩阵