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

Problem Description
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
 
Author
CHEN, Xue
 
Source
 题意:多个r(friends)救a(angle),‘x’代表守卫可以选择干掉他,但需要多花1分钟。
问最块要多久能救a(angle)。
#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(最佳路径)的更多相关文章

  1. ArcGIS 网络分析[1.2] 利用1.1的线shp创建网络数据集/并简单试验最佳路径

    上篇已经创建好了线数据(shp文件格式)链接:点我 这篇将基于此shp线数据创建网络数据集. 在此说明:shp数据的网络数据集仅支持单一线数据,也就是说基于shp文件的网络数据集,只能有一个shp线文 ...

  2. 哈密顿绕行世界问题(dfs+记录路径)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2181 哈密顿绕行世界问题 Time Limit: 3000/1000 MS (Java/Others) ...

  3. 使用 EOLINKER 进行接口测试的最佳路径 (下)

    本文为 <使用 EOLINKER 进行接口测试的最佳路径> 下半部分文章,主要介绍测试脚本如何执行和报告生成,以及测试项目人员如何协作.还没看过上篇文章请戳 使用 EOLINKER 进行接 ...

  4. BFS和DFS记录路径

    DFS记录路径的意义好像不大,因为不一定是最短的,但是实现起来却很简单. #include<math.h> #include<stdio.h> #include<queu ...

  5. 寻找最佳路径(ArcPy实现)

    一.背景 随着社会经济发展需求,公路的重要性日益提高.在一些交通欠发达的地区,公路建设迫在眉睫.如何根据实际地形情况设计出比较合理的公路规划,是一个值得研究的问题. 二.实验目的: (1)通过练习,熟 ...

  6. dp(动态规划之最佳路径+dfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1078 FatMouse and Cheese Time Limit: 2000/1000 MS (Java/Ot ...

  7. SDUT 1269-走迷宫(DFS打印路径)

    走迷宫 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描写叙述 有一个m*n格的迷宫(表示有m行.n列),当中有可走的也有不可走的,假 ...

  8. hdu1242 Rescue DFS(路径探索题)

    这里我定义的路径探索题指 找某路能够到达目的地,每次走都有方向,由于是探索性的走 之后要后退 那些走过的状态都还原掉 地址:http://acm.hdu.edu.cn/showproblem.php? ...

  9. 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 ...

随机推荐

  1. jvm——CodeCache

    https://juejin.im/post/5c890f21f265da2d993dc692 CodeCache是热点代码的暂存区,经过即时编译器编译的代码会放在这里,它存在于堆外内存.除了JIT编 ...

  2. 【leetcode】1218. Longest Arithmetic Subsequence of Given Difference

    题目如下: Given an integer array arr and an integer difference, return the length of the longest subsequ ...

  3. java-dockerfile

    java环境dockefile FROM centos:7 MAINTAINER yon@taexa.com ENV JAVA_HOME /usr/local/jdk ENV JRE_HOME ${J ...

  4. Redis实战(十三)Redis的三种集群方式

    序言 能聊聊redis cluster集群模式的原理吗 资料 https://www.cnblogs.com/51life/p/10233340.html Redis 集群分片原理

  5. JUnit——assertThat(acture,matcher)

    使用hamcrest之前需要引入相关的jar包,包括hamcrest-core.1.3.jar和hamcrest-library-1.3.jar. 具体引入的方法为:右击JUnit工程——build ...

  6. TTTTTTTTTTTTTTTTT HDU 2586 How far away LCA的离线算法 Tarjan

    链接: How far away ? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  7. 【BZOJ3931】[CQOI2015]网络吞吐量

    Description 路由是指通过计算机网络把信息从源地址传输到目的地址的活动,也是计算机网络设计中的重点和难点.网络中实现路由转发的硬件设备称为路由器.为了使数据包最快的到达目的地,路由器需要选择 ...

  8. Mysql安装及用简单用法

    Mysql安装和初始化 初见: Mysql软件 - 服务端软件 - 服务端程序 - 解析指令 - 对文件的操作 - 客户端软件 - 客户端程序 - 发送指令(sql语句) - 解析指令 其他类似软件: ...

  9. shell实现统计浏览次数并将结果保存到文件中

    日志文件是每日一个.统计日志文件中的关键字,获取每日浏览次数.将次数保存到txt文件中.. 将日期也一并保存到txt文件中. 输入开始日期和结束日期,就可以统计出每日的次数 代码如下: #!/bin/ ...

  10. Centos7系统备份与恢复教程

    原文地址:https://www.cnblogs.com/fang888/p/8473485.html Centos系统备份与恢复教程 tar: 特点 1.保留权限 2.适合备份整个目录 3.可以选择 ...