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.)
 

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

题意概况:给你一个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:

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 孕妇左侧肋骨疼怎么办 宝宝睡觉质量差怎么办 晚上睡觉作梦怎么办 猫打呼噜很大声怎么办 一睡觉就做梦怎么办 脑子里老是幻觉怎么办 严重认床睡不着怎么办 认床导致睡不着怎么办 碰到打呼噜的人怎么办 打呼吵得睡不着怎么办 打呼太吵睡不着怎么办 睡觉时舍友说话怎么办 夜晚醒了睡不着怎么办 夜晚怕黑睡不着怎么办 宝宝不愿意盖被子怎么办 白天睡觉晚上睡不着怎么办 晚上睡觉不安神怎么办 晚上经常睡不着觉怎么办 老是睡不着觉怎么办啊 小孩睡觉不安稳怎么办 睡觉时动不了怎么办 特别累还睡不着怎么办 又累又睡不着怎么办 干活累的睡不着怎么办 狗一有动静就叫怎么办 楼上天天闹动静怎么办 喝了奶茶失眠怎么办 失眠一宿第二天怎么办 睡觉外面噪音大怎么办 怀孕早期晚上睡不着怎么办 短发发尾翘怎么办 很累就是睡不着怎么办 人累但是睡不着怎么办 如果晚上睡不着该怎么办 晚上睡不着觉该怎么办 晚上睡不着该怎么办呢 晚上失眠睡不着该怎么办 晚上一直睡不着该怎么办 怀孕晚上睡不着该怎么办 运动太累睡不着怎么办 运动完睡不着觉怎么办