bzoj1611[Usaco2008 Feb]Meteor Shower流星雨

Description

去年偶們湖南遭受N年不遇到冰凍災害,如今芙蓉哥哥則據說另外一個駭人聽聞的消息: 一場流星雨即將襲擊整個霸中,因爲流星體積過大,它們沒法在撞擊到地面前燃燒殆盡, 屆時將會對它撞到的一切東西形成毀滅性的打擊。很天然地,芙蓉哥哥開始擔憂本身的 安全問題。以霸中至In型男名譽起誓,他必定要在被流星砸到前,到達一個安全的地方 (也就是說,一塊不會被任何流星砸到的土地)。若是將霸中放入一個直角座標系中, 芙蓉哥哥如今的位置是原點,而且,芙蓉哥哥不能踏上一塊被流星砸過的土地。根據預 報,一共有M顆流星(1 <= M <= 50,000)會墜落在霸中上,其中第i顆流星會在時刻 T_i (0 <= T_i <= 1,000)砸在座標爲(X_i, Y_i) (0 <= X_i <= 300;0 <= Y_i <= 300) 的格子裏。流星的力量會將它所在的格子,以及周圍4個相鄰的格子都化爲焦土,固然 芙蓉哥哥也沒法再在這些格子上行走。芙蓉哥哥在時刻0開始行動,它只能在第一象限中, 平行於座標軸行動,每1個時刻中,她能移動到相鄰的(通常是4個)格子中的任意一個, 固然目標格子要沒有被燒焦才行。若是一個格子在時刻t被流星撞擊或燒焦,那麼芙蓉哥哥 只能在t以前的時刻在這個格子裏出現。請你計算一下,芙蓉哥哥最少須要多少時間才能到 達一個安全的格子。安全

Input

* 第1行: 1個正整數:M * 第2..M+1行: 第i+1行爲3個用空格隔開的整數:X_i,Y_i,以及T_ispa

Output

輸出1個整數,即芙蓉哥哥逃生所花的最少時間。若是芙蓉哥哥不管如何都沒法在流星雨中存活下來,輸出-1code

Sample Input

4
0 0 2
2 1 2
1 1 2
0 3 5
輸入說明:
一共有4顆流星將墜落在霸中,它們落地點的座標分別是(0, 0),(2, 1),(1, 1)
以及(0, 3),時刻分別爲2,2,2,5。


t = 0 t = 2 t = 5
5|. . . . . . . 5|. . . . . . . 5|. . . . . . .
4|. . . . . . . 4|. . . . . . . 4|# . . . . . .
* = 流星落點
3|. . . . . . . 3|. . . . . . . 3|* # . . . . .
2|. . . . . . . 2|. # # . . . . 2|# # # . . . .
# = 行走禁區
1|. . . . . . . 1|# * * # . . . 1|# # # # . . .
0|B . . . . . . 0|* # # . . . . 0|# # # . . . .
-------------- -------------- --------------
0 1 2 3 4 5 6 0 1 2 3 4 5 6 0 1 2 3 4 5 6


Sample Output

5
輸出說明:
若是咱們觀察在t=5時的霸中,能夠發現離芙蓉哥哥最近的安全的格子是
(3,0)——不過因爲早在第二顆流星落地時,芙蓉哥哥直接跑去(3,0)的路線就被封死了。
離芙蓉哥哥第二近的安全格子爲(4,0),但它的狀況也跟(3,0)同樣。再接下來的格子就是在
(0,5)-(5,0)這條直線上。在這些格子中,(0,5),(1,4)以及(2,3)都能在5個單位時間內到達。

5|. . . . . . .
4|. . . . . . .
3|3 4 5 . . . . 某個合法的逃生方案中
2|2 . . . . . . 芙蓉哥哥每一個時刻所在地點
1|1 . . . . . .
0|0 . . . . . .
--------------
0 1 2 3 4 5 6

有一點點坑的廣搜blog

#include<cstdio>
#define maxn 310
using namespace std;
const int mx[4]={0,1,0,-1};
const int my[4]={1,0,-1,0};
bool map[maxn][maxn];
int flag[maxn][maxn];
int head[100000];
int n,m,t,w=1,ma;
struct stone{
	int x,y,t,next;
}dat[50010];
struct q{
	int x,y,t;
}queue[100000];
inline int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
inline bool mark(int x,int y)
{
	if (x<0||y<0) return 0;
	return 1;
}
inline void expl(int x,int y)
{
	map[x][y]=1;
	for (int i=0;i<4;i++)
	  if (mark(x+mx[i],y+my[i]))
	  {
		map[x+mx[i]][y+my[i]]=1;
	  }
}
inline void expl2(int x,int y)
{
	flag[x][y]=-1;
	for (int i=0;i<4;i++)
	  if (mark(x+mx[i],y+my[i])) flag[x+mx[i]][y+my[i]]=-1;
}
inline void bfs()
{
	queue[1].x=0;
	queue[1].y=0;
	queue[1].t=0;
	while (t<w)
	{
		int nx=queue[++t].x,ny=queue[t].y,nt=queue[t].t;
		if (nt!=queue[t-1].t)
		{
			for (int i=head[nt];i;i=dat[i].next)
			  expl2(dat[i].x,dat[i].y);
		}
		if (flag[nx][ny]!=0) continue;
		if (nt>ma) break;
		flag[nx][ny]=1;
		for (int i=0;i<4;i++)
		  {
		  	int wx=nx+mx[i],wy=ny+my[i];
		  	if (mark(wx,wy)&&flag[wx][wy]==0)
			{
				queue[++w].x=wx;
				queue[w].y=wy;
				queue[w].t=nt+1;
				if (!map[wx][wy]&&nt<ma) {printf("%d\n",nt+1);return;}
		  	}
		  }
	}
	printf("%d",-1);
}
int main()
{
	n=read();
	for (int i=1;i<=n;i++)
	{
		dat[i].x=read();
		dat[i].y=read();
		dat[i].t=read();
		dat[i].next=head[dat[i].t];
		head[dat[i].t]=i;
		if (dat[i].t>ma) ma=dat[i].t;
		expl(dat[i].x,dat[i].y);
	}
	bfs();
}
相關文章
相關標籤/搜索