【题目描述】
大楼的每一层楼都可以停电梯,而且第i层楼(1≤i≤N)上有一个数字Ki(0≤=Ki≤=N)。电梯只有四个按钮:开,关,上,下。上下的层数等于当前楼层上的那个数字。当然,如果不能满足要求,相应的按钮就会失灵。例如:3 3 1 2 5代表了Ki(K1=3,K2=3,……),从一楼开始。在一楼,按“上”可以到4楼,按“下”是不起作用的,因为没有-2楼。那么,从A楼到B楼至少要按几次按钮呢?
【输入】
共有二行,第一行为三个用空格隔开的正整数,表示N,A,B(1≤N≤200, 1≤A,B≤N),第二行为N个用空格隔开的正整数,表示Ki。
【输出】
一行,即最少按键次数,若无法到达,则输出-1。
【输入样例】
5 1 5
3 3 1 2 5
【输出样例】
3
【来源】
No
宽搜基础题,主要是题意吧,虽然宽搜都是要有标记数组,但是这题我一开始做的时候并不觉得要标记,关键点我觉得就是题意和标记数组
#include<cstdio>
#include<queue>
#include<iostream>
using namespace std;
int N,A,B;
struct node{
int h;
int step;
}s,t,Node;
int bk[205];
int main(){
cin>>N>>A>>B;
int num[N];
s.h=A,s.step=0,t.h=B;
for(int i=1;i<=N;i++) cin>>num[i];
queue<node> q;
q.push(s);
bk[A]=1;
while(!q.empty()){
node top=q.front();
q.pop();
if(top.h == t.h) {
cout<<top.step;
return 0;
}
if(bk[top.h+num[top.h]] ==0 && top.h+num[top.h] <=N) {
Node.h=top.h+num[top.h] ,Node.step=top.step+1;
q.push(Node);
bk[top.h+num[top.h]] =1;
}
if(bk[top.h-num[top.h]] ==0 && top.h-num[top.h] >0 ) {
Node.h=top.h-num[top.h] ,Node.step=top.step+1;
q.push(Node);
bk[top.h-num[top.h]] =1;
}
}
cout<<"-1";
return 0;
}
我看还有别的做法,最短路径,dp什么的,目前还是先bfs