*【HDU - 1242 】 Rescue (反向dfs,或bfs)

题干:

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的形式、、、

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441837.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

华为鸿蒙os系统转正,华为鸿蒙OS系统正式官宣,转正工作提上日程,明年多款终端将使用...

华为鸿蒙OS系统相信很多小伙伴都不陌生&#xff0c;作为国内现如今顶尖的科技企业。华为这些年的发展也是十分迅速的&#xff0c;而再快速的发展过程中。更多的用户对于华为的新款系统也充满了好奇&#xff0c;要知道一款属于国人自己的国产系统。在之前的国内手机上是几乎不存…

【FZU - 2140 】Forever 0.5 (计算几何,构造)

题干&#xff1a; Given an integer N, your task is to judge whether there exist N points in the plane such that satisfy the following conditions: 1. The distance between any two points is no greater than 1.0. 2. The distance between any point and the ori…

map型字段 mongodb_MongoDB极简教程

来源&#xff1a;我没有三颗心脏1.MongDB 简介MongoDB(来自于英文单词“Humongous”&#xff0c;中文含义为“庞大”)是可以应用于各种规模的企业、各个行业以及各类应用程序的开源数据库。作为一个适用于敏捷开发的数据库&#xff0c;MongoDB 的数据模式可以随着应用程序的发展…

html 如何改变图片形状,图形变换的三种方式是什么?

图形变换的三种方式1、平移平移&#xff0c;是指在同一平面内&#xff0c;将一个图形上的所有点都按照某个直线方向做相同距离的移动&#xff0c;这样的图形运动叫做图形的平移运动&#xff0c;简称平移。平移不改变图形的形状和大小。图形经过平移&#xff0c;对应线段相等&am…

springboot html压缩,springboot 请求响应压缩

官方文档原文&#xff1a;[https://docs.spring.io/spring-boot/docs/2.1.5.RELEASE/reference/htmlsingle/\#how-to-enable-http-response-compression][https_docs.spring.io_spring-boot_docs_2.1.5.RELEASE_reference_htmlsingle_how-to-enable-http-response-compression]…

Trie树(字典树)详细知识点及其应用

Trie&#xff0c;又经常叫前缀树&#xff0c;字典树等等。它有很多变种&#xff0c;如后缀树&#xff0c;Radix Tree/Trie&#xff0c;PATRICIA tree&#xff0c;以及bitwise版本的crit-bit tree。当然很多名字的意义其实有交叉。 定义 在计算机科学中&#xff0c;trie&#x…

aip格式转化为pdf_python提取pdf文档中的表格数据、svg格式转换为pdf

提取pdf文件中的表格数据原文链接https://www.analyticsvidhya.com/blog/2020/08/how-to-extract-tabular-data-from-pdf-document-using-camelot-in-python/另外还参考了这篇文章https://camelot-py.readthedocs.io/en/master/实现提取pdf文档中的表格数据需要使用camelot模块…

html验证邮箱自动,html5+JavaScript进行邮箱地址验证

html5 网页特效 邮箱地址验证body, input, textarea {font-family: "helvetica", arial, helvetica;}label {display: block;float: left;clear: left;text-align: right;width: 100px;margin-right: 10px;}p { padding: 10px; }fieldset { border: 1px solid #ccc; …

6 rethad 自定义硬盘_图文详解zabbix配置自定义监控项过程

概述今天主要介绍一下zabbix如何去配置自定义监控模板&#xff0c;下面一起来看看吧&#xff01;一、模板1、概念模板(template)&#xff1a;可以应用到主机上的实体(包括监控项、触发器、数据图、图表、监控分类、低级的自动发现规则)的集合。当一个模板被应用到一个主机后&am…

【HDU - 1085 】Holding Bin-Laden Captive! (母函数)

题干&#xff1a; We all know that Bin-Laden is a notorious terrorist, and he has disappeared for a long time. But recently, it is reported that he hides in Hang Zhou of China! “Oh, God! How terrible! ” Don’t be so afraid, guys. Although he hides in a…

html text align属性,HTML canvas

实例在位置 150 创建一条红线。位置 150 是下面实例中定义的所有文本的锚点。请研究每种 textAlign 属性值的效果&#xff1a;YourbrowserdoesnotsupporttheHTML5canvastag.JavaScript&#xff1a;var cdocument.getElementById("myCanvas");var ctxc.getContext(&qu…

html超链接点不了_HTML、CSS、JS都有哪些区别?不看必悔

划重点HTML、CSS、及JS的区别有哪些&#xff1f;在日常学习中&#xff0c;html&#xff0c;css和js我们都学过&#xff0c;起初分不清这三者的区别和联系&#xff0c;随着知识的增长&#xff0c;有了一些体会。看一下这三项技术都是什么&#xff0c;能干什么&#xff1f;web前端…

【Codeforces - 378C】Maze(dfs,思维)

题干&#xff1a; Pavel loves grid mazes. A grid maze is an n  m rectangle maze where each cell is either empty, or is a wall. You can go from one cell to another only if both cells are empty and have a common side. Pavel drew a grid maze with all empty…

2019游戏 f1_2019年F1中国大奖赛激情开跑,这款手游带你体验真实F1的魅力

不知不觉&#xff0c;F1中国大奖赛进入第16个年头。你是否还记得2018年的F1中国大奖赛&#xff0c;真是跌宕起伏&#xff0c;红牛队车手里卡多在第三次练习赛中爆缸&#xff0c;之后在车队技师的努力下才赶上排位赛&#xff0c;结果他在正赛时第6位发车&#xff0c;竟然超出对手…

html自定义字体demo,如何在HTML网站上安装自定义字体

是的&#xff0c;您可以使用名为 font-face的CSS功能。它只在CSS3中正式批准&#xff0c;但已经在CSS2中提出并实现&#xff0c;并且在IE中已经支持相当长的时间。你在CSS中声明它像这样&#xff1a;font-face { font-family: Delicious; src: url(Delicious-Roman.otf); }font…

【OpenJ_Bailian - 1258】【POJ - 1258】Agri-Net (最小生成树裸题)

题干&#xff1a; Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the area. He needs your help, of course. Farmer John ordered a high speed connection for his farm and is goin…

docker jenkins 公钥_代码自动发布docker(20.10.1)+k8s(1.20.1)

PaaS平台Docker版本(20.10.1) 2020-12-15 k8s版本(1.20.1) 2020-12-19代码托管&#xff1a;https://github.com/kubernetes/官方网址&#xff1a;https://kubernetes.io/pod--容器外壳service--不是真正的服务是iptables或ipvs中的规则先创建pod&#xff0c;后创建service…

【HDU - 1272】小希的迷宫 (并查集判环)

题干&#xff1a; 上次Gardon的迷宫城堡小希玩了很久&#xff08;见Problem B&#xff09;&#xff0c;现在她也想设计一个迷宫让Gardon来走。但是她设计迷宫的思路不一样&#xff0c;首先她认为所有的通道都应该是双向连通的&#xff0c;就是说如果有一个通道连通了房间A和B&…

东北育才高中2021年高考成绩查询,东北育才学校国际部2021年招生计划

学校每年的招生计划都会发生变化&#xff0c;并不是一成不变的&#xff0c;所以大家一定要时刻关注招生计划。在报考一所学校的时候&#xff0c;大家要有计划性的选择报考学校&#xff0c;要先去了解学校大概招收多少学生&#xff0c;这样大家在报考学校时候的才能更好地去选择…

html启动word程序,Word工具栏直接启动外部程序

Word工具栏直接启动外部程序互联网 发布时间&#xff1a;2008-10-06 15:13:37 作者&#xff1a;佚名 我要评论从Word工具栏中也能直接启动外部程序&#xff0c;这样操作起来非常方便。我们以从Word启动计算器举例说明。单击“工具”菜单下的“自定义”命令&#xff0c;打…