【HDU】 1242 Rescue
来源:互联网 发布:jisucopy是什么软件 编辑:程序博客网 时间:2024/06/03 19:45
Rescue
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 23012 Accepted Submission(s): 8134
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
题解:我表示这一题我是用DFS混的...
正解应该是用BFS加优先队列。
#include <iostream>#include <cstring>#include <cstdio>#include <cstdlib>using namespace std;int way[4][2]={{0,1},{0,-1},{1,0},{-1,0}};char s[205][205];int n,m,flag[205][205],min1,x,y;int check(int x,int y){ return s[x][y]!='#' && x>=0 && x<n && y>=0 && y<m && !flag[x][y];}void dfs(int x,int y,int d){ int p=0; if (s[x][y]=='r') { min1=min(min1,d); return; } for (int i=0;i<4;i++) if (check(x+way[i][0],y+way[i][1])) { if (s[x+way[i][0]][y+way[i][1]]=='x') p=1; flag[x+way[i][0]][way[i][1]+y]=1; dfs(x+way[i][0],y+way[i][1],d+p+1); flag[x+way[i][0]][way[i][1]+y]=0; p=0; }}int main(){ while (scanf("%d%d",&n,&m)!=EOF) { memset(s,0,sizeof(s)); memset(flag,0,sizeof(flag)); min1=10000000; for (int i=0;i<n;i++) scanf("%s",s[i]); for (int i=0;i<n;i++) for (int j=0;j<m;j++) if (s[i][j]=='a') { x=i; y=j; break; } flag[x][y]=1; dfs(x,y,0); if (min1!=10000000) printf("%d\n",min1); else printf("Poor ANGEL has to stay in the prison all his life.\n"); } return 0;}
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
- 86.Xcode配置.pch文件
- 系统学习机器学习之线性判别式(二)
- Duilib介绍以及各个类的简介
- IOS/Swift 通讯录步骤
- switch 中的return break
- 【HDU】 1242 Rescue
- tableview或者ScrollView看不到边界以外的内容
- java学习—day_6
- Centos下:/usr/bin/ld: cannot find -lXext
- float类型在值传递过程中出现的问题
- 项目管理十大知识范围
- poj 1611 The Suspects(并查集)
- 数据结构与算法学习
- 系统学习机器学习之参数方法(三)