记录编号 |
154696 |
评测结果 |
AAAAAAAAAA |
题目名称 |
[HDU 1548] 奇怪的电梯 |
最终得分 |
100 |
用户昵称 |
devil |
是否通过 |
通过 |
代码语言 |
C++ |
运行时间 |
0.008 s |
提交时间 |
2015-03-23 22:54:05 |
内存使用 |
0.31 MiB |
显示代码纯文本
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <map>
#include <queue>
#include <cstring>
#include <sstream>
#include <algorithm>
using namespace std;
typedef long long ll;
const int MAXN=30010;
const int MAX_INT=0x7fffffff;
const int MAXT=210;
struct node
{
int lift;
int steps;
node() {lift=0;steps=0;}
};
bool vis[MAXT];
int num[MAXT];
int N,A,B;
void BFS()
{
queue<node> q;
node st;st.lift=A;
st.steps=0;
q.push(st);
vis[A]=true;
while(!q.empty())
{
node t=q.front();
q.pop();
if(t.lift==B) {printf("%d\n",t.steps);return;}
node nt=t;nt.steps++;
nt.lift=t.lift+num[t.lift];
if(nt.lift<=N&&!vis[nt.lift])
{
q.push(nt);
vis[nt.lift]=true;
}
nt.lift=t.lift-num[t.lift];
if(nt.lift>=1&&!vis[nt.lift])
{
q.push(nt);
vis[nt.lift]=true;
}
}
printf("-1\n");
}
int main()
{
///freopen("sample_data.in","r",stdin);
///freopen("data.in","r",stdin);
///freopen("data.out","w",stdout);
freopen("lift.in","r",stdin);
freopen("lift.out","w",stdout);
scanf("%d%d%d",&N,&A,&B);
memset(vis,false,sizeof(vis));
memset(num,0,sizeof(num));
for(int i=1;i<=N;i++) scanf("%d",&num[i]);
BFS();
return 0;
}