ZOJ 1649 Rescue(有敵人迷宮BFS),zojbfs

來源:互聯網
上載者:User

ZOJ 1649 Rescue(有敵人迷宮BFS),zojbfs

題意 求迷宮中從a的位置到r的位置需要的最少時間  經過'.'方格需要1s  經過‘x’方格需要兩秒  '#'表示牆

由於有1s和2s兩種情況  需要在基礎迷宮bfs上加些判斷

令到達每個點的時間初始為無窮大  當從一個點到達該點用的時間比他本來的時間小時  更新這個點的時間並將這個點入隊  掃描完全圖就得到答案咯

#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;}





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.