题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1242
Rescue
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 32113 Accepted Submission(s): 11220
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
本题的意思:天使(Angel)被抓,他的朋友(a,注意:a不止一个)要找到他。但是监狱里有墙(#)、侍卫(x)、路(.)。墙不能通过、每走一个‘.‘’用时1,杀死侍卫通过用时2,。请编写程序:求出天使的朋友最短用多长时间能找到天使。如果能找到,输出最短时间;如果不能找到,输出“oor ANGEL has to stay in the prison all his life.”
需要注意的是:本题 天使的朋友 不止一个,所以改变思维:由天使去找他的朋又。
以下为代码,希望能够帮助观者。
代码:
#include<iostream>
using namespace std;
char ch[205][205];
int visit[205][205]; //用于标记天使的朋友走过的路
int minlen,m,n,len; //minlen用于存储最短时间,len用于存储走不同的路所用时间
void dfs(int x,int y,int len){
if(len>minlen) //如果这条路的用时已超过最短时间,则返回上一层
return;
if(ch[x][y]=='#') //如果遇到墙,返回上一层
return;
if(visit[x][y]==1) //如果这条路走过,则返回上一层
return;
if(x<0||y<0||x>=m||y>=n) //如果越界,则返回上一层
return;
if(ch[x][y]=='r'){ //如果找到天使,则走本条路所用时间与之前的最短时间比较,保证minlen所存储的是最短时间
if(len<minlen)
minlen=len;
return;
}
if(ch[x][y]=='x') //如果遇到侍卫,则时间另加1
len++;
visit[x][y]=1; //走过的路标记为1
dfs(x-1,y,len+1); //搜索上边
dfs(x,y+1,len+1); //搜索右边
dfs(x+1,y,len+1); //搜索下边
dfs(x,y-1,len+1); //搜索左边
visit[x][y]=0;
}
int main(){
int ax,ay;
while(cin>>m>>n){
for(int i=0;i<=m-1;i++){
for(int j=0;j<=n-1;j++){
cin>>ch[i][j];
if(ch[i][j]=='a'){ //ax,ay用于标记天使的位置
ax=i;
ay=j;
}
}
}
minlen=205*205; //把minlen赋值为最大时间
len=0;
dfs(ax,ay,len);
if(minlen==205*205) //如果minlen的值没有改变,说明不存在找到天使的路
cout<<"Poor ANGEL has to stay in the prison all his life."<<endl;
else
cout<<minlen<<endl;
}
return 0;
}