题目
不知道为什么又MLE一些,又TLE一些,又WA一些,又AC一些,悬赏一个关注!!!!!!!!
代码:
#include<bits/stdc++.h>
using namespace std;
int n,a,b,e[205];
bool c[205],flag=true;
struct node{
int step,d;
};
queue<node>q;
void bfs(){
node t;
t.step=0;
t.d=a;
q.push(t);
while(!q.empty()){
t=q.front();
q.pop();
node tt;
if(t.d-e[t.d]>0&&t.d-e[t.d]<=n&&c[t.d-e[t.d]]==true){
tt.d=t.d-e[t.d];
tt.step=t.step+1;
q.push(tt);
c[t.d-e[t.d]]==false;
if(tt.d==b){
printf("%d",tt.step);
flag=false;
return;
}
}
if(t.d+e[t.d]>0&&t.d+e[t.d]<=n&&c[t.d+e[t.d]]==true){
tt.d=t.d+e[t.d];
tt.step=t.step+1;
q.push(tt);
c[t.d+e[t.d]]==false;
if(tt.d==b){
printf("%d",tt.step);
flag=false;
return;
}
}
}
}
int main(){
cin>>n>>a>>b;
for(int i=1;i<=n;i++){
cin>>e[i];
}
memset(c,true,sizeof(c));
bfs();
if(flag){
cout<<-1;
}
return 0;
}
求大佬调一调