hdu 1242(搜索)
Rescue
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 25081 Accepted Submission(s): 8887
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...
..#..#.#
#...##..
.#......
........
走卫兵守护的路花费的时间多1,考虑优先队列
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<math.h>
#include<queue>
#include<iostream>
using namespace std;
typedef long long LL; char graph[][];
bool vis[][];
struct Node
{
int x,y;
int step;
};
Node s,t;
bool operator < (Node a,Node b)
{
return a.step>b.step;
}
int n,m;
int dir[][] = {{-,},{,},{,-},{,}};
bool check(int x,int y)
{
if(x<||x>=n||y<||y>=m||graph[x][y]=='#'||vis[x][y]==true) return false;
return true;
}
int bfs()
{
memset(vis,false,sizeof(vis));
priority_queue<Node> q;
q.push(s);
vis[s.x][s.y]=true;
s.step = ;
while(!q.empty())
{
Node now = q.top();
q.pop();
if(now.x==t.x&&now.y==t.y)
{
return now.step;
}
Node next;
for(int i=; i<; i++)
{
next.x = now.x+dir[i][];
next.y = now.y+dir[i][];
if(!check(next.x,next.y)) continue;
if(graph[next.x][next.y]=='x')
{
next.step=now.step+;
q.push(next);
vis[next.x][next.y]=;
}
else if(graph[next.x][next.y]=='.')
{
next.step=now.step+;
q.push(next);
vis[next.x][next.y]=;
}
}
}
return -;
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=; i<n; i++)
{
scanf("%s",graph[i]);
for(int j=; j<m; j++)
{
if(graph[i][j]=='r')
{
s.x=i,s.y=j;
graph[i][j]='.';
}
if(graph[i][j]=='a')
{
t.x=i,t.y=j;
graph[i][j]='.';
}
} }
int res = bfs();
if(res==-)
{
printf("Poor ANGEL has to stay in the prison all his life.\n");
}
else printf("%d\n",res);
}
return ;
}
hdu 1242(搜索)的更多相关文章
- hdu 1242 Rescue
题目链接:hdu 1242 这题也是迷宫类搜索,题意说的是 'a' 表示被拯救的人,'r' 表示搜救者(注意可能有多个),'.' 表示道路(耗费一单位时间通过),'#' 表示墙壁,'x' 代表警卫(耗 ...
- hdu 5887 搜索+剪枝
Herbs Gathering Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- hdu 5636 搜索 BestCoder Round #74 (div.2)
Shortest Path Accepts: 40 Submissions: 610 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: ...
- Square HDU 1518 搜索
Square HDU 1518 搜索 题意 原题链接 给你一定若干个木棒,让你使用它们组成一个四边形,要求这些木棒必须全部使用. 解题思路 木棒有多种组合方式,使用搜索来进行寻找,这里需要进行优化,不 ...
- HDU 1242 (BFS搜索+优先队列)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1242 题目大意:多个起点到一个终点,普通点耗时1,特殊点耗时2,求到达终点的最少耗时. 解题思路: ...
- HDU 1242 Rescue (BFS(广度优先搜索))
Rescue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submis ...
- hdu - 1242 Rescue && hdu - 2425 Hiking Trip (优先队列+bfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1242 感觉题目没有表述清楚,angel的朋友应该不一定只有一个,那么正解就是a去搜索r,再用普通的bfs就能过了 ...
- HDU 1242 Rescue(BFS+优先队列)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1242 题目描述: Problem Description Angel was caught by t ...
- hdu 4848 搜索+剪枝 2014西安邀请赛
http://acm.hdu.edu.cn/showproblem.php?pid=4848 比赛的时候我甚至没看这道题,事实上不难.... 可是说实话,如今对题意还是理解不太好...... 犯的错误 ...
随机推荐
- Linux Ptrace 详解
转 https://blog.csdn.net/u012417380/article/details/60470075 Linux Ptrace 详解 2017年03月05日 18:59:58 阅读数 ...
- Bootstrap 模态框 select2搜索框无法输入
去掉模态框的div中的 tabindex="-1" 这个属性 <div class="modal fade" role="dialog" ...
- 【Java_Spring】RestTemplate发HTTP请求详解
Springboot — 用更优雅的方式发HTTP请求(RestTemplate详解) Spring RestTemplate提交时设置http header请求头 Spring之RestTempla ...
- Qt概念和快捷键
Qt概念和快捷键 Qt简介 1.Qt的由来和发展 Qt是一个1991年由Qt Company开发的跨平台C++图形用户界面应用程序开发框架.它既可以开发GUI程序,也可用于开发非GUI程 ...
- vsftpd服务安装与虚拟用户配置
vsftpd的全名是“Very secure FTP Daemon” 一.安装vsftpd安装db4-util用于生成认证文件 yum -y install db4-utils 安装vsftpd yu ...
- 浅探webpack优化
由于前端的快速发展,相关工具的发展速度也是相当迅猛,各大框架例如vue,react都有自己优秀的脚手架工具来帮助我们快速启动一个新项目,也正式因为这个原因,我们对于脚手架中最关键的一环webpack相 ...
- Geode 集群搭建,快速上手使用
Geode 介绍: Geode是一个提供实时.一致访问大型分布式云平台下数据密集型应用的数据管理平台. Geode 通过跨多进程,把内存.CPU.网络资源和可选的本地磁盘汇集起来,来管理应用程序对象及 ...
- 【HIHOCODER 1176】 欧拉路·一
描述 小Hi和小Ho最近在玩一个解密类的游戏,他们需要控制角色在一片原始丛林里面探险,收集道具,并找到最后的宝藏.现在他们控制的角色来到了一个很大的湖边.湖上有N个小岛(编号1..N),以及连接小岛的 ...
- Linux学习-用 make 进行宏编译
为什么要用 make 先来想象一个案例,假设我的执行档里面包含了四个原始码文件,分别是 main.c haha.c sin_value.c cos_value.c 这四个文件,这四个文件的目的是: m ...
- 在python中对元祖进行排序
在python里你可以对一个元组进行排序.例子是最好的说明: >>> items = [(1, 'B'), (1, 'A'), (2, 'A'), (0, 'B'), (0, 'a' ...