I used two methods to do this. DFS does not have an AC and I don't know where the error is. I posted a Post saying that I have never been enlightened. The second classic method BFS has passed, 76 MS 360 k, the idea is simple, and it is also a very standard breadth-first search.
Code:
BFS:
[Cpp]
# Include <iostream>
# Include <queue>
Using namespace std;
Int n, a, B;
Bool visit [210];
Int ki [210];
Struct node {
Int x, step;
} P, q;
Queue <node> Q;
Int bfs (){
While (! Q. empty () Q. pop ();
P. x =;
P. step = 0;
Q. push (p );
Visit [p. x] = 1;
If (p. x = B) return 0;
While (! Q. empty ())
{
P = Q. front ();
Q. pop ();
If (p. x = B) return p. step;
For (int I = 0; I <2; I ++)
{
If (I = 0 ){
Q. x = p. x + ki [p. x];
If (q. x> n) continue;
If (visit [q. x]) continue;
Q. step = p. step + 1;
Visit [q. x] = 1;
Q. push (q );
}
If (I = 1 ){
Q. x = p. x-ki [p. x];
If (q. x <1) continue;
If (visit [q. x]) continue;
Q. step = p. step + 1;
Visit [q. x] = 1;
Q. push (q );
}
}
}
Return-1;
}
Int main ()
{
While (cin> n)
{
If (n = 0) break;
Cin> a> B;
For (int I = 1; I <= n; I ++)
Cin> ki [I];
Memset (visit, 0, sizeof (visit ));
Cout <bfs () <endl;
}
Return 0;
}
# Include <iostream>
# Include <queue>
Using namespace std;
Int n, a, B;
Bool visit [210];
Int ki [210];
Struct node {
Int x, step;
} P, q;
Queue <node> Q;
Int bfs (){
While (! Q. empty () Q. pop ();
P. x =;
P. step = 0;
Q. push (p );
Visit [p. x] = 1;
If (p. x = B) return 0;
While (! Q. empty ())
{
P = Q. front ();
Q. pop ();
If (p. x = B) return p. step;
For (int I = 0; I <2; I ++)
{
If (I = 0 ){
Q. x = p. x + ki [p. x];
If (q. x> n) continue;
If (visit [q. x]) continue;
Q. step = p. step + 1;
Visit [q. x] = 1;
Q. push (q );
}
If (I = 1 ){
Q. x = p. x-ki [p. x];
If (q. x <1) continue;
If (visit [q. x]) continue;
Q. step = p. step + 1;
Visit [q. x] = 1;
Q. push (q );
}
}
}
Return-1;
}
Int main ()
{
While (cin> n)
{
If (n = 0) break;
Cin> a> B;
For (int I = 1; I <= n; I ++)
Cin> ki [I];
Memset (visit, 0, sizeof (visit ));
Cout <bfs () <endl;
}
Return 0;
}
DFS:
[Cpp]
# Include <iostream>
# Include <string>
# Include <cstdio>
# Include <cmath>
# Include <vector>
# Include <algorithm>
# Include <sstream>
# Include <cstdlib>
# Include <fstream>
# Include <queue>
Using namespace std;
Int n, a, B, cnt, flag;
Int cmin;
Bool visit [210];
Int ki [210];
Void dfs (int x ){
If (x = B ){
Flag = 1;
If (cnt <cmin) cmin = cnt;
Return;
}
If (flag) return;
If (x + ki [x] <= n ){
If (! Visit [x + ki [x]) {
Cnt ++;
Visit [x + ki [x] = 1;
Dfs (x + ki [x]);
Visit [x + ki [x] = 0;
Cnt --;
}
}
If (x-ki [x]> = 1 ){
If (! Visit [x-ki [x]) {
Cnt ++;
Visit [x-ki [x] = 1;
Dfs (x-ki [x]);
Visit [x-ki [x] = 0;
Cnt --;
}
}
}
Int main ()
{
// Ifstream fin;
// Fin. open ("data1.txt ");
While (cin> n)
{
If (n = 0) break;
Cin> a> B;
For (int I = 1; I <= n; I ++)
Cin> ki [I];
Cnt = 0;
Cmin = 999999;
Flag = 0;
Memset (visit, 0, sizeof (visit ));
Visit [a] = 1;
Dfs ();
If (flag)
Cout <cmin <endl;
Else cout <-1 <endl;
}
Return 0;
}
# Include <iostream>
# Include <string>
# Include <cstdio>
# Include <cmath>
# Include <vector>
# Include <algorithm>
# Include <sstream>
# Include <cstdlib>
# Include <fstream>
# Include <queue>
Using namespace std;
Int n, a, B, cnt, flag;
Int cmin;
Bool visit [210];
Int ki [210];
Void dfs (int x ){
If (x = B ){
Flag = 1;
If (cnt <cmin) cmin = cnt;
Return;
}
If (flag) return;
If (x + ki [x] <= n ){
If (! Visit [x + ki [x]) {
Cnt ++;
Visit [x + ki [x] = 1;
Dfs (x + ki [x]);
Visit [x + ki [x] = 0;
Cnt --;
}
}
If (x-ki [x]> = 1 ){
If (! Visit [x-ki [x]) {
Cnt ++;
Visit [x-ki [x] = 1;
Dfs (x-ki [x]);
Visit [x-ki [x] = 0;
Cnt --;
}
}
}
Int main ()
{
// Ifstream fin;
// Fin. open ("data1.txt ");
While (cin> n)
{
If (n = 0) break;
Cin> a> B;
For (int I = 1; I <= n; I ++)
Cin> ki [I];
Cnt = 0;
Cmin = 999999;
Flag = 0;
Memset (visit, 0, sizeof (visit ));
Visit [a] = 1;
Dfs ();
If (flag)
Cout <cmin <endl;
Else cout <-1 <endl;
}
Return 0;
}