HDU 1242 Rescue dfs
2016-04-05 17:25
501 查看
Rescue
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)Total Submission(s) : 3 Accepted Submission(s) : 1
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
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
Author
CHEN, XueSource
ZOJ Monthly, October 2003ACcode:
#include <map>
#include <queue>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#define maxn 202
#define inf 0x3f3f
using namespace std;
char mapp[maxn][maxn];
bool vis[maxn][maxn];
int n,m,sx,sy,time=inf;
void dfs(int x,int y,int t){
if(x<=0||x>n||y<=0||y>m)return;
if(t>=time)return;
if(mapp[x][y]=='#')return;
if(vis[x][y])return;
if(mapp[x][y]=='r'){
time=time<t?time:t;
return ;
}
if(mapp[x][y]=='x')
t++;
vis[x][y]=1;
dfs(x+1,y,t+1);dfs(x-1,y,t+1);
dfs(x,y+1,t+1);dfs(x,y-1,t+1);
vis[x][y]=0;
}
int main(){
while(scanf("%d%d",&n,&m)!=EOF){
getchar();
for(int i=1;i<=n;++i){
for(int j=1;j<=m;++j){
mapp[i][j]=getchar();
if(mapp[i][j]=='a'){
sx=i;
sy=j;
}
}
getchar();
}
memset(vis,0,sizeof(vis));
time=inf;
dfs(sx,sy,0);
if(time==inf)
printf("Poor ANGEL has to stay in the prison all his life.\n" );
else
printf("%d\n",time);
}
return 0;
}
相关文章推荐
- [ios] dispatch_get_main_queue 的同步异步问题
- Meterial Design UI Widgets —— RecyclerView 循环视图
- Android4.4 Systemui状态栏状态图标更新流程分析
- LeetCode 63 - Unique Paths II
- UITableView的section header view悬停的方法
- Nexus设置guest只访问指定artifact
- UIDynamic 物理动效
- UISearchBar(搜索框)的方法属性介绍
- AndroidUI之登录界面的实现分析
- UIButton内文字自适应button长度
- Android4.4 Systemui状态栏之信号逻辑流程分析
- Android深入浅出系列之Bluetooth—蓝牙操作(一)
- Android深入浅出系列之Bluetooth—蓝牙操作(二)
- Android深入浅出系列之Bluetooth—蓝牙操作(一)
- 在Web工程中用JavaMail 发送邮件时报com.sun.mail.smtp.SMTPSendFailedException: 553 authentication is required原因
- setValuesForKeysWithDictionary奔溃问题
- LeetCode 62 - Unique Paths
- 10个顶级的CSS UI开源框架
- 黄聪:No 'Access-Control-Allow-Origin' header is present on the requested resource解决办法
- iOS 9 弹窗UIalertController