您的位置:首页 > 其它

ZOJ 1649 BFS

2016-05-12 21:48 459 查看
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 2E2 + 10;
const int dir[4][2] = {{0, 1}, {0, -1}, {1, 0}, { -1, 0}};
struct Node
{
int x, y;
int step;
bool operator<(const Node &oth)const
{
return step > oth.step;
}
};
Node cur, Next;
char mp[maxn][maxn];
int n, m, Arrival[maxn][maxn], Startx, Starty;
int BFS()
{
priority_queue<Node>Q;
cur.x = Startx; cur.y = Starty; cur.step = 0;
Arrival[Startx][Starty] = 0;
Q.push(cur);
while (!Q.empty())
{
cur = Q.top(); Q.pop();
if (mp[cur.x][cur.y] == 'a') return cur.step;
for (int i = 0; i < 4; i++)
{
Next.x = cur.x + dir[i][0];
Next.y = cur.y + dir[i][1];
Next.step = cur.step + 1;
if (mp[Next.x][Next.y] == 'x') Next.step++;
if (mp[Next.x][Next.y] != '#' && Next.step < Arrival[Next.x][Next.y])
{
Arrival[Next.x][Next.y] = Next.step;
Q.push(Next);
}
}
}
return 0;
}
int main(int argc, char const *argv[])
{
while (~scanf("%d%d", &n, &m))
{
for (int i = 0; i <= m + 1; i++)
mp[0][i] = mp[n + 1][i] = '#';
for (int i = 1; i <= n; i++)
{
scanf("%s", mp[i] + 1);
mp[i][0] = mp[i][m + 1] = '#';
for (int j = 1; j <= m; j++)
if (mp[i][j] == 'r') Startx = i, Starty = j;
}
memset(Arrival, INF, sizeof(Arrival));
int ans = BFS();
if (ans) printf("%d\n", ans);
else printf("Poor ANGEL has to stay in the prison all his life.\n");
}
return 0;
}


这题简直有鬼。。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: