The input part of the problem, write a wrong symbol, the result of various WA, really should not, since write once should be careful, strive to succeed.
Idea: Use the VIS array to record the most recent time each point was bombed by a bomb. Records the current time with the D array. When D<vis, this road can go, then the VIS initialization-1, when encountered Vis=-1, that is, the security zone.
Of course, the array should be slightly larger because the bombs will spread around.
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include < Queue> #include <cmath>using namespace std; int m; int d[305][305]; int vis[305][305]; int dx[] = {1,0,-1,0,0}; int dy[] = {0,1,0,-1,0}; struct PA {int x, y; PA (int x=0,int y=0): X (x), Y (y) {}}; int BFs () {queue<pa> q; Q.push (PA (0,0)); Memset (d,-1,sizeof (d)); D[0][0] = 0; if (Vis[0][0]==-1) return 0; if (vis[0][0]==0) return-1; while (!q.empty ()) {pa u = q.front (); Q.pop (); for (int i=0;i<5;i++) {PA v; v.x = U.x+dx[i]; V.Y = U.y+dy[i]; bool OK = false; if (v.x<0| | V.Y<0) continue; if (d[v.x][v.y]==-1) {D[V.X][V.Y] = D[u.x][u.y] + 1; ok =true;} if (vis[v.x][v.y]==-1) {return D[V.X][V.Y]; } if (Ok& &vis[v.x][v.y]>d[v.x][v.y]) Q.push (v); }} return-1; } int main () {scanf ("%d", &m); memset (vis,-1,sizeof (VIS)); int x,y,t; for (int i=0;i<m;i++) {scanf ("%d%d%d", &x,&y,&t); if (vis[x][y]==-1) vis[x][y] = t; else if (vis[x][y]>t) vis[x][y] = t; if (x>0) {if (vis[x-1][y]==-1) vis[x-1][y] = t; else if (vis[x-1][y]>t) vis[x-1][y] = t; } if (y>0) {if (vis[x][y-1]==-1) vis[x][y-1] = t; else if (vis[x][y-1]>t) vis[x][y-1] = t; } if (vis[x+1][y]==-1) vis[x+1][y] = t; else if (vis[x+1][y]>t) vis[x+1][y] = t; if (vis[x][y+1]==-1) vis[x][y+1] = t; else if (vis[x][y+1]>t) vis[x][y+1] = t; } printf ("%d\n", BFS ()); return 0; }
Meteor shower (POJ-3669)