Rescue

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 25081    Accepted Submission(s): 8887

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
 

走卫兵守护的路花费的时间多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(搜索)的更多相关文章

  1. hdu 1242 Rescue

    题目链接:hdu 1242 这题也是迷宫类搜索,题意说的是 'a' 表示被拯救的人,'r' 表示搜救者(注意可能有多个),'.' 表示道路(耗费一单位时间通过),'#' 表示墙壁,'x' 代表警卫(耗 ...

  2. hdu 5887 搜索+剪枝

    Herbs Gathering Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  3. hdu 5636 搜索 BestCoder Round #74 (div.2)

    Shortest Path  Accepts: 40  Submissions: 610  Time Limit: 4000/2000 MS (Java/Others)  Memory Limit: ...

  4. Square HDU 1518 搜索

    Square HDU 1518 搜索 题意 原题链接 给你一定若干个木棒,让你使用它们组成一个四边形,要求这些木棒必须全部使用. 解题思路 木棒有多种组合方式,使用搜索来进行寻找,这里需要进行优化,不 ...

  5. HDU 1242 (BFS搜索+优先队列)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1242 题目大意:多个起点到一个终点,普通点耗时1,特殊点耗时2,求到达终点的最少耗时. 解题思路: ...

  6. HDU 1242 Rescue (BFS(广度优先搜索))

    Rescue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submis ...

  7. hdu - 1242 Rescue && hdu - 2425 Hiking Trip (优先队列+bfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1242 感觉题目没有表述清楚,angel的朋友应该不一定只有一个,那么正解就是a去搜索r,再用普通的bfs就能过了 ...

  8. HDU 1242 Rescue(BFS+优先队列)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1242 题目描述: Problem Description Angel was caught by t ...

  9. hdu 4848 搜索+剪枝 2014西安邀请赛

    http://acm.hdu.edu.cn/showproblem.php?pid=4848 比赛的时候我甚至没看这道题,事实上不难.... 可是说实话,如今对题意还是理解不太好...... 犯的错误 ...

随机推荐

  1. Redis错误解决:(error) MISCONF Redis is configured to save RDB snapshots

    刚开始学习使用redis数据库,在执行删除命令时,提示了我这么一个错误: 错误提示 (error) MISCONF Redis is configured to save RDB snapshots, ...

  2. 按键精灵安卓版 tap、touch命令 不好用的解决办法!

    用按键精灵手机版写脚本来操作新浪微博APP,在关注列表页自动取消关注,代码如下: If x > -1 And y > -1 Then delay 1000 tap x,y delay 10 ...

  3. Windows7_64位 NVIDIA 卡 OpenCl环境配置

    序 最近做一个项目需要用到OpenCL,由于之前没有接触过,所以在环境配置第一关就遇到了一些问题,查阅很多资料才配置完成,现在记录如下,希望给一些童鞋一些帮助. 整个步骤也很简单: 了解系统配置,选择 ...

  4. Cleaning Shifts POJ - 2376 (贪心题)

    Cleaning Shifts Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 31194   Accepted: 7677 ...

  5. 二叉苹果树——树形Dp(由根到左右子树的转移)

    题意:给出一个二叉树,每条边上有一定的边权,并且剪掉一些树枝,求留下 Q 条树枝的最大边权和. ( 节点数 n ≤100,留下的枝条树 Q ≤ n ,所有边权和 ∑w[i] ≤30000 ) 细节:对 ...

  6. Linux学习-Linux 的开机流程分析

    开机流程一览 系统开机的经过可以汇整成底下的流程的: 加载 BIOS 的硬件信息与进行自我测试,并依据设定取得第一个可开机的装置; 读取并执行第一个开机装置内 MBR 的 boot Loader (亦 ...

  7. JAVA-基础(一)

    1.一个变量可以声明为final,这样做的目的是阻止它的内容被修改.这意味着在声明final 变量的时候,你必须初始化它(在这种用法上,final类似于C/C++中的const). 例如: final ...

  8. PYday16&17-设计模式\选课系统习题

    1.设计模式:对程序做整体得规划设计,这样做是为了更好的实现功能,使代码的可扩展性更好有27种常见的设计模式.流行的设计模式参考书:GoF设计模式.大话设计模式设计模式是为了更好的实现模块间的解耦,便 ...

  9. STL学习笔记1--vector

    C++STL(Standard Template Library)标准模板库是通用类模板和算法的集合.包含一些标准的数据结构的实现如queues(队列),lists(链表),stacks(栈)等.ST ...

  10. Python全栈开发第二期课表

     day01-python 全栈开发-基础篇                 01 开课介绍 01:55:13 ★  02 开课介绍02 01:28:31 ★  03 开课介绍03 00:22:55 ...