洛谷P1135 奇怪的电梯 广搜

洛谷P1135 奇怪的电梯
广搜 打标记 如果到最后还搜不出来,那就直接输出答案了

 1 #include <cstdio> 
 2 #include <cstring>
 3 #include <queue>
 4 using namespace std ;
 5 
 6 const int maxn = 201 ;
 7 struct node{
 8     int pos,step ;
 9 };
10 int n,S,T,u,v ;
11 int a[maxn] ;
12 bool f[maxn] ;
13 queue<node> Q ;
14 
15 inline int bfs() 
16 {
17     Q.push( (node){S,0} ) ;
18     node p ;
19     f[ S ] = 1 ;
20     while(!Q.empty()) 
21     {
22         p = Q.front() ;
23         Q.pop() ;
24         u = p.pos ;
25         if(u==T) return p.step ;
26         v = u + a[u] ;
27         if(v>=1&&v<=n&&!f[v]) Q.push( (node){v,p.step+1} ),f[v] = 1 ; 
28         v = u - a[u] ;
29         if(v>=1&&v<=n&&!f[v]) Q.push( (node){v,p.step+1} ),f[v] = 1 ;
30     }
31      return -1 ;
32 }
33 
34 int main() 
35 {
36      scanf("%d%d%d",&n,&S,&T) ;
37      for(int i=1;i<=n;i++) scanf("%d",&a[ i ]) ;
38       int ans = bfs() ;
39      printf("%d",ans) ;
40      
41     return 0 ; 
42 }
原文地址:https://www.cnblogs.com/third2333/p/6896110.html