poj 1649
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
不使用优先队列的TLE的深搜代码,留着以后反思。
Time Limit Exceeded | 1649 | C++ | 2001 | 760 |
#include
#include
#include
#include
#define maxn 205
using namespace std;
bool visit[maxn][maxn];
char map[maxn][maxn];
int dir[4][2] = {{0,-1},{0,1},{1,0},{-1,0}};
int q,p,ans,sx,sy;
int way[maxn][maxn];
int qx[maxn*maxn],qy[maxn*maxn];
void dfs(int x,int y)
{
int i,j,nx,ny;
if(map[x][y]=='a')
{
ans=min(ans, way[x][y]);
return;
}
for(i=0;i<4;i++)
{
nx=x+dir[i][0];
ny=y+dir[i][1];
if(nx>=0&&nx=0&&ny>map[i][j];
if(map[i][j]=='r')
{
sx=i;sy=j;
}
}
memset(visit,0,sizeof(visit));
memset(way,0,sizeof(way));
visit[sx][sy]=1;
ans=1000000000;
dfs(sx,sy);
if(ans==1000000000)
printf("Poor ANGEL has to stay in the prison all his life.\n");
printf("%d\n",ans);
}
return 0;
}
使用优先队列的AC的代码:
Accepted | 1649 | C++ | 10 | 228 |
#include
#include
#include
#include
using namespace std;
class Node
{
public :
int x, y, time;
friend bool operator < (const Node &a, const Node &b)
{
return a.time > b.time;
}
};
priority_queue Q;
Node first, next;
char map[201][201];
int n, m, a, b;
int dir[4][2] = {{0,1},{0,-1},{1,0},{-1,0}};
int bfs()
{
first.x = a;
first.y = b;
first.time = 0;
Q.push(first);
int fx, fy;
while (!Q.empty())
{
first = Q.top();
Q.pop();
int i;
for (i = 0; i < 4; ++i)
{
fx = first.x + dir[i][0];
fy = first.y + dir[i][1];
if (fx >= 0 && fx < n && fy >= 0 && fy < m && map[fx][fy] != '#')
{
if (map[fx][fy] == 'r')
{
return first.time + 1;
}
next.x = fx;
next.y = fy;
if (map[fx][fy] == '.')
{
map[fx][fy] = '#';
next.time = first.time + 1;
Q.push(next);
}
else if (map[fx][fy] == 'x')
{
map[fx][fy] = '#';
next.time = first.time + 2;
Q.push(next);
}
}
}
}
return -1;
}
int main()
{
int cnt;
while (scanf("%d %d", &n, &m) != EOF)
{
int i, j;
for (i = 0; i < n; ++i)
{
getchar();
for (j = 0; j < m; ++j)
{
scanf("%c", &map[i][j]);
if (map[i][j] == 'a')
{
map[i][j] = '#';
a = i;
b = j;
}
}
}
while (!Q.empty())
{
Q.pop();
}
cnt = bfs();
if (cnt == -1)
{
printf("Poor ANGEL has to stay in the prison all his life.\n");
}
else
{
printf("%d\n", cnt);
}
}
return 0;
}