Long time no brush problem, unfamiliar.
Test Instructions Analysis:
Test instructions understood that in a two-dimensional forward axis, a point (a meteor) along with its top and bottom four points will be destroyed at some point. A person at the origin, ask her the minimum time to reach the safe area is how much.
Code ideas:
Search from the origin, if the current point is safe (will not be destroyed), then it is over. Otherwise, search in four directions, if the point in that direction has not been searched, and the time to arrive at that point is less than the minimum time that is destroyed, then the point is considered to be reachable. Record the minimum time to reach the point and queue the point.
The previous processing of the search is this. Set each point to be secure (never destroyed), and the point is corrupted at the time of the INF. When reading the data, for each input, we deal with five points (the point of the input coordinates and the four points up and down): The point is destroyed, the minimum time to take the point to be destroyed each time.
It is important to note that although the input coordinate range x, y is [0,300], the coordinates of the security point may be greater than 300 and the maximum may be 302. I was so wrong once.
Personal code:
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <queue>5#include <algorithm>6 using namespacestd;7 8 Const intN =305, inf=1010;9 structnodeTen { One intx, y; A BOOLF//will it be destroyed ? - intT//minimum time to be destroyed - intS//the minimum time it takes to get here. the }p[n][n]; - BOOLVis[n][n]; - intStep; - BOOLIsinintXinty) + { - returnx<=n&&x>=0&&y<=n&&y>=0; + } A intdx[5]={0,0,0,1,-1}; at intdy[5]={0,1,-1,0,0}; -Queue<node>Q; - node p1,p2; - voidBFS () - { - inti,j,k; in while(!q.empty ()) - { top1=Q.front (); + Q.pop (); - //printf ("%d%d%d\n", p1.x,p1.y,p1.s); the if(p1.f==0) {step=p1.s; Break;} * for(k=1;k<5; k++) $ {Panax Notoginsengi=p1.x+Dx[k]; -j=p1.y+Dy[k]; the if(!vis[i][j]&&Isin (i,j)) + { A if(p1.s+1<p[i][j].t) the { +p[i][j].s=p1.s+1; - Q.push (P[i][j]); $vis[i][j]=1; $ } - } - } the } - }Wuyi intMain () the { - //freopen ("Test.txt", "R", stdin); Wu intm,i,x,y,t,j; - while(SCANF ("%d", &m)! =EOF) About { $ for(i=0; i<n;i++){ - for(j=0; j<n;j++){ -p[i][j].x=i; -p[i][j].y=J; Ap[i][j].f=0; +p[i][j].t=INF; the } - } $ for(i=0; i<m;i++){ thescanf"%d%d%d",&x,&y,&t); the for(j=0;j<5; j + +) the { the inta=x+dx[j],b=y+Dy[j]; - if(Isin (A, b)) in { thep[a][b].f=1; thep[a][b].t=min (p[a][b].t,t); About } the } the } the while(!q.empty ()) Q.pop (); +p[0][0].s=0; -Q.push (p[0][0]); thevis[0][0]=1;Bayistep=-1; thememset (Vis,0,sizeof(Vis)); the BFS (); -printf"%d\n", step); - } the return 0; the}
View Code
poj3669 Wide Search