TOJ-1335-营救天使
此题曾让我无限放在此处纪念下。
营救天使
Time Limit(Common/Java):1000MS/10000MS Memory Limit:65536KByte
Total Submit: 236 Accepted: 56
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, "x" stands for a guard, 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)。r每走一步耗费一个单位的时间,如果路途遇上x,杀死x则需要一个单位的时间,求r找到a的最短时间。如果找不到就输出"Poor ANGEL has to stay in the prison all his life."
解题思路
此题是一个迷宫问题,求解最短路径。使用广度优先搜索可解,但是要注意个细节,就是搜索的顺序带来的一系列问题!
请看这组数据:
3 3
#.r
#.x
#a#
如果宽搜是按照上下左右顺序进行的,那么队列中首先会加入(1,2),(0,1).接着以(1,2)为起点,进行宽搜,首先让时间+1,因为此次有守卫,然后向周围搜索,向队列中加入(1,1),并将其标记为“已访问”....
如果是这样的搜索,那么最短时间将会是4而不是3!因为(1,1)先被从x过来的路径标记为以访问,那么(0,1)点过来的路径将不能访问到(1,1)!导致最短路变成了4而不是3.
同理,还有这组数据:
4 3
..r
.#x
.#x
.ax
如果结果是7,也需要注意了。
此题还有个漏洞,就是题目虽说有多个(for each of angle's friend)朋友(起点),但测试数据只有一个朋友(起点)!当然,如果有个多个朋友的话,也不难,只有多次调用bfs求取最小的那个。
测试数据
7 8
#.#####.
#.a#..r.
#..#x...
..#..#.#
#...##..
.#......
........
13
7 8
#.#####.
#.a#..r.
#..#x...
..#..#.#
##..##..
.#......
........
Poor ANGEL has to stay in the prison all his life.
2 8
#.#####.
#.a#..r.
Poor ANGEL has to stay in the prison all his life.
4 8
#.#####.
#.a#..r.
#..#x...
.....#.#
9
3 3
#.r
#.x
#a#
3
3 3
#xr
#..
#a#
3
3 3
..r
.#x
.a.
4
4 3
..r
.#x
.#x
.ax
6
源代码g++-15ms
1.true false标记法
#include <cstdio>
#include <queue>
using namespace std;
const int direction[4][2]={{-1,0},{1,0},{0,-1},{0,1} };//四个方向
int row,col;//行、列
int startR,startC;//起始点行列
char maze[200][201];//迷宫
bool visit[200][201];//标记
int bfs()//广度优先搜索
{
queue<int> open;
open.push(startR);//压入行
open.push(startC);//压入列
open.push(0);//压入时间
visit[startR][startC]=true;
while(!open.empty())
{
int r=open.front();
open.pop();
int c=open.front();
open.pop();
int t=open.front();
open.pop();
if(maze[r][c]=='a')//找到终点
{
return t;
}
else if(maze[r][c]=='x')//遇到守卫
{
//增加1个单位的战斗时间
maze[r][c]='.';
open.push(r);
open.push(c);
open.push(t+1);
continue;//因为需要一个战斗时间,
//故要把搜索步数延迟一步,让其他更优的搜索先搜,以防出错。
}
for(int d=0;d<4;++d)
{
int dr=r+direction[d][0];
int dc=c+direction[d][1];
if(dr>=0 && dr<row && dc>=0 && dc<col)
{
if(maze[dr][dc]!='#' && !visit[dr][dc])
{
visit[dr][dc]=true;
open.push(dr);
open.push(dc);
open.push(t+1);
}
}
}
}
return -1;
}
int main()
{
while(scanf("%d%d",&row,&col)!=EOF)
{
for(int i=0;i<row;++i)
{
scanf("%s",maze[i]);
for(int k=0;k<col;++k)
{
if(maze[i][k]=='r')//找起点
{
startR=i;
startC=k;
}
visit[i][k]=false;
}
}
int t=bfs();
if(t==-1)printf("Poor ANGEL has to stay in the prison all his life./n");
else printf("%d/n",t);
}
return 0;
}
2.更改步数法
#include <cstdio>
#include <queue>
using namespace std;
const int INFINITY=1000000000;//无穷大
const int direction[4][2]={{-1,0},{1,0},{0,-1},{0,1} };//四个方向
int row,col;//行、列
int startR,startC;//起始点行列
int destR,destC;
char maze[200][201];//迷宫
int mark[200][201];//标记
int bfs()//广度优先搜索
{
queue<int> open;
open.push(startR);
open.push(startC);
mark[startR][startC]=0;
while(!open.empty())
{
int r=open.front();
open.pop();
int c=open.front();
open.pop();
// if(maze[r][c]=='a')//不能加上这几句,因为先到达终点的不一定是最优的!
// {
// return mark[r][c];
// }
for(int d=0;d<4;++d)
{
int dr=r+direction[d][0];
int dc=c+direction[d][1];
if(dr>=0 && dr<row && dc>=0 && dc<col)
{
if(maze[dr][dc]=='x' && mark[r][c]+2<mark[dr][dc])//遇到守卫
{
//增加1个单位的战斗时间
mark[dr][dc]=mark[r][c]+2;
open.push(dr);
open.push(dc);
}
else if(maze[dr][dc]!='x'
&&maze[dr][dc]!='#'
&& mark[r][c]+1<mark[dr][dc])
{
mark[dr][dc]=mark[r][c]+1;
open.push(dr);
open.push(dc);
}
}
}
}
return mark[destR][destC];
}
int main()
{
while(scanf("%d%d",&row,&col)!=EOF)
{
for(int i=0;i<row;++i)
{
scanf("%s",maze[i]);
for(int k=0;k<col;++k)
{
if(maze[i][k]=='r')//找起点
{
startR=i;
startC=k;
}
else if(maze[i][k]=='a')
{
destR=i;
destC=k;
}
mark[i][k]=INFINITY;
}
}
int t=bfs();
if(t==INFINITY)printf("Poor ANGEL has to stay in the prison all his life./n");
else printf("%d/n",t);
}
return 0;
}