Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2385    Accepted Submission(s): 862

       本题相当简单,用广搜即可通过,没有任何技巧而言。

代码:

1 #include<stdio.h>
2 #include<string.h>
3 #include<queue>
4  using namespace std;
5 typedef struct node
6 {
7 int data;
8 int time;
9 }NODE;
10  int main()
11 {
12 queue<NODE > qu;NODE cur,next;
13 int n,a,b,i,mark1;int mark[205],f[205];
14 while(scanf("%d",&n)!=EOF)
15 {
16 if(n==0)
17 break;
18 scanf("%d%d",&a,&b);
19 for(i=1;i<=n;i++)
20 scanf("%d",&f[i]);
21 mark1=0;
22 while(!qu.empty ())
23 qu.pop();
24 cur.data=a;cur.time =0;
25 memset(mark,0,sizeof(mark));
26 mark[cur.data]=1;
27 qu.push(cur);
28 while(!qu.empty ())
29 {
30 cur=qu.front ();
31 qu.pop();
32 if(cur.data==b)
33 {
34 mark1=1;
35 break;
36 }
37 for(i=1;i<=2;i++)
38 {
39 if(i%2==0)
40 next.data=cur.data +f[cur.data];
41 else
42 next.data =cur.data-f[cur.data];
43 if(next.data>=1 && next.data<=n && !mark[next.data])
44 {
45 next.time=cur.time +1;
46 mark[next.data]=1;
47 qu.push (next);
48 }
49 }
50 }
51 if(mark1)
52 printf("%d\n",cur.time );
53 else
54 printf("-1\n");
55 }
56 return 0;
57 }