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 ...
随机推荐
- c# 谷歌动态口令对接
https://www.cnblogs.com/easyauthor/p/11054869.html Google 身份验证器与两步验证功能配合,可在您登录 Google 帐户时为您平添一重安全保障. ...
- lazarus for win32 serviceApp
procedure TMyThread.Execute; begin //Application.EventLog.FileName:='d:\xx.txt'; //Application.Event ...
- jQery Datatables回调函数中文
Datatables——回调函数 ------------------------------------------------- fnCookieCallback:还没有使用过 $(documen ...
- Java+超大文件上传
之前仿造uploadify写了一个HTML5版的文件上传插件,没看过的朋友可以点此先看一下~得到了不少朋友的好评,我自己也用在了项目中,不论是用户头像上传,还是各种媒体文件的上传,以及各种个性的业务需 ...
- BZOJ 3786: 星系探索 ETT
Description 物理学家小C的研究正遇到某个瓶颈. 他正在研究的是一个星系,这个星系中有n个星球,其中有一个主星球(方便起见我们默认其为1号星球),其余的所有星球均有且仅有一个依赖星球.主星球 ...
- CF452E Three strings 广义后缀自动机
建一个广义后缀自动机统计一下就行,好长时间不敲后缀自动机调了半天~ #include <bits/stdc++.h> using namespace std; namespace IO { ...
- Hive、Spark优化案例
一.Join原则 将条目少的表/子查询放在Join的左边.原因:在Join的reduce阶段,位于Join左边的表的内容会被加载进内存,条目少的表放在左边,可以减少发生内存溢出的几率. 小表关联大表: ...
- nodejs 中 接受前端的数据请求的处理
前台 ----> 后台 后台要接受 前台的数据,只能通过 http 但是 前台接受 后台的数据有 from ajax jsonp nodejs 给我们提供了模块 url 模块,可以 ...
- leetcode-mid-sorting and searching-75. Sort Colors-NO
mycode 97.95% class Solution(object): def sortColors(self, nums): """ :type nums: L ...
- node服务通过Jenkins上线流程
构建流程 构建服务器: 拉取指定分支代码 构建服务器: 安装依赖 构建服务器: 执行构建 构建服务器: 如果上线流程,则在 git 上创建 tag,供回滚使用 构建服务器:打包 node 服务代码,和 ...