A strange lift

广搜:

哈哈。我用了stl..偷懒啦。。懒得写队列。。

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<deque>
#include<algorithm>
using namespace std;
deque<int>q;
int floor,N,M,A[300];
int mark[300];
int dp[300];
int main( )
{ 
    int i,j,x;
  while(scanf("%d",&floor),floor)
  {
   memset(mark,0,sizeof(mark));
   memset(dp,0,sizeof(dp));
   q.clear();
   scanf("%d%d",&N,&M);
   memset(A,0,sizeof(A));
   for(i=1;i<=floor;i++)
   scanf("%d",&A[i]);
   q.push_back(N);
   x=N;
   while(x!=M)
   {
    if((x+A[x]<=floor)&&!mark[x+A[x]])
    {
     mark[x+A[x]]=1;
     q.push_back(x+A[x]);
     dp[x+A[x]]=dp[x]+1;
    }
    if((x-A[x]>=1)&&!mark[x-A[x]])
    {
     mark[x-A[x]]=1;
     q.push_back(x-A[x]);
     dp[x-A[x]]=dp[x]+1;
    }
    q.pop_front();
     if(q.empty()) break;
    x=q.front();
   
   }
   if(x==M) printf("%d\n",dp[x]);
   else puts("-1");
   
  }
  return 0;
}
   

posted on 2011-05-07 22:15  more think, more gains  阅读(130)  评论(0编辑  收藏  举报

导航