P2895做题笔记

P2895

首先我们先理清题意:

  • 有n个流星砸在地面上,每个流星有一个x,y坐标和一个砸下来的时间t
  • 每个流星落地后会将周围四格变为焦土,这5个都无法经过(一个上午都败在了读题上)
  • 贝茜的起点是(0,0)终点是一个不会被砸的地方

这就是最重要的几点(也是最坑的)

打代码时主要问题是

  • 一个点可能先后被砸很多次,要取一个min值
  • 没有必要跑完一整个广搜,搜到安全的地方输出就行

所以综上所述,可以很容易的打出AC代码

#include<bits/stdc++.h>
using namespace std;
int n;
int a[1010][1010];
int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0};
struct node{
	int x,y,step;
	node(int xx,int yy,int st){
		x=xx; y=yy; step=st;
	}
};
queue<node> q;
bool vis[1010][1010];
void bfs(){
	q.push(node(0,0,0));
	vis[0][0]=true;
	while(!q.empty()){
		node nd=q.front(); q.pop();
		if(a[nd.x][nd.y]==10000){
			cout<<nd.step<<endl;
			return;
		}
		for(int i=0;i<4;i++){
			int xx=nd.x+dx[i];
			int yy=nd.y+dy[i];
			if(xx>=0 && yy>=0 && a[xx][yy]>nd.step+1 && vis[xx][yy]==false){
				vis[xx][yy]=true;
				q.push(node(xx,yy,nd.step+1));
			}
		}
	}
	cout<<-1;
}
int main(){
	cin>>n;
	for(int i=0;i<=1005;i++){
		for(int j=0;j<=1005;j++){
			a[i][j]=10000;
		}
	}
	for(int i=1;i<=n;i++){
		int x,y,w;
		cin>>x>>y>>w;
	    a[x][y]=min(w,a[x][y]);
		for(int j=0;j<4;j++){
			int xx=x+dx[j];
			int yy=y+dy[j];
			if(xx>=0 && yy>=0) a[xx][yy]=min(a[xx][yy],w);
		}
	}
	bfs();
	return 0;
}
posted @ 2024-06-27 15:47  WJX120423  阅读(12)  评论(0编辑  收藏  举报