输出-1求调
查看原帖
输出-1求调
915921
DougNo1楼主2024/7/9 14:30

代码:

#include <bits/stdc++.h>
using namespace std;

struct point{
	int x,y;//坐标
	int t,step;//t:流星落下的时间  step:走到这里用的步数 
	bool b;//是否走过(0:没走过 1:走过) 
}a[305][305];

int main(){
	//输入1
    int n; 
    cin>>n;
    //初始化 
    for(int i=1;i<=300;i++){
		for(int j=1;j<=300;j++){
			a[i][j].x = i;
			a[i][j].y = j;
			a[i][j].t = -1;
			a[i][j].b = 0;
		}
    }
    //输入2 
    int dx[6]={0,-1,1,0,0,0},dy[6]={0,0,0,-1,1,0};
    for(int i=1;i<=n;i++){
		int xx,yy,tt;
		cin>>xx>>yy>>tt;
		xx++,yy++;
		for(int j=1;j<=5;j++){
			if(xx+dx[i]<1 || yy+dy[i]<1) continue;
			a[xx+dx[i]][yy+dy[i]].t=tt;
		}
    }
    //BFS前准备 
    queue<point> q;
    a[1][1].step=0;
    a[1][1].b=1;
    q.push(a[1][1]);
    //BFS 
    while(!q.empty()){
		point p=q.front();
		q.pop();
		
		for(int i=1;i<=4;i++){
			int xx=p.x+dx[i], yy=p.y+dy[i];
			if(xx<1 || yy<1 || a[xx][yy].b==0) continue;
			if(a[xx][yy].t == -1){
				cout<<p.step+1;
				return 0;
			}
			if(p.step+1<a[xx][yy].t){
				a[xx][yy].x=xx;
				a[xx][yy].y=yy;
				a[xx][yy].b=1;
				a[xx][yy].step=p.step+1;
				q.push(a[xx][yy]);
			} 
			
		}
    }
    cout<<-1;
	return 0;
}

结果不管输入什么,输出都是-1

请大佬们帮忙调一下代码

2024/7/9 14:30
加载中...