Rescue
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 23185 Accepted Submission(s): 8196
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.)
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.
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
分别用了DFS和BFS。
DFS是递归的,肯定很耗时,大概有180ms,BFS大概20ms,以为BFS用到了队列,当判断时间超过前面的时间的时候,就不入列,反之入列,从而减少了递归的每个都要考虑的情况,
BFS
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <queue>
using namespace std;
struct node
{
int x,y,time;
friend bool operator < (node n1,node n2)
{
return n2.time < n1.time;
}
};
int n,m,t;
char s[201][201];
int vis[201][201];
int mov[4][2] = {1,0,-1,0,0,1,0,-1};
int ex,ey,sx,sy;
int flag;
inline int judge(int x,int y)
{
if(x<1 || x>n || y<1 || y>m || !vis[x][y] || s[x][y] == '#')
return 1;
return 0;
}
int dfs1()
{
int i;
priority_queue<node> Q;
node a,next;
a.x = sx;
a.y = sy;
a.time = 0;
Q.push(a);
vis[sx][sy] = 0;
while(!Q.empty())
{
a = Q.top();
Q.pop();
if(a.x == ex && a.y == ey)
return a.time;
for(i=0;i<4;i++)
{
next = a;
next.x += mov[i][0];
next.y += mov[i][1];
if(judge(next.x,next.y))
continue;
next.time++;
if(s[next.x][next.y] == 'x')
next.time++;
if(vis[next.x][next.y] > next.time)
{
vis[next.x][next.y] = next.time;
Q.push(next);
}
}
}
}
int main()
{
int i,j,k;
while(~scanf("%d%d%*c",&n,&m))
{
memset(vis,1,sizeof(vis));
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
scanf("%c",&s[i][j]);
if(s[i][j] == 'r')
{
ex = i;
ey = j;
}
if(s[i][j] == 'a')
{
sx = i;
sy = j;
}
}
getchar();
}
k=0;
k = dfs1();
printf(k?"%d\n":"Poor ANGEL has to stay in the prison all his life.\n",k);
}
}
DFS
#include <stdio.h>
#include <string.h>
#include <math.h>
int n,m,t;
char s[201][201];
int vis[201][201];
int ex,ey,sx,sy;
int flag;
void dfs1(int x,int y,int time)
{
if(x < 1 || x > n || y < 1 || y > m || s[x][y] == '#' || vis[x][y] )
{
return ;
}
if(s[x][y] == 'x')
{
time++;
}
if(x == ex && y == ey)
{
t = time < t ? time : t;
flag = 1;
return ;
}
vis[x][y] = 1;
dfs1(x-1,y,time+1);
dfs1(x+1,y,time+1);
dfs1(x,y-1,time+1);
dfs1(x,y+1,time+1);
vis[x][y] = 0;
}
int main()
{
int i,j,k;
while(~scanf("%d%d%*c",&n,&m))
{
memset(vis,0,sizeof(vis));
flag = 0;
t = 0x7fffffff;
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
scanf("%c",&s[i][j]);
if(s[i][j] == 'r')
{
ex = i;
ey = j;
}
if(s[i][j] == 'a')
{
sx = i;
sy = j;
}
}
getchar();
}
dfs1(sx,sy,0);
printf(flag?"%d\n":"Poor ANGEL has to stay in the prison all his life.\n",t);
}
}