dfs(最佳路径)
http://acm.hdu.edu.cn/showproblem.php?pid=1242
Rescue
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 43328 Accepted Submission(s): 14783
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.)
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.
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."
#.#####.
#.a#..r.
#..#x...
..#..#.#
#...##..
.#......
........
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <algorithm>
#include <iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include <stdio.h>
#include<queue>
#include <string.h>
#define INF 0x3f3f3f3f
using namespace std;
char s[][];
int ans ;
int dir[][] = {{,-},{,},{,},{-,}};
int n , m ;
int vis[][]; void dfs(int x , int y , int w)
{
if(ans <= w)
{
return ;
}
if(s[x][y] == 'r')
{
ans = min(ans , w);
return ;
}
vis[x][y] = ;//一条路径的开始
for(int i = ; i < ; i++)
{
int xx = x + dir[i][];
int yy = y + dir[i][];
if(vis[xx][yy] || s[xx][yy] == '#' || xx < || xx >= n || yy < || yy >= m)
{ continue ;
}
//vis[xx][yy] = 1;错误标记
if(s[xx][yy] == 'x')
dfs(xx , yy , w+);
else
dfs(xx , yy , w+);
//vis[xx][yy] = 0 ;//错误标记
}
vis[x][y] = ;//一条路径的结束,准备回溯。
} int main()
{ while(~scanf("%d%d" , &n , &m))
{
int x , y ;
for(int i = ; i < n ; i++)
{
for(int j = ; j < m ; j++)
{
cin >> s[i][j] ;
if(s[i][j] == 'a')
{
x = i , y = j ;
}
}
}
ans = INF ;
dfs(x , y , ); if(ans != INF)
{
cout << ans << endl ;
}
else
{
cout << "Poor ANGEL has to stay in the prison all his life." << endl;
}
} return ;
}
dfs(最佳路径)的更多相关文章
- ArcGIS 网络分析[1.2] 利用1.1的线shp创建网络数据集/并简单试验最佳路径
上篇已经创建好了线数据(shp文件格式)链接:点我 这篇将基于此shp线数据创建网络数据集. 在此说明:shp数据的网络数据集仅支持单一线数据,也就是说基于shp文件的网络数据集,只能有一个shp线文 ...
- 哈密顿绕行世界问题(dfs+记录路径)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2181 哈密顿绕行世界问题 Time Limit: 3000/1000 MS (Java/Others) ...
- 使用 EOLINKER 进行接口测试的最佳路径 (下)
本文为 <使用 EOLINKER 进行接口测试的最佳路径> 下半部分文章,主要介绍测试脚本如何执行和报告生成,以及测试项目人员如何协作.还没看过上篇文章请戳 使用 EOLINKER 进行接 ...
- BFS和DFS记录路径
DFS记录路径的意义好像不大,因为不一定是最短的,但是实现起来却很简单. #include<math.h> #include<stdio.h> #include<queu ...
- 寻找最佳路径(ArcPy实现)
一.背景 随着社会经济发展需求,公路的重要性日益提高.在一些交通欠发达的地区,公路建设迫在眉睫.如何根据实际地形情况设计出比较合理的公路规划,是一个值得研究的问题. 二.实验目的: (1)通过练习,熟 ...
- dp(动态规划之最佳路径+dfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1078 FatMouse and Cheese Time Limit: 2000/1000 MS (Java/Ot ...
- SDUT 1269-走迷宫(DFS打印路径)
走迷宫 Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描写叙述 有一个m*n格的迷宫(表示有m行.n列),当中有可走的也有不可走的,假 ...
- hdu1242 Rescue DFS(路径探索题)
这里我定义的路径探索题指 找某路能够到达目的地,每次走都有方向,由于是探索性的走 之后要后退 那些走过的状态都还原掉 地址:http://acm.hdu.edu.cn/showproblem.php? ...
- poj 2488 A Knight's Journey 【骑士周游 dfs + 记忆路径】
题目地址:http://poj.org/problem?id=2488 Sample Input 3 1 1 2 3 4 3 Sample Output Scenario #1: A1 Scenari ...
随机推荐
- 《Spring源码深度解析》一
Spring整体架构 1.1 Spring整体架构 1.1.1 Core Container: 模块:Core.Beans.Context和Expression Language Core:框架的基础 ...
- Educational Codeforces round 78 A、B
链接:https://codeforces.com/contest/1278 A:Shuffle Hashing 题意:对于一个字符串p可以执行一个"hash"操作,首先将p内的元 ...
- 10.django的一些方法理解
django get_object_or_404 get_object_or_404是django的django shortcuts的方法,用来查询数据,或者抛出一个DoesNotExist的异常 用 ...
- Redis实战(十四)Redis实现Session共享
序言 登录的处理流程: 1.登录页面提交用户名密码. 2.登录成功后生成token.Token相当于原来的jsessionid,字符串,可以使用uuid. 3.把用户信息保存到redis.Key就是t ...
- Html5大文件断点续传实现方法
之前仿造uploadify写了一个HTML5版的文件上传插件,没看过的朋友可以点此先看一下~得到了不少朋友的好评,我自己也用在了项目中,不论是用户头像上传,还是各种媒体文件的上传,以及各种个性的业务需 ...
- hdu 1166 线段树 区间求和 +单点更新 CD模板
题目链接 敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total S ...
- Navicat使用与python操作数据库
一.Navicat使用 1.下载地址: <https://pan.baidu.com/s/1bpo5mqj> 2.测试+链接数据库,新建库 3.新建表,新增字段+类型+约束 4.设计表:外 ...
- jenkins启动tomcat失败的解决方法
在网上看了都说是加BUILDID, 但是我加了之后,还是启动不成功. 执行了下面2个步骤: 1.在远程服务器的启动脚本里,用nohup来运行启动命令 nohup ./*.start.${prg}.sh ...
- BZOJ 2281 Luogu P2490 [SDOI2011]黑白棋 (博弈论、DP计数)
怎么SDOI2011和SDOI2019的两道题这么像啊..(虽然并不完全一样) 题目链接: (bzoj) https://www.lydsy.com/JudgeOnline/problem.php?i ...
- lines
lines Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...