90分求助, #2 WA
查看原帖
90分求助, #2 WA
538085
liysjianttso楼主2023/3/19 12:55

谁能帮我看一下为什么第二个测试点是WA,谢谢! 主体是参照深基上改的,不知道有什么问题,求各位指点

#include<stdio.h>
#include<queue>
using namespace std;
int n,a,b,f[10000],ans=-1,vis[10000];
struct Coord{
	int floor,times;
}; 
queue<Coord> c;
int main(){
	scanf("%d%d%d",&n,&a,&b);
	c.push((Coord){a,0});
	for(int i = 1;i<=n;i++){
		scanf("%d",&f[i]);
	}
	while(!c.empty()){
		Coord now = c.front();
		c.pop();
		
		int up = now.floor+f[now.floor];
		if(up==b){ans=now.times+1;break;}
		if(!vis[up]){
			c.push((Coord){up,now.times+1});
			vis[up]=1;
		}
		int down = now.floor-f[now.floor];
		
		if(down==b){ans = now.times+1;break;}
		if(!vis[down]){
			if(down>=1){
				c.push((Coord){down,now.times+1});
				vis[down] = 1;
			}
		}
		
	}
	printf("%d",ans);
}
2023/3/19 12:55
加载中...