WA求调!!!
查看原帖
WA求调!!!
1355791
zhoutiance楼主2024/8/17 20:59
#include<bits/stdc++.h>
using namespace std;
int n,startx,endx; 
int vis[205],d[205];
struct node{
	int floor,step;
};
void bfs(){
	queue<node> q;
	q.push({startx,0});
	while(q.size()){
		node hd = q.front();
		q.pop();
		if(hd.floor == endx){
			printf("%d",hd.step);
			return;
		}
		for(int i = 1;i <= 2;i++){
			int nx = i%2 == 1 ? hd.floor + d[hd.floor] : hd.floor - d[hd.floor];
			int nstep = hd.step+1;
			if(nx > 0 && nx <= n && vis[endx]){
				q.push({nx,nstep});
				vis[nx] = 1;
			}
		}
	}
	printf("%d",-1);
	return;
}
int main(){
	cin>>n>>startx>>endx;
	for(int i = 1;i <= n;i++){
		cin>>d[i];
	}
	bfs();
	return 0;
}                                                                            
2024/8/17 20:59
加载中...