strange lift contest4 简单bfs
#include <stdio.h>
#include <deque>
#include <string.h>
using namespace std;
deque<int>q;
int dp[4000000];
int floor[110000];
int visit[1100];
int main( )
{
int N, i, t, v,s ,e, flag;
while (scanf("%d",&N), N)
{
flag = 0;
q.clear();
scanf("%d%d",&s,&e);
memset(dp, 0, sizeof(dp));
memset(visit,0,sizeof(visit));
for (i = 1; i <= N; i++)
scanf("%d",&floor[i]);
q.push_back(s);
visit[s] = 1;
while ( !q.empty( ))
{
t = q.front( );
if (t == e) {
printf("%d\n",dp[t]);
flag = 1;
break;
}
v = t + floor[t];
if ( v >= 1 && v <= N && !visit[v]) {
q.push_back(v);
dp[v] = dp[t] + 1;
visit[v] = 1;
}
v = t - floor[t];
if ( v >= 1 && v <= N && !visit[v]) {
q.push_back(v);
dp[v] = dp[t] + 1;
visit[v] = 1;
}
q.pop_front( );
}
if (!flag)
puts("-1");
}
return 0;
}
posted on 2011-08-04 07:45 more think, more gains 阅读(151) 评论(0) 编辑 收藏 举报