题干:

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

题目大意:

天使被困在监狱,他的朋友们想见他,监狱的地形复杂,包括路(用点标示),墙(用#标示),天使的位置(用a标示),他的朋友(用r标示),监狱里还有守卫(用x标示),他的朋友只能向左右上下四个方向走,走以不花一单位时间,若碰上守卫,消灭守卫需要额外花费一单位时间。问最少多长时间天使能见到他的朋友。

解题报告:

注意这题,可能有多个朋友,所以需要从Angel作为起点,朋友作为判断出口,而不是让朋友来找Angel,所以需要反着跑dfs。

错误代码:

#include<bits/stdc++.h>using namespace std;
const int INF = 0x3f3f3f3f;
int n,m;
int ans;
int ex,ey;
int nx[4] = {0,1,0,-1};
int ny[4] = {1,0,-1,0};
char maze[205][205];
bool vis[205][205];
bool fit(int x,int y) {if(x > n || x < 1 || y > m || y < 1) return false;return true;
}
void dfs(int x,int y,int step) {if(step >= ans) return ;if(maze[x][y] == 'r'/*x == ex && y == ey*/) {ans = min(ans,step);return;}for(int k = 0; k<4; k++) {int tx = x + nx[k];int ty = y + ny[k];if(maze[tx][ty] == '#' || vis[tx][ty] == 1) continue;if(!fit(tx,ty)) continue;if(maze[tx][ty] == 'x') step++;vis[tx][ty]=1;dfs(tx,ty,step+1);vis[tx][ty]=0;//step--;}
}int main()
{int xx,yy;while(~scanf("%d%d",&n,&m)) {ans = INT_MAX;for(int i = 1; i<=n; i++) {scanf("%s",maze[i]+1);}for(int i = 1; i<=n; i++) {for(int j = 1; j<=m; j++) {if(maze[i][j] == 'a') xx=i,yy=j;}}dfs(xx,yy,0);if(ans == INT_MAX) printf("Poor ANGEL has to stay in the prison all his life.\n");else printf("%d\n",ans);}return 0;
}

AC代码:

#include<bits/stdc++.h>using namespace std;
const int INF = 0x3f3f3f3f;
int n,m;
int ans;
int ex,ey;
int nx[4] = {0,1,0,-1};
int ny[4] = {1,0,-1,0};
char maze[205][205];
bool vis[205][205];
bool fit(int x,int y) {if(x > n || x < 1 || y > m || y < 1) return false;return true;
}
void dfs(int x,int y,int step) {if(step >= ans) return ;if(maze[x][y] == 'r'/*x == ex && y == ey*/) {ans = min(ans,step);return;}for(int k = 0; k<4; k++) {int tx = x + nx[k];int ty = y + ny[k];if(maze[tx][ty] == '#' || vis[tx][ty] == 1) continue;if(!fit(tx,ty)) continue;vis[tx][ty]=1;if(maze[tx][ty] == 'x') dfs(tx,ty,step+2);else dfs(tx,ty,step+1);vis[tx][ty]=0;}
}int main()
{int xx,yy;while(~scanf("%d%d",&n,&m)) {ans = INT_MAX;for(int i = 1; i<=n; i++) {scanf("%s",maze[i]+1);}for(int i = 1; i<=n; i++) {for(int j = 1; j<=m; j++) {if(maze[i][j] == 'a') xx=i,yy=j;}}dfs(xx,yy,0);if(ans == INT_MAX) printf("Poor ANGEL has to stay in the prison all his life.\n");else printf("%d\n",ans);}return 0;
}

对于这个错误代码,第一次修改的时候,加上了注释掉的那句step--,然后样例就wa,然后一想 还是不对,应该是如果step++过,那才step--,不然肯定答案就不正确了啊,于是就放弃了这种step++这种方式,改成了下面AC代码中的形式。

以后可以补充一下bfs的形式、、、

*【HDU - 1242 】 Rescue (反向dfs,或bfs)相关推荐

  1. (最优解法)46行代码AC_HDU1242 Rescue(DFS解法+BFS解法)

    励志用少的代码做高效表达 Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. T ...

  2. HDU 1242 Rescue BFS+优先队列

    题目链接:点击打开链接http://acm.hdu.edu.cn/showproblem.php?pid=1242 #include <stdio.h> #include <stri ...

  3. (step4.2.3)hdu 1242(Rescue——BFS)

    题目大意:friends用最短的时间去救angel '.'表示通道 '#'表示墙壁 'x'表示guard.走一格要一单位时间,杀死一个guard要一个单位时间. 如果可以救求最短时间,否则按要求输出 ...

  4. HDU 1242 Rescue

    bfs问题. Angel有被关在监狱,她有非常多朋友要去救她. #表示墙,.表示路,x表示警卫,r表示她的朋友. 因为可能有非常多朋友,可是Angel仅仅有一个,所以搜索起点设为Angel.仅仅要找到 ...

  5. 强连通图------(1)通过两次DFS或BFS判断是不是强连通图

    一.定义: 强连通图(Strongly Connected Graph)是指在有向图G中,如果对于每一对vi.vj,vi≠vj,从vi到vj和从vj到vi都存在路径,则称G是强连通图. 二.判断是不是 ...

  6. 数据结构基础(21) --DFS与BFS

    DFS 从图中某个顶点V0 出发,访问此顶点,然后依次从V0的各个未被访问的邻接点出发深度优先搜索遍历图,直至图中所有和V0有路径相通的顶点都被访问到(使用堆栈). //使用邻接矩阵存储的无向图的深度 ...

  7. LeetCode算法题7:DFS和BFS

    文章目录 前言 深度优先搜索算法伪代码: 广度优先搜索算法伪代码: 一.图像渲染 DFS: BFS: 上面BFS算法存在的问题: 修改 1: 修改 2: 二.岛屿的最大面积 DFS: BFS : 三. ...

  8. 连通图的判断(并查集, DFS, BFS)

    首先要明确什么是连通图??? 连通图:对于一个图来说,图中的任意一个点都能访问到所有的点,则说明该图连通 很明显,如果要判断一个图是否连通,则必须要从任意一个搜索一遍,判断是否到达了所有的点,则很快会 ...

  9. PAT甲级1099 Build A Binary Search Tree (30分):[C++题解]建立二叉搜索树、dfs和bfs

    文章目录 题目分析 题目链接 题目分析 题意重述:给定一棵二叉树的结构,和待填的数值,请将数据填到二叉树中的结点中,使之满足二叉搜索树的性质. 然后按照层序遍历输出数值. 分析: 本题分两步. 第一步 ...

  10. python 拓扑排序 dfs bfs_拓扑排序的DFS和BFS

    博主以前有一个疑问,DFS和BFS各自的适用范围是?我想你今天看了这篇文章之后会有一个判断! BFS 数据结构与算法分析:c语言描述(p217) 已经存在一个Indgree入度数组(indgree[v ...

最新文章

  1. Node的异步与java的异步_node中异步IO的理解
  2. 46W 奖金池等你来战!微众银行第三届金融科技高校技术大赛火热报名中!
  3. R语言因子分析FA(factor analysis)步骤实战
  4. Docker镜像的导入导出
  5. 全球主要城市经纬度api
  6. Vue状态管理之Vuex
  7. 运用神经网络方法找寻集成学习中的最优权重
  8. [java进阶]4.关键字throws和throw
  9. SharePoint 2010 隐藏快速启动栏(左侧导航)
  10. java j集合_JNotes/Java-集合篇(2)集合之List.md at master · harryjudy2240/JNotes · GitHub...
  11. (43)VHDL实现译码器与解码器
  12. Win10专业版下如何禁用Windows Defender功能?
  13. ubuntu装机并设置远程连接
  14. Linux内核源代码 学习笔记
  15. 安卓TV应用 Hello Word - 怎样新建一个Android TV 项目
  16. FPGA--有限状态机(FSM)的设计
  17. 大数据——DBT:dbt集成数据质量监控插件elementary(生产环境篇)
  18. 浅谈“高内聚,低耦合”
  19. 5,10,15,20-四(3,5-二甲氧基苯基)卟啉((TdmPP)H2)/2-硝基-5,10,15,20-四(3,5-二甲氧基苯基)卟啉铜(NO2TdmPP)Cu)齐岳供应
  20. linux关闭硬盘检测,关闭Linux开机强制检测硬盘

热门文章

  1. mpAndroidchart 坐标和图表距离_【玩转图表系列】六步,美化你的图表,让老板刮目相看!...
  2. 安川最小巧机器人_2020工博会,安川展品前瞻(机器人篇)
  3. php代码expl,php – 参数号无效:参数未定义Explination
  4. java替换list中元素,Java 实例 - List 元素替换
  5. 检测同心圆_(二)光线如何被眼睛检测到?
  6. html的div显示到最左侧,HTML/CSS:如何淡化div的左右边缘?
  7. c语言控制数码管显示时间,数码管显示时间整点报时C语言实例
  8. 如何查看Linux版本号(内核版本号和发行版本号)
  9. winCE改变字库方法
  10. WinCE驱动的动态加载