Problem Description
Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison.
Angel's friends want to save Angel. Their task is: approach Angel. We assume that "approach Angel" is to get to the position where Angel stays. When there's a guard in the grid, we must kill him (or her?) to move into the grid. We assume that we moving up, down, right, left takes us 1 unit time, and killing a guard takes 1 unit time, too. And we are strong enough to kill all the guards.
You have to calculate the minimal time to approach Angel. (We can move only UP, DOWN, LEFT and RIGHT, to the neighbor grid within bound, of course.)
Input
First line contains two integers stand for N and M.
Then N lines follows, every line has M characters. "." stands for road, "a" stands for Angel, and "r" stands for each of Angel's friend.
Process to the end of the file.
Output
For each test case, your program should output a single integer, standing for the minimal time needed. If such a number does no exist, you should output a line containing "Poor ANGEL has to stay in the prison all his life."
Sample Input
7 8
#.#####.
#.a#..r.
#..#x...
..#..#.#
#...##..
.#......
........
Sample Output
13
题目的意思是a是起点,要求最短到r的时间,#是路障,x是守卫,可以选着杀掉守卫,但是
会消耗掉一个单位时间,可以用dfs寻找最短时间的路径,也可以用BFS,但是要用到优先队列
(普通队列WA了)因为杀掉守卫会消耗一秒,但是取最短的时间,所以时间少的排在前
DFS
#include<iostream>
#include<string.h>
const int INF = 0x3f3f3f3f;
using namespace std;
char map[1010][1010];
int vis[1010][1010];
int sx, sy,ans=INF;
int dir[4][2] = { 0, 1, 0, -1, 1, 0, -1, 0 };
int n, m;
void dfs(int x, int y, int t){
if (map[x][y] == 'r')
{ //遇到目的地
if (t < ans)
ans = t;
return;
}
for (int i = 0; i < 4; i++)
{
int xx = x + dir[i][0];
int yy = y + dir[i][1];
int tt = t + 1;
if (xx < n&&xx >= 0 && yy < m&&yy >= 0 && !vis[xx][yy] && map[xx][yy] != '#')//检查坐标是否合法
{
if (map[x][y] == 'x')tt++;//遇到守卫
vis[xx][yy] = 1, dfs(xx, yy,tt), vis[xx][yy] = 0;
}
}
}
int main(){
while (cin >> n >> m){
ans = INF;
memset(vis, 0, sizeof(vis));
for (int i = 0; i < n; i++)
cin >> map[i];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (map[i][j] == 'a')
{
sx = i, sy = j;
break;
}
vis[sx][sy] = 1;
dfs(sx, sy, 0);
if (ans==INF)
cout << "Poor ANGEL has to stay in the prison all his life." << endl;
else
cout << ans<< endl;
}
return 0;
}
BFS
#include<iostream>
#include<stdio.h>
#include<queue>
using namespace std;
char map[210][210];
int vis[210][210];
int n, m;
int dir[4][2] = { 0, 1, 0, -1, 1, 0, -1, 0 };
struct node{
int x, y, t;
bool operator < (const node a)const
{
return this->t>a.t;
}
};
int bfs(int x, int y, int t){
priority_queue<node>q;
q.push({ x, y, t });
while (!q.empty())
{
x = q.top().x, y = q.top().y, t = q.top().t;
q.pop();
if (vis[x][y])continue;
vis[x][y] = 1;
if (map[x][y] == 'r')
return t;
for (int i = 0; i < 4; i++)
{
int xx = x + dir[i][0];
int yy = y + dir[i][1];
int tt = t + 1;
if (xx >= 0 && xx < n&&yy >= 0 && yy < m&&map[xx][yy] != '#')
{
if (map[xx][yy] == 'x')
tt++;
q.push({ xx, yy, tt });
}
}
}
return -1;
}
int main(){
int bx, by;
while (cin >> n >> m)
{
memset(vis, 0, sizeof vis);
for (int i = 0; i < n; i++)
cin >> map[i];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (map[i][j] == 'a')
{
bx = i, by = j;
break;
}
int ans = bfs(bx, by, 0);
if (ans == -1)
cout << "Poor ANGEL has to stay in the prison all his life." << endl;
else
cout << ans << endl;
}
}