Catch That Cow
思路:利用bfs进行广搜,第一个找到的即为最短次数。。。。
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2717
以下是AC代码:
思路:利用bfs进行广搜,第一个找到的即为最短次数。。。。
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2717
以下是AC代码:
#include"iostream" #include"cstring" #include"cstdio" #include"queue" #define max 100000 using namespace std; int nx[3]={1,-1}; typedef struct node { int x,step; }chao; int v[100010]; void bfs(int n,int k) { queue <chao> q; chao from,to; from.x=n; from.step=0; q.push(from); memset(v,0,sizeof(v)); v[n]=1; while(!q.empty()) { from=q.front(); q.pop(); if(from.x==k) { printf("%d\n",from.step); return ; } for(int i=0;i<3;i++) { if(i==2) { to.x=2*from.x; if(to.x>=0&&to.x<=max&&!v[to.x]) { v[to.x]=1; to.step=from.step+1; q.push(to); } } else { to.x=from.x+nx[i]; if(to.x<=max&&to.x>=0&&!v[to.x]) { v[to.x]=1; to.step=from.step+1; q.push(to); } } } } } int main() { int from,to; while(scanf("%d%d",&from,&to)!=EOF) { bfs(from,to); } return 0; }