#include <iostream>
#include <cmath>
using namespace std;
int m;
int time=0;
int finalmap[310][310]={0},currentmap[310][310]={0};
int sequence[50010]={0};
int s=1;
int startime[50010]={0};
int starpoint[2][50010]={0};
int currentpoint[2][10000]={0};
int comingpoint[2][10000]={0};
int currentpointnum=1;
int comingpointnum=0;
int mov(int x,int y)
{
if (finalmap[x][y]==0)
{
return 1;
}
else
{
if (currentmap[x][y]==0)
{
comingpoint[x][y]=time;
comingpointnum++;
comingpoint[0][comingpointnum]=x;
comingpoint[1][comingpointnum]=y;
return 0;
}
return 0;
}
}
int main(void)
{
finalmap[0][0]=-1;
currentmap[0][0]=-1;
for (int i=1;i<=309;i++)
{
finalmap[0][i]=-1;
currentmap[0][i]=-1;
finalmap[i][0]=-1;
currentmap[i][0]=-1;
}
cin>>m;
scanf("%d%d%d",&starpoint[0][1],&starpoint[1][1],&startime[1]);
starpoint[0][1]++;
starpoint[1][1]++;
sequence[1]=1;
finalmap[starpoint[0][1]][starpoint[1][1]]=-1;
finalmap[starpoint[0][1]+1][starpoint[1][1]]=-1;
finalmap[starpoint[0][1]-1][starpoint[1][1]]=-1;
finalmap[starpoint[0][1]][starpoint[1][1]+1]=-1;
finalmap[starpoint[0][1]][starpoint[1][1]-1]=-1;
for (int i=2;i<=m;i++)
{
scanf("%d%d%d",&starpoint[0][i],&starpoint[1][i],&startime[i]);
starpoint[0][i]++;
starpoint[1][i]++;
sequence[i]=i;
finalmap[starpoint[0][i]][starpoint[1][i]]=-1;
finalmap[starpoint[0][i]+1][starpoint[1][i]]=-1;
finalmap[starpoint[0][i]-1][starpoint[1][i]]=-1;
finalmap[starpoint[0][i]][starpoint[1][i]+1]=-1;
finalmap[starpoint[0][i]][starpoint[1][i]-1]=-1;
//sort
if (startime[sequence[i]]<startime[sequence[i-1]])
{
if (startime[sequence[i]]<startime[sequence[1]])
{
for (int j=i;j>=1;j--)
{
sequence[j+1]=sequence[j];
}
sequence[1]=i;
}
else
{
int p1=1,p2=i-1;
while (p2-p1>1)
{
if (startime[(p1+p2)/2]>startime[i])
{
p2=(p1+p2)/2;
}
else
{
p1=(p1+p2)/2;
}
}
for (int j=i;j>=p2;j--)
{
sequence[j+1]=sequence[j];
}
sequence[p2]=i;
}
}
}
currentpoint[0][1]=1;
currentpoint[1][1]=1;
currentmap[1][1]=1;
if (finalmap[1][1]==0)
{
cout<<'0';
return 0;
}
while (currentpointnum)
{
time++;
comingpointnum=0;
while (time==startime[sequence[s]])
{
currentmap[starpoint[0][sequence[s]]][starpoint[1][sequence[s]]]=-1;
currentmap[starpoint[0][sequence[s]]+1][starpoint[1][sequence[s]]]=-1;
currentmap[starpoint[0][sequence[s]]-1][starpoint[1][sequence[s]]]=-1;
currentmap[starpoint[0][sequence[s]]][starpoint[1][sequence[s]]+1]=-1;
currentmap[starpoint[0][sequence[s]]][starpoint[1][sequence[s]]-1]=-1;
s++;
}
for (int i=1;i<=currentpointnum;i++)
{
if (mov(currentpoint[0][i]+1,currentpoint[1][i]))
{
cout<<time;
return 0;
}
if (mov(currentpoint[0][i]-1,currentpoint[1][i]))
{
cout<<time;
return 0;
}
if (mov(currentpoint[0][i],currentpoint[1][i]+1))
{
cout<<time;
return 0;
}
if (mov(currentpoint[0][i],currentpoint[1][i]-1))
{
cout<<time;
return 0;
}
}
for (int i=1;i<=comingpointnum;i++)
{
currentpoint[0][i]=comingpoint[0][i];
currentpoint[1][i]=comingpoint[1][i];
}
currentpointnum=comingpointnum;
}
cout<<"-1";
return 0;
}
做了一天了,好不容易样例过了,这样真的让我破防