suyikai @ 2024-09-07 00:14:52
#include <iostream>
#include<vector>
#include<queue>
using namespace std;
int mp[310][310]={0},st[310][310]={0};
int m;//流星数量
int dx[]={1,-1,0,0};
int dy[]={0,0,1,-1};
int ans = -1; // 记录答案
struct node{
int x,y,t;
};
deque<node> q;
void bfs(){
q.push_front({0,0,0});
st[0][0] =1;
while(!q.empty()){
auto t = q.front();q.pop_front();
int ti = t.t;
for(int i=0;i<4;i++){
int x = t.x+dx[i], y =t.y+dy[i];
if(x>300 || x<0 || y >300 || y<0) continue;
if(ti+1>=mp[x][y] && mp[x][y]!=-1) continue;
if(st[x][y]!=0) continue;
if(mp[x][y] ==-1){
if(ans ==-1) ans = ti+1;
else{
ans = min(ans,ti+1);
}
}
st[x][y] = 1;
q.push_back({x,y,ti+1});
}
}
}
int main(){
for(int i=0;i<310;i++){
for(int j=0;j<310;j++) mp[i][j] = -1;
}
cin>>m;
while(m--){
int x,y,t;
cin>>x>>y>>t;
if(mp[x][y] ==-1) mp[x][y] = t;
else{
mp[x][y] = min(mp[x][y],t);
}
for(int i=0;i<4;i++){
int xx = x+dx[i], yy = y+dy[i];
if(xx>300 || xx<0 || yy >300 || yy<0) continue;
if(mp[xx][yy] ==-1 ) mp[xx][yy] = t;
else{
mp[xx][yy] = min(mp[xx][yy],t);
}
}
}
// for(int i=0;i<=10;i++){
// for(int j=0;j<=10;j++){
// cout<<mp[i][j]<<" ";
// }
// cout<<endl;
// }
bfs();
cout<<ans;
}
by yu25 @ 2024-09-21 23:03:29
你错这里