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 ...
随机推荐
- GO语言学习笔记5-defer的使用
1. 什么是defer defer是Go语言提供的一种用于注册延迟调用的机制:让函数或语句可以在当前函数执行完毕后(包括通过return正常结束或者panic导致的异常结束)执行. 2. defer的 ...
- Devexpress MVC GridView / CardView (持续更新)
//获取gridview里面的combo box 显示的文本 //获取某个column在gridview的 index RightGridView.GetColumnByField("Fun ...
- CDOJ 1057 秋实大哥与花 线段树 区间更新+区间查询
链接: I - 秋实大哥与花 Time Limit:1000MS Memory Limit:65535KB 64bit IO Format:%lld & %llu Submit ...
- 序列式容器————dequeue
#include <deque> 双端队列,可以在队头队尾进行入队出队操作 deque<int> q; q.empty(); q.push_front(s);//将s插入到队头 ...
- java中的过滤器 --Filter
package filter; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.Filter ...
- Springboot 后台管理框架halo.kotlin
https://gitee.com/backControl/halo-kotlin 现在开了两个分支,一个分支在做数据分析,一个分支是集成spring oauth2.0技术 之后会接入最新的技术和实现 ...
- kafka操作命令
kafka启动 bin/kafka-server-start.sh -daemon config/server.properties 创建topic bin/kafka-topics.sh -zook ...
- webpack4常用片段
webpack 4常用 初始化 npm init // Webpack 4.0以后需要单独安装 npm install webpack webpack-cli --save-dev 基础的config ...
- 2018 icpc 徐州
A 矩阵树定理可以用于最小生成树计数,最直观的做法就是求个mst,再用矩阵树定理求最小生成树个数,但是n<=1e5,显然不是o(n^3)可以做出来的. 考虑随机数据生成器,固定1e5的边,但是边 ...
- Spring bean的自动装配属性
bean的自动装配属性能简化xml文件配置. bean 的自动装配属性分为四种: 1.byName 2.byTyoe 3.constructor 4. autodetect byName: 它查找配置 ...