POJ 3669 Meteor Shower (BFS+預處理)
Description
Bessie hears that an extraordinary meteor shower is coming; reports say that these meteors will crash into earth and destroy anything they hit. Anxious for her safety, she vows to find her way to a safe location (one that is never destroyed by a meteor) . She is currently grazing at the origin in the coordinate plane and wants to move to a new, safer location while avoiding being destroyed by meteors along her way.
The reports say that M meteors (1 ≤ M ≤ 50,000) will strike, with meteor i will striking point (Xi, Yi) (0 ≤ Xi ≤ 300; 0 ≤ Yi ≤ 300) at time Ti (0 ≤ Ti ≤ 1,000). Each meteor destroys the point that it strikes and also the four rectilinearly adjacent lattice points.
Bessie leaves the origin at time 0 and can travel in the first quadrant and parallel to the axes at the rate of one distance unit per second to any of the (often 4) adjacent rectilinear points that are not yet destroyed by a meteor. She cannot be located on a point at any time greater than or equal to the time it is destroyed).
Determine the minimum time it takes Bessie to get to a safe place.
Input
* Line 1: A single integer: M
* Lines 2..M+1: Line i+1 contains three space-separated integers: Xi, Yi, and Ti
Output
* Line 1: The minimum time it takes Bessie to get to a safe place or -1 if it is impossible.
Sample
Sample Input 4 0 0 2 2 1 2 1 1 2 0 3 5 Sample Output 5
題意:
巨大流星雨即將襲來。每個流星會對擊中的地方以及周圍(上下左右四格)造成破壞。Bessie開始時位於(0, 0)位置,並希望逃到一處不會被襲擊到的地方。已知每移動一格需要1個時間單位,被流星破壞後的地方不能再進入。給出M個流星在T時刻擊中的地方(X, Y),問Bessie能否逃到安全的地方,若能輸出最短時間,否則輸出-1。
思路:
首先根據題意自己構建迷宮,將map數組初始化為MAX,表示這個格子被襲擊的時間為INF(即永遠不會被襲擊)。對於每一個流星,將其影響反映到map上,如果破壞範圍由重疊,那麽格子顯示的是較早的破壞時間,下一步用BFS搜索。
代碼:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <queue> #define MAX 0x3f3f3f3f using namespace std; int map[305][305];//存節點信息 int vis[305][305];//標記數組 int dir[4][2]= {-1,0, 1,0, 0,1, 0,-1};//上下左右四個方向 int end; struct node { int x,y;//兩點表示節點位置 int time; } start;//入隊列使用 queue<node> q;//隊列,自己維護用來存儲節點信息 int bfs(int x,int y) { memset(vis,0,sizeof(vis)); start.x=x,start.y=y,start.time=0;//將傳遞過來的0.0節點放入結構體 vis[x][y]=1;//標記為已搜過 q.push(start);//入隊列 while(!q.empty()) { node now=q.front();//取隊頭元素 q.pop(); if(map[now.x][now.y]==MAX) { return now.time;//如果符合條件,返回;根據題意自己寫符合的條件。 } for(int i=0; i<4; i++)//四個方向入隊列 { start.x=now.x+dir[i][0],start.y=now.y+dir[i][1];//將第一個方向的入隊列 start.time=now.time+1; if(start.x>=0&&start.y>=0&&vis[start.x][start.y]==0&&start.time<map[start.x][start.y])//判斷是否越界 { vis[start.x][start.y]=1; q.push(start); } } } return -1; } int main() { int n; while(~scanf("%d",&n)) { memset(map,MAX,sizeof(map)); for(int j=0; j<n; j++) { int x,y,time; scanf("%d%d%d",&x,&y,&time); if(map[x][y]>time) map[x][y]=time; for(int i=0; i<4; i++)//自己建圖過程,一般不需要自己建圖 { int cx,cy; cx=x+dir[i][0],cy=y+dir[i][1]; if(cx>=0&&cy>=0) if(map[cx][cy]>time) map[cx][cy]=time; } } int ans=bfs(0,0);//從00點開始廣搜,根據題目要求具體定 cout<<ans<<endl; } }
POJ 3669 Meteor Shower (BFS+預處理)