uva11624-fire! (BFs two times)

Source: Internet
Author: User
Tags cas printf

Topic links


Test instructions: Your task is to help J out of a maze of fires spreading. J can move up or down in four directions per minute, and all the burning squares will spread around. There are some obstacles in the maze, J and fire can not enter. When J went to the boundary lattice of a maze, we thought he had gone out of the maze.

Idea: This is a problem above the white, in fact, as long as each lattice what time on fire to deal with it. Two times BFS, the first time to deal with lattice fire, the second BFS is to determine whether in the shortest possible time to go out.

Code:

#include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <

Algorithm> using namespace std;
const int MAXN = 1005;
const int INF = 0X3F3F3F3F;
const int dx[] = {-1, 0, 0, 1};

const int dy[] = {0,-1, 1, 0}; 
        struct node{node (int xx, int yy, int dd) {x = xx;
        y = yy;
    D = DD;
} int x, y, D;

};
Char G[MAXN][MAXN];
int VF[MAXN][MAXN], VM[MAXN][MAXN], T[MAXN][MAXN];

int R, c, SX, SY;

Queue<node> QF; 
        void Bfs_fire () {while (!qf.empty ()) {node u = qf.front (); 
        Qf.pop ();
        Node v = u; 
            for (int i = 0; i < 4; i++) {int tx = u.x + dx[i];      
            int ty = u.y + dy[i];
            if (TX < 0 | | TX >= R | | Ty < 0 | | ty >= c | | g[tx][ty] = = ' # ') continue; 
                if (!vf[tx][ty]) {Vf[tx][ty] = 1; 
                v.x = TX;
                v.y = Ty;
                V.D = u.d + 1; T[tx][tY] = V.D;
            Qf.push (v);
    }}}} int Bfs_man () {queue<node> QM; 
    while (!qm.empty ()) Qm.pop ();
    Node S (SX, SY, 0);
    Qm.push (s);
    memset (VM, 0, sizeof (VM));
    Vm[sx][sy] = 1; 
        while (!qm.empty ()) {node u = qm.front (); 
        Qm.pop ();
        if (u.x = = 0 | | u.x = = R-1 | | u.y = = 0 | | u.y = c-1) return U.D + 1;
        Node v = u; 
            for (int i = 0; i < 4; i++) {int tx = u.x + dx[i]; 
            int ty = u.y + dy[i];
            if (TX < 0 | | TX >= R | | Ty < 0 | | ty >= c | | g[tx][ty] = = ' # ') continue;
            if (u.d + 1 >= t[tx][ty]) continue; 
                if (!vm[tx][ty]) {Vm[tx][ty] = 1; 
                v.x = TX;
                v.y = Ty;
                V.D = u.d + 1;
            Qm.push (v);
}}} return-1; 
    } int main () {int cas;
    scanf ("%d", &cas); while (cas--) {scanf("%d%d", &r, &c);

        for (int i = 0; i < R; i++) scanf ("%s", G[i]);
        while (!qf.empty ()) Qf.pop ();
        memset (VF, 0, sizeof (VF));

        memset (t, INF, sizeof (t));
                    for (int i = 0, i < R; i++) {for (int j = 0; J < C; j + +) {if (g[i][j] = = ' J ') { 
                    SX = i; 
                sy = j;       
                    } if (g[i][j] = = ' F ') {node tmp (i, j, 0);
                    VF[I][J] = 1;
                    T[I][J] = 0;
                Qf.push (TMP);
        }}} bfs_fire ();    
        int ans = Bfs_man ();
        if (ans = =-1) printf ("impossible\n");
    else printf ("%d\n", ans);
} return 0; }


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.