LittleOcean @ 2024-10-13 20:33:14
#include<iostream>
#include<queue>
using namespace std;
#include<cstring>
int n,a,b;
int dx[]={-1,0,1,0};
int dy[]={0,1,0,-1};
int map[305][305];
typedef pair<int,int>PII;
queue <PII> q;
int dist[305][305];
int bfs(int x,int y){
memset(dist,-1,sizeof dist);
q.push({x,y});
dist[x][y]=0;
for(int i=0;i<4;i++){
auto t=q.front();
q.pop();
a=t.first+dx[i],b=t.second+dy[i];
if(a<0 || b<0 )continue;
if(dist[t.first][t.second]+1>=map[a][b])continue;
dist[a][b]=dist[t.first][t.second]+1;
q.push({a,b});
if(map[a][b]==1e9)break;
}
return dist[a][b];
}
int main(){
memset(map,1e9,sizeof map);
cin>>n;
while(n--){
int x1,y1,t1;
cin>>x1>>y1>>t1;
map[x1][y1]=min(t1,map[x1][y1]);
for(int i=0;i<4;i++){
map[x1][y1]=t1;
int c=x1+dx[i],d=y1+dy[i];
if(c<0 || c>301 || d<0 || d>301){
map[c][d]=min(t1,map[c][d]);
}
}
}
int res=bfs(0,0);
cout<<res;
}