Meteor shower (POJ-3669)

Source: Internet
Author: User

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&LT;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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.