标签:
Catch That Cow4
//此题是在一条水平线上追赶羊,分为三种情况 x-1 /x+1 /2*x 注意边界即可
#include <stdio.h> #include <string.h> #include <queue> using namespace std; int n,start,stop; int p[210]; int vis[210]; struct Node { int floor; int step; }; int bfs(int flo) { queue <Node> q; memset(vis,0,sizeof(vis)); Node a; a.floor=flo,a.step=0; q.push(a); while(!q.empty()) { Node b=q.front(); q.pop(); vis[b.floor]=1; if(b.floor==stop) return b.step; for(int i=0;i<2;i++) //0 up 1 down { Node c=b; if(i==0) c.floor=c.floor+p[c.floor]; else c.floor=c.floor-p[c.floor]; if(!vis[c.floor]&&c.floor>=1&&c.floor<=n) { c.step++; q.push(c); vis[c.floor]=1; } } } return -1; } int main() { while(~scanf("%d",&n)&&n) { scanf("%d%d",&start,&stop); for(int i=1;i<=n;i++) scanf("%d",&p[i]); printf("%d\n",bfs(start)); } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/a73265/article/details/46860357