ZOJ 1649 Rescue (with enemy maze BFS)

Source: Internet
Author: User

Test instructions The minimum time required from the position of a to the position of R in the maze to go through '. ' squares need 1s through ' x ' squares need two seconds ' # ' to indicate wall

Because of the 1s and 2s two cases need to add some judgment on the base maze BFS

The time at which each point is reached is initially infinite when it takes more time to reach that point from one point to the point of time than his original time, and the point is scanned for a complete picture to get an answer.

#include <cstdio> #include <cstring> #include <queue> using namespace std;
const int N = 205;
Char Mat[n][n];
int Time[n][n], SX, SY;
int dx[4] = {0, 0,-1, 1};
int dy[4] = {-1, 1, 0, 0};
    struct Grid {int x, y;

Grid (int xx = 0, int yy = 0): X (xx), Y (yy) {}};
    void BFs () {memset (time, 0x3f, sizeof (time));
    Time[sx][sy] = 0;
    Queue<grid> G;

    G.push (Grid (SX, SY));
        while (!g.empty ()) {Grid cur = G.front ();
        G.pop ();
        int cx = cur.x, cy = cur.y, CT = time[cx][cy];
            for (int i = 0; i < 4; ++i) {int NX = CX + dx[i], NY = cy + dy[i];
                if (Mat[nx][ny] && mat[nx][ny]! = ' # ') {int tt = CT + 1;
                if (mat[cx][cy] = = ' x ') ++tt;
                    if (TT < Time[nx][ny]) {Time[nx][ny] = TT;
                G.push (Grid (NX, NY)); }}}}} int main () {inT N, M, ex, EY;
        while (~SCANF ("%d%d", &n, &m)) {memset (mat, 0, sizeof (MAT));
        for (int i = 1; I <= n; ++i) scanf ("%s", Mat[i] + 1); for (int i = 1; l <= N; ++i) for (int j = 1; j <= m; ++j) if (mat[i][j] = = ' a ') SX = i, sy
                = J;
        else if (mat[i][j] = = ' r ') ex = i, ey = j;
        BFS ();
        if (Time[ex][ey] = time[0][0]) printf ("%d\n", Time[ex][ey]);
    else printf ("Poor ANGEL have to stay in the prison all his life.\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.