ZOJ 1649 Rescue (with enemy maze BFS), zojbfs

Source: Internet
Author: User

ZOJ 1649 Rescue (with enemy maze BFS), zojbfs

The minimum time required to go from the position to the r position in the maze through the '.' square requires 1 s to go through the 'X' square. It takes two second' # 'to represent the wall.

In the case of 1 s and 2 s, you need to make some judgments on the base maze bfs.

The time to arrive at each point is initially infinite. When the time used to arrive at the point from a point is later than the original time hour, the system updates the time at the point and scans the complete graph to obtain the 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; i <= 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 has 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.