Qiuqiqiu  
不管道路多么崎岖坎坷,我永远不停下追逐梦想的脚步!

http://acm.hdu.edu.cn/showproblem.php?pid=1548

我的代码
 1 #include <cstdio>
2 #include <cstring>
3 #include <queue>
4 using namespace std;
5 const int N=210;
6 int n,k[N],vis[N],dis[N];
7 queue<int> q;
8 int bfs(int s,int e)
9 {
10 if (s==e) return 0;
11 while (!q.empty()) q.pop();
12 vis[s]=1; dis[s]=0;
13 q.push(s);
14 int x,nx;
15 while (!q.empty())
16 {
17 x=q.front(); q.pop();
18 for (int d=0;d<2;d++)
19 {
20 if (d) nx=x+k[x];
21 else nx=x-k[x];
22 if (nx<1 || nx>n || vis[nx]) continue;
23 vis[nx]=1;
24 dis[nx]=dis[x]+1;
25 if (nx==e) return dis[nx];
26 q.push(nx);
27 }
28 }
29 return -1;
30 }
31 int main()
32 {
33 int a,b,i;
34 while (scanf("%d%d%d",&n,&a,&b) && n)
35 {
36 memset(vis,0,sizeof(vis));
37 memset(dis,0,sizeof(dis));
38 for (i=1;i<=n;i++) scanf("%d",&k[i]);
39 printf("%d%\n",bfs(a,b));
40 }
41 return 0;
42 }

 

posted on 2012-01-14 17:53  Qiuqiqiu  阅读(141)  评论(0编辑  收藏  举报