hdu 1242 Rescue
来源:互联网 发布:知乎 360借壳江南嘉捷 编辑:程序博客网 时间:2024/06/03 19:57
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
题意概况:给你一个n*m的图,a代表天使的位置,r代表天使的朋友的位置,x代表士兵,遇到士兵需要花费1个时间去打败他,天使的朋友要去救天使,问最快需要多少时间找到天使?
解题思路:
DFS:因为天使有多个朋友,所以应该让天使去找朋友,找到一个朋友记录需要的时间,在找到下一个朋友的时候比较需要时间选择短的,看最后所用的最短时间。因为有士兵的存在,在遇到士兵的用时加1。其他就是DFS模板。
BFS:
代码:
DFS
#include<stdio.h>#include<string.h>#include<math.h>#define N 300int m,n;char map[N][N];int book[N][N];int max;int next[4][2]={{1,0},{0,1},{0,-1},{-1,0}};void DFS(int x,int y,int t){int i,j,k;if(map[x][y]=='r'){if(t<max)max=t;return ;}if(map[x][y]=='x'){t++;}for(i=0;i<4;i++){int tx=x+next[i][0];int ty=y+next[i][1];if(tx>m||tx<1||ty>n||ty<1)continue;//printf("%d %d \n",tx,ty);if(map[tx][ty]!='#'&&book[tx][ty]==0){book[tx][ty]=1;DFS(tx,ty,t+1);book[tx][ty]=0;}}return ;}int main(){int i,j,u,v;while(scanf("%d%d",&m,&n)!=EOF){max=99999999;for(i=1;i<=m;i++){scanf("\n");for(j=1;j<=n;j++){scanf("%c",&map[i][j]);if(map[i][j]=='a'){u=i;v=j;}}}/*for(i=1;i<=m;i++){for(j=1;j<=n;j++)printf("%c",map[i][j]);printf("\n");}*///printf("%d %d \n",u,v);memset(book,0,sizeof(book));book[u][v]=1;DFS(u,v,0);if(max==99999999)printf("Poor ANGEL has to stay in the prison all his life.\n");elseprintf("%d\n",max);}return 0;}
BFS:
阅读全文
0 0
- hdu 1242 Rescue
- HDU-1242-Rescue
- HDU 1242 Rescue
- HDU-1242 Rescue
- hdu 1242 Rescue
- hdu 1242 Rescue<java>
- hdu 1242 rescue
- HDU-1242:Rescue
- HDU 1242 Rescue
- HDU 1242 Rescue
- hdu 1242 Rescue
- hdu 1242 Rescue
- hdu 1242 Rescue
- hdu 1242 Rescue
- hdu 1242 Rescue
- Rescue hdu 1242
- hdu-1242-Rescue
- Hdu 1242 Rescue
- 关于Spring+Hibernate注解式整合问题
- Unity_Itween插件
- Fiori学习笔记
- Python如何忽略警告
- DataTable快速导到Excel方法
- hdu 1242 Rescue
- Web应用部署笔记(廖雪峰教程)
- CPP基础知识(二)--类和对象
- RCNN 若干问题
- Git远程仓库
- ubuntu 的upgrade 和 update 安装git
- 全栈必备 敏捷估点
- 移动端语音播放以及语音条拖动的实现
- Qt笔记_18