题意:0-
思路:输入时先预处理,我定义一个结构体(包含vis检测是否有陨石掉在此格,time掉落时间),所以处理完之后,只需搜索vis为0即没有陨石掉落的地方即可。
代码附上:
#include <cstdio>
#include <cstring>
#include <cctype>
#include <iostream>
#include <queue>
#include <cmath>
#include <algorithm>
#define FIN freopen("input.txt","r",stdin)
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
const int MAX = 310;
int n,res;
struct Point
{
int vis,time2;
} maze[MAX][MAX];
int time1[MAX][MAX],rec[MAX][MAX];
int dx[]={1,-1,0,0},dy[]={0,0,1,-1};
void bfs(){
queue<P>que;
rec[0][0]=1;
que.push(make_pair(0,0));
while(!que.empty()){
P p=que.front();que.pop();
if(!maze[p.first][p.second].vis) {res=time1[p.first][p.second];break;}
for(int i=0;i<4;i++){
int nx=p.first+dx[i];
int ny=p.second+dy[i];
if(0<=nx&&0<=ny&&!rec[nx][ny]){
rec[nx][ny]=1;
if(maze[nx][ny].vis&&maze[nx][ny].time2<=time1[p.first][p.second]+1) continue;
que.push(make_pair(nx,ny));
time1[nx][ny]=time1[p.first][p.second]+1;
}
}
}
}
int main()
{
//FIN;
while(~scanf("%d",&n)){
memset(maze,0,sizeof(maze));
memset(time1,0,sizeof(time1));
memset(rec,0,sizeof(rec));
res=-1;
for(int i=0;i<n;i++){
int x,y,t;
scanf("%d%d%d",&x,&y,&t);
maze[x][y].vis=1;maze[x][y].time2=maze[x][y].time2?min(t,maze[x][y].time2):t;
for(int j=0;j<4;j++){
if(0<=x+dx[j]&&0<=y+dy[j]){
maze[x+dx[j]][y+dy[j]].vis=1;maze[x+dx[j]][y+dy[j]].time2=maze[x+dx[j]][y+dy[j]].time2?min(t,maze[x+dx[j]][y+dy[j]].time2):t;
}
}
}
bfs();
if(res!=-1) printf("%d\n",res);
else printf("%d\n",res);
}
return 0;
}