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. 概括的描述一下Spring注册流程

    Spring经过大神们的构思.编码,日积月累而来,所以,对其代码的理解也不是一朝一夕就能快速完成的.源码学习是枯燥的,需要坚持!坚持!坚持!当然也需要技巧,第一遍学习的时候,不用关注全部细节,不重要的 ...

  2. k8s的资源限制及资源请求

    容器的资源需求及限制:  需求:requests   ##定义容器运行时至少需要资源  限制:limits     ##定义容器运行时最多能分配的资源    requests:pod.spec.con ...

  3. python 爬取知乎图片

    先上完整代码 import requests import time import datetime import os import json import uuid from pyquery im ...

  4. 日志切割logrotate和定时任务crontab详解

    1.关于日志切割 日志文件包含了关于系统中发生的事件的有用信息,在排障过程中或者系统性能分析时经常被用到.对于忙碌的服务器,日志文件大小会增长极快,服务器会很快消耗磁盘空间,这成了个问题.除此之外,处 ...

  5. Laravel核心解读--Console内核

    Console内核 上一篇文章我们介绍了Laravel的HTTP内核,详细概述了网络请求从进入应用到应用处理完请求返回HTTP响应整个生命周期中HTTP内核是如何调动Laravel各个核心组件来完成任 ...

  6. leetcode-21-knapsack

    322. Coin Change Write a function to compute the fewest number of coins that you need to make up tha ...

  7. cf 1006E

    #include <iostream> #include <cstdio> #include <cstring> #include <string> # ...

  8. jmeter throughput controller

    工作方式:可以按规定次数执行,也可以选择按百分比执行,其中的百分比必须是10,20,30类似的整数. 使用场景:可以随机的去按百分比浏览网址. 以下是具体脚本:

  9. JSONP分享-- 在JavaScript中跨域请求

    如果你正在开发一个现代的基于web的应用程序,那么你: 在客户端使用JavaScript. 需要集成那些没有完全在你控制之下的服务(或者那些来自不同的域). 在你的浏览器控制台中遇到过这个错误信息: ...

  10. WordPress后台添加侧边栏菜单

    add_action('admin_menu', 'register_custom_menu_page'); function register_custom_menu_page() { add_me ...