UVA 11624--fire!

Source: Internet
Author: User
Tags printf

Title: This is the topic

Test instructions: John works in the Maze, the maze is on fire, the fire spreads from four directions and asks John if he can escape.

Idea: Wide search, two wide search, one is fire, and the other is John. Here are a few points to note:

1. The position of the fire may have multiple, before WA in this. From "Unfortunately, portions of the maze havecaught on fire" can be learned.

2. John was at the exit (border), and later WA did not take this into account.

3. I was the fire and John Search at the same time, but first judge the fire will not burn, and then judge whether people want to go, so then the wrong way to take a person can walk the team first judgment is the boundary, but because I was the first judge of fire, Fire will put John in the export burned, so will judge to go out, that is

3 3

#JF

###

###

It was supposed to be 1, and I was impossible.

So on the outside, judge whether the starting point is the end point.

#include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <

Queue> #include <math.h> using namespace std;
const int MAX = 1005;
const int INF = 0X3F3F3F3F;
Char _map[max][max];
BOOL Visit[max][max];
int D[max][max];
int x[4] = {-1, 0, 1, 0};

int y[4] = {0, 1, 0,-1};
int co;
int R, C;
int SX, SY;
int num;
    struct Node {int xi, Yi;
int step;

}ff[max * Max], Jj[max * Max], F[max * Max];  BOOL OK (node en) {if (En.xi = = 0 | | en.xi = = R-1 | | en.yi = = 0 | | en.yi = c-1) &&!visit[en.xi][en.yi])
    {return true;
} return false;
    } bool BFs () {queue <node> q;
    node start;
    START.XI = SX;
    Start.yi = sy;
    Start.step = 1;
    D[sx][sy] = 1;
    Q.push (start);
    Queue <node> Fire;
    for (int i = 0; i < Co; i++) {Fire.push (f[i]);
        } while (!q.empty ()) {int K1 = 0;
     while (!fire.empty ()) {node fhead, ftail;       Fhead = Fire.front ();
            Fire.pop ();
                for (int i = 0; i < 4; i++) {int xx = fhead.xi + x[i];
                int yy = Fhead.yi + y[i];
                    if (xx >= 0 && xx < r && yy >= 0 && yy < c &&!visit[xx][yy]) {
                    Ftail.xi = XX;
                    Ftail.yi = yy;
                    VISIT[XX][YY] = true;
                ff[k1++] = Ftail;  }}} for (int i = 0; i < K1; i++) Fire.push (Ff[i]);//front 1s fire to point int K2
        = 0;
            while (!q.empty ()) {node Head, tail;
            Head = Q.front ();
Q.pop (); if (OK (head)) {//num = Head.step;//return true;//}//before WA is here, in the team
                First judgment, error for (int i = 0; i < 4; i++) {int xx = head.xi + x[i];
                int yy = Head.yi + y[i]; if (xx >= 0 && xx < R && yy >= 0 && yy < c &&!visit[xx][yy] && d[xx
                    ][YY] > head.step + 1) {tail.xi = XX;
                    Tail.yi = yy;
                    Tail.step = Head.step + 1;
                    D[XX][YY] = Tail.step;
                    jj[k2++] = tail;
                        if (OK (tail)) {num = Tail.step;
                    return true; }//each point to determine if it is an exit}} for (int i = 0; i < K2; i++) Q.push (Jj[i]
);//The point where John can go to before 1s} return false;
    } int main () {int t;
    scanf ("%d", &t);
        while (t--) {scanf ("%d%d", &r, &c);
        CO = 0;
        Memset (visit, false, sizeof (visit));
        for (int i = 0; i < R; i++) scanf ("%s", _map[i]); for (int i = 0, i < R; i++) {for (int j = 0; J < C; j + +) {D[i][j] = inF
                    if (_map[i][j] = = ' J ') {sx = i;
                sy = j;
                } else if (_map[i][j] = = ' # ') {visit[i][j] = true;
                    } else if (_map[i][j] = = ' F ') {//possibly multiple fire f[co].xi = i;
                    F[co].yi = j;
                    co++;
                VISIT[I][J] = true;
        }}} node start;
        START.XI = SX;
        Start.yi = sy;
        if (start) {printf ("1\n");
        }//determines whether the starting point is the export else if (BFS ()) {printf ("%d\n", num);
        } else {printf ("impossible\n");
}} 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.