HDU Rescue
2016-04-08 18:31
330 查看
Rescue
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 11 Accepted Submission(s) : 3
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.)
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.
using namespace std;
#include<cstring>
#define M 205
char map[M][M];
int visit[M][M];
int sx,sy,d;
int m,n,minl;
void DFS(int x,int y,int l){
if(!(x>=0&&x<m&&y>=0&&y<n)) //限制在图内。
return ;
if(l>=minl) return; //减支。
if(map[x][y]=='#'||visit[x][y]==1) //不能通过。
return ;
if(map[x][y]=='x')
l++; //遇守卫长度加一。
if(map[x][y]=='r'){
if(l<minl) minl=l;
return ;
}
visit[x][y]=1; //当前层标记。
DFS(x,y+1,l+1);
DFS(x,y-1,l+1);
DFS(x-1,y,l+1);
DFS(x+1,y,l+1);
visit[x][y]=0; //对初始位置无用。对>=2位置广搜做准备。
}
int main(){
int i,j;
while(scanf("%d%d ",&m,&n)!=EOF){ //注意接收回车符。用一个空格即可。
memset(visit,0,sizeof(visit)); //初始化。
for(i=0;i<m;i++){
for(j=0;j<n;j++){
map[i][j]=getchar();
if(map[i][j]=='a'){
sx=i;
sy=j;
}
}
getchar(); //吸收回车符。或用字符串接收可不吸收。
}
minl=M; //最小值初始化。赋M。以寻找最小值。
DFS(sx,sy,0);//从0开始。l 即总长度。
if(minl!=M) printf("%d\n",minl);
else printf("Poor ANGEL has to stay in the prison all his life.\n" );
}
}
Rescue
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 11 Accepted Submission(s) : 3
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
#include<cstdio>using namespace std;
#include<cstring>
#define M 205
char map[M][M];
int visit[M][M];
int sx,sy,d;
int m,n,minl;
void DFS(int x,int y,int l){
if(!(x>=0&&x<m&&y>=0&&y<n)) //限制在图内。
return ;
if(l>=minl) return; //减支。
if(map[x][y]=='#'||visit[x][y]==1) //不能通过。
return ;
if(map[x][y]=='x')
l++; //遇守卫长度加一。
if(map[x][y]=='r'){
if(l<minl) minl=l;
return ;
}
visit[x][y]=1; //当前层标记。
DFS(x,y+1,l+1);
DFS(x,y-1,l+1);
DFS(x-1,y,l+1);
DFS(x+1,y,l+1);
visit[x][y]=0; //对初始位置无用。对>=2位置广搜做准备。
}
int main(){
int i,j;
while(scanf("%d%d ",&m,&n)!=EOF){ //注意接收回车符。用一个空格即可。
memset(visit,0,sizeof(visit)); //初始化。
for(i=0;i<m;i++){
for(j=0;j<n;j++){
map[i][j]=getchar();
if(map[i][j]=='a'){
sx=i;
sy=j;
}
}
getchar(); //吸收回车符。或用字符串接收可不吸收。
}
minl=M; //最小值初始化。赋M。以寻找最小值。
DFS(sx,sy,0);//从0开始。l 即总长度。
if(minl!=M) printf("%d\n",minl);
else printf("Poor ANGEL has to stay in the prison all his life.\n" );
}
}
相关文章推荐
- Quartz 2D中UIImage的分类
- Cause: com.mysql.jdbc.PacketTooBigException: Packet for query is too large (144900 > 1024)
- 关于java中几个CP的一点说明--stringBuilder和stringBuffer与hashTable和hashMap
- String、StringBuffer、StringBuilder简单理解
- Qt 示例学习--3 gallery quick controls
- UEditor1.4.3关于上传文件绝对路径的配置
- easyui 添加不重复的tab选项卡
- Virtual machine steal BlueTooth from Host(Mac)
- HDU:1242 Rescue(BFS+优先队列)
- 实际用户和有效用户的
- UIButton的基本用法和事件
- easyui验证
- iOS _监听UITextField输入状态
- 2786: [JSOI]Word Query电子字典
- 1007. Maximum Subsequence Sum
- iOS开发学习之 - 深入学习UIButton(1)
- iOS UI绘制和布局基础解析
- JAVA---Unique Characters
- 模板模式 和 策略模式的区别, builder pattern构造者模式,BeanDefinitionBuilder
- iOS学习笔记10-UIView动画