【HDU】 1242 Rescue
2016-01-19 09:37
253 查看
Rescue
[b]Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 23012 Accepted Submission(s): 8134
[/b]
[align=left]Problem Description[/align]
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.)
[align=left]Input[/align]
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.
[align=left]Output[/align]
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."
[align=left]Sample Input[/align]
7 8 #.#####. #.a#..r. #..#x... ..#..#.# #...##.. .#...... ........
[align=left]Sample Output[/align]
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; }
相关文章推荐
- Duilib介绍以及各个类的简介
- 处理隐藏导航栏底部的横线首先要明白苹果导航栏底部横线用的是UIImageView
- Android UI线程和非UI线程
- EF框架step by step(9)—Code First Fluent API
- bzoj1570: [JSOI2008]Blue Mary的旅行
- CodeForces 527D Clique Problem
- async = require('async')
- iOS学习笔记5-UIView
- UIView动画——钟表的制作
- IOS学习笔记UI篇
- New UI-带图片(drawableXxx)的TextView
- UICollectionView学习总结
- PHP 统计一维数组value相同的元素的个数num,并将其转化为下标为数字,值是value和num的二维数组
- Android UI集锦——1.Android Drawable分类汇总(1/3)
- Android UI集锦——1.Android Drawable分类汇总(2/3)
- Android UI集锦——1.Android Drawable分类汇总(3/3)
- iOS UIImageView设置为圆形
- iOS开发--一些UINavigationBar属性的设置
- RabbitMQ 3.5.3,guest用户无法远程登陆
- New UI-为TextView设置超链接跳转的三个方法