社区讨论

35分求调

P2895[USACO08FEB] Meteor Shower S参与者 3已保存回复 4

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
4 条
当前快照
1 份
快照标识符
@lykyjlks
此快照首次捕获于
2024/07/14 10:49
2 年前
此快照最后确认于
2024/07/14 12:07
2 年前
查看原帖
代码:
CPP
#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(){
	//输入n 结构体初始化 
    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].step = 100000;
			a[i][j].b = 0;
		}
    }
    //输入其他数据 做标记 
    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[j]<1 || yy+dy[j]<1) continue;
			a[xx+dx[j]][yy+dy[j]].t=tt;
		}
    }
    //创建队列 队列初始化 
    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==1) 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;
}

请大佬们帮我调一下代码!

回复

4 条回复,欢迎继续交流。

正在加载回复...