I_AM_Nigger @ 2023-06-22 23:19:33
#include<bits/stdc++.h>
using namespace std;
int tx[4] = {1,0,-1,0};
int ty[4] = {0,1,0,-1};
int ans = INT_MAX;
int mp[500][500];
int vis[500][500];
queue <int> qx;
queue <int> qy;
int n,ending;
int ex,ey;
struct meteor
{
int x[100];
int y[100];
int cnt;
}s[1010];
int main()
{
cin >> n;
for(int i = 1;i<=n;i++)
{
int a,b,c;
cin >> a >> b >> c;
ex = max(ex,a);ey = max(ey,b);
s[c].x[s[c].cnt] = a;
s[c].y[s[c].cnt] = b;
s[c].cnt++;
ending = max(ending,c);
}
ex += 10;ey += 10;
qx.push(0);
qy.push(0);
vis[0][0] = 1;
while(!qx.empty() && !qy.empty())
{
int x = qx.front();int y = qy.front();
qx.pop();qy.pop();
// cout << x << " " << y << " " << mp[x][y] << endl;
for(int i = 0;i<s[mp[x][y]].cnt;i++)
{
// if(mp[x][y] == 5){cout << "yes\n";}
vis[s[mp[x][y]].x[i]][s[mp[x][y]].y[i]] = 2;
for(int j = 0;j<4;j++)
{
int nx = s[mp[x][y]].x[i] + tx[j];int ny = s[mp[x][y]].y[i] + ty[j];
if(nx >= 0 && ny >= 0 && nx <= ex && ny <= ey)
{
vis[nx][ny] = 2;
}
}
}
if(mp[x][y] > ending)
{
break;
}
for(int i = 0;i<4;i++)
{
int nx = x + tx[i];int ny = y + ty[i];
if(nx >= 0 && ny >= 0 && nx <= ex && ny <= ey && vis[nx][ny] == 0)
{
vis[nx][ny] = 1;
mp[nx][ny] = mp[x][y] + 1;
qx.push(nx);qy.push(ny);
}
}
}
// cout << "vis:\n";for(int i = 0;i<=ex;i++){for(int j = 0;j<=ey;j++){cout << vis[i][j] << " ";}cout << '\n';}
// cout << "mp:\n";for(int i = 0;i<=ex;i++){for(int j = 0;j<=ey;j++){cout << mp[i][j] << " ";}cout << '\n';}
for(int i = 0;i<=ex;i++)
{
for(int j = 0;j<=ey;j++)
{
if(vis[i][j] == 1)
{
ans = min(ans,mp[i][j]);
}
}
}
if(ans != INT_MAX)
{
cout << ans << endl;
}
else
{
cout << -1 << endl;
}
return 0;
}
错的很奇怪,大犇995