Http://poj.org/problem? Id = 3669
Question:
Given several coordinates, at the T moment, there will be a meteorite rain, and the upper and lower sides will also be damaged. How to find a safe place in the shortest time.
Train of Thought: pre-process the points around the whereabouts of each meteorite, and then BFS can.
1 #include <cstdio> 2 #include <cstring> 3 #include <queue> 4 #include <algorithm> 5 #define maxn 50000 6 using namespace std; 7 8 int n; 9 int ans,max1;10 int vis[400][400];11 int dir[5][2]={{0,0},{0,1},{0,-1},{1,0},{-1,0}};12 struct node13 {14 int x,y;15 int t;16 }p[maxn],st1,st2;17 18 void bfs(int x,int y)19 {20 queue<node>q;21 st1.x=x;22 st1.y=y;23 st1.t=0;24 if(vis[x][y]==0)25 {26 return;27 }28 q.push(st1);29 while(!q.empty())30 {31 st2=q.front(); q.pop();32 if(vis[st2.x][st2.y]==-1)33 {34 ans=st2.t;35 return ;36 }37 for(int i=0; i<5; i++)38 {39 int x1=st2.x+dir[i][0];40 int y1=st2.y+dir[i][1];41 if(!(x1>=0&&y1>=0)) continue;42 if((vis[x1][y1]>st2.t+1)||(vis[x1][y1]==-1))43 {44 if(vis[x1][y1]!=-1)45 {46 vis[x1][y1]=st2.t+1;47 }48 st1.x=x1;st1.y=y1;49 st1.t=st2.t+1;50 q.push(st1);51 }52 }53 }54 }55 56 int main()57 {58 while(scanf("%d",&n)!=EOF)59 {60 ans=-1;61 memset(vis,-1,sizeof(vis));62 for(int i=1; i<=n; i++)63 {64 scanf("%d%d%d",&p[i].x,&p[i].y,&p[i].t);65 for(int j=0; j<5; j++)66 {67 int xx=p[i].x+dir[j][0];68 int yy=p[i].y+dir[j][1];69 if(!(xx>=0&&yy>=0)) continue;70 if(vis[xx][yy]==-1)71 {72 vis[xx][yy]=p[i].t;73 }74 else75 {76 vis[xx][yy]=min(vis[xx][yy],p[i].t);77 }78 }79 }80 bfs(0,0);81 printf("%d\n",ans);82 }83 return 0;84 }
View code
Poj 3669 meteor shower