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 比赛的时候我甚至没看这道题,事实上不难.... 可是说实话,如今对题意还是理解不太好...... 犯的错误 ...
随机推荐
- Java JDBC的基本知识
CallableStatement接口——主要调用数据库中的存储过程 即为一种方法,可以调用, 传递参数 delimiter // //这里是改变执行操作语句的分隔符,也就是将SQL语句的&quo ...
- 【Python学习之五】高级特性4(切片、迭代、列表生成器、生成器、迭代器)
4.生成器(generator) 通过列表生成式,我们可以直接创建一个列表.但是,受到内存限制,列表容量肯定是有限的.如果列表元素可以按照某种算法推算出来,那我们是否可以在循环的过程中不断推算出后续的 ...
- Java-JFrame可视化开发
Java-JFrame可视化开发的一般步骤 JFrame可以做出类似于QQ登录功能的窗体,通过JFrame可以利用Java代码实现窗体功能,一般用于CS项目的C(客户端)的开发: 利用JFrame可以 ...
- docker镜像下载
获得CentOS的Docker CE 预计阅读时间: 10分钟 要在CentOS上开始使用Docker CE,请确保 满足先决条件,然后 安装Docker. 先决条件 Docker EE客户 要安装D ...
- [图文]RHEL 7/CentOS 7/Fedora28 联网初始化
实验说明: 入门Linux,一般会遇到以下几个问题: 从哪里获取LInux镜像? 如何通过镜像文件安装Linux系统? 安装实体机还是虚拟机? 安装完系统如何配置网络? 虚拟机的网络配置与实体机有何不 ...
- 【mysql】mysql has gone away
原文 http://www.jb51.net/article/23781.htm MySQL server has gone away 问题的解决方法 投稿:mdxy-dxy 字体:[增加 减小] 类 ...
- Hessian知识学习总结(二)——Hessian的helloworld
一.下载Hessian 可在hessian官网http://hessian.caucho.com/ 或者http://download.csdn.net/detail/wodediqizhang/95 ...
- python3爬取”理财大视野”中的股票,并分别写入txt、excel和mysql
需求:爬取“理财大视野”网站的排名.代码.名称.市净率.市盈率等信息,并分别写入txt.excel和mysql 环境:python3.6.5 网站:http://www.dashiyetouzi.co ...
- LeetCode(162) Find Peak Element
题目 A peak element is an element that is greater than its neighbors. Given an input array where num[i ...
- LeetCode (160) Intersection of Two Linked Lists
题目 Write a program to find the node at which the intersection of two singly linked lists begins. For ...