zoj 1649 Rescue

BFS..第一次使用C++ STL的队列来写广搜。

 

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<algorithm>
using namespace std;
const int maxn = 222;
struct Point{ int time, x, y; };
queue<Point> Q;
char mapp[maxn][maxn];
int mt[maxn][maxn];
int dir[4][2] = { { -1, 0 }, { 1, 0 }, { 0, -1 }, { 0, 1 } };
int main()
{
    int n, m, i, j, sx, sy, ex, ey, ans;
    while (~scanf("%d%d", &n, &m))
    {
        for (i = 0; i < n; i++) scanf("%s", mapp[i]);
        for (i = 0; i < n; i++)
        {
            for (j = 0; j < m; j++)
            {
                mt[i][j] = 0x7FFFFFFF;
                if (mapp[i][j] == 'a') sx = i, sy = j;
                if (mapp[i][j] == 'r') ex = i, ey = j;
            }
        }    
        Point ss; ss.time = 0; ss.x = sx; ss.y = sy;
        Q.push(ss);
        ans = 0x7FFFFFFF;
        while (!Q.empty())
        {
            Point h, t;
            h = Q.front(); Q.pop();
            if (h.x == ex&&h.y == ey&&h.time < ans) ans = h.time;
            for (i = 0; i < 4; i++)
            {
                int xx = h.x + dir[i][0], yy = h.y + dir[i][1];
                if (xx >= 0 && xx <= n-1&&yy >= 0 && yy <= m-1)
                {
                    if (mapp[xx][yy] != '#')
                    {
                        if (mapp[xx][yy] == 'x')
                        {
                            if (h.time + 2 < mt[xx][yy])
                            {
                                t.time = h.time + 2;
                                t.x = xx; t.y = yy;
                                mt[xx][yy] = h.time + 2;
                                Q.push(t);
                            }
                        }
                        else
                        {
                            if (h.time + 1 < mt[xx][yy])
                            {
                                t.time = h.time + 1;
                                t.x = xx; t.y = yy;
                                mt[xx][yy] = h.time + 1;
                                Q.push(t);
                            }
                        }
                    }
                }
            }
        }
        if (ans != 0x7FFFFFFF)printf("%d\n", ans);
        else printf("Poor ANGEL has to stay in the prison all his life.\n");
    }
    return 0;
}

 

posted @ 2015-04-30 08:51  Fighting_Heart  阅读(158)  评论(0编辑  收藏  举报