A strange lift
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=1548
解题思路:
和hdu2717很相似,也是按题意来,直接搜就行了。。。
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int N = 200;
int n,A,B;
int a[N+10];
int vis[N+10];
bool bfs(){
queue<int> q;
q.push(A);
memset(vis,0,sizeof(vis));
while(!q.empty()){
int cur = q.front();
q.pop();
//cout<<cur<<endl;
if(cur == B)
return true;
int next = cur-a[cur];
if(next>=0 && !vis[next]){
vis[next] = vis[cur]+1;
q.push(next);
}
next = cur+a[cur];
if(next<=n && !vis[next]){
vis[next] = vis[cur]+1;
q.push(next);
}
}
return false;
}
int main(){
while(scanf("%d",&n),n){
scanf("%d%d\n",&A,&B);
for(int i = 1; i <= n; i++)
scanf("%d",&a[i]);
if(bfs())
printf("%d\n",vis[B]);
else
printf("-1\n");
}
return 0;
}