hdu Rescue 1242
Rescue
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11566 Accepted Submission(s): 4205
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.
#.#####.
#.a#..r.
#..#x...
..#..#.#
#...##..
.#......
........
/*
广搜
检查了很久 最后 被困死的时候是 0,
if(visit[i][j]<num && visit[i][j]!=0)
考虑了一些情况,题意很清晰,有多个r。一个a么?应该是。但是我没有处理。
由于x的存在使得 到达各点的时间可能存在多样,也加进去比较了。
但是之前写的dfs,没有考虑过这样的情况。
*/
#include<stdio.h>
#include<stdlib.h>
#define HH 11111111
char a[][];
int map[][]={{,},{,},{,-},{-,}};
int zhan[],len;
int visit[][];
int n,m;
void bfs(int x,int y)
{
int i,x1,y1;
zhan[++len]=x;
zhan[++len]=y;
visit[x][y]=;
while(len>)
{
y=zhan[len--];
x=zhan[len--];
for(i=;i<;i++)
{
x1=x+map[i][];
y1=y+map[i][];
if(x1>=&&x1<=n && y1>=&&y1<=m)
{
if(visit[x1][y1]== && a[x1][y1]!='#')
{
if(a[x1][y1]=='.'||a[x1][y1]=='r')
visit[x1][y1]=visit[x][y]+;
else if(a[x1][y1]=='x')
visit[x1][y1]=visit[x][y]+;
zhan[++len]=x1;
zhan[++len]=y1;
}
if(visit[x1][y1]> && a[x1][y1]!='#')
{
if((a[x1][y1]=='.'||a[x1][y1]=='r')&&visit[x1][y1]>visit[x][y]+)
{
visit[x1][y1]=visit[x][y]+;
zhan[++len]=x1;
zhan[++len]=y1;
}
if(a[x1][y1]=='x' && visit[x1][y1]>visit[x][y]+)
{
visit[x1][y1]=visit[x][y]+;
zhan[++len]=x1;
zhan[++len]=y1;
}
}
}
}
}
}
int main()
{
int i,j,num;
while(scanf("%d%d",&n,&m)>)
{
for(i=;i<=n;i++)
scanf("%s",a[i]+);
for(i=;i<=n;i++)
for(j=;j<=m;j++)
visit[i][j]=;
for(i=;i<=n;i++)
for(j=;j<=m;j++)
{
if(a[i][j]=='a')
{
len=;
bfs(i,j);
}
}
num=HH;
for(i=;i<=n;i++)
for(j=;j<=m;j++)
if(a[i][j]=='r')
{
if(visit[i][j]<num && visit[i][j]!=)
num=visit[i][j];
}
if(num==HH) printf("Poor ANGEL has to stay in the prison all his life.\n");
else printf("%d\n",num);
}
return ;
}
单纯的广搜,在浙大oj超时.... 蒋神却过了,思想很厉害。
/*
优先队列
*/ #include<stdio.h>
#include<iostream>
#include<cstdlib>
#include<string.h>
#include<queue>
#define HH 11111111
using namespace std;
char a[][];
int visit[][];
int n,m;
int map[][]={{,},{,},{-,},{,-}};
struct node
{
friend bool operator< (node n1,node n2)
{
return n1.p>n2.p;
}
int p;
int x;
int y;
};
void bfs(int x,int y)
{
int i,x1,y1;
priority_queue<node>b;
while(!b.empty())
{
b.pop();
}
node tmp,tmp1;
tmp.x=x;
tmp.y=y;
tmp.p=;
b.push(tmp);
visit[x][y]=;
while(b.size()>)
{
tmp=b.top();
b.pop();
for(i=;i<;i++)
{
x1=tmp.x+map[i][];
y1=tmp.y+map[i][];
if(x1>=&&x1<=n && y1>=&&y1<=m && visit[x1][y1]== && a[x1][y1]!='#')
{
if(a[x1][y1]=='x')
visit[x1][y1]=tmp.p+;
else if(a[x1][y1]=='.' || a[x1][y1]=='r')
visit[x1][y1]=tmp.p+;
tmp1=tmp;
tmp.x=x1;
tmp.y=y1;
tmp.p=visit[x1][y1];
b.push(tmp);
tmp=tmp1;
if(a[x1][y1]=='r')return;
}
}
}
}
int main()
{
int i,j,num;
while(scanf("%d%d",&n,&m)>)
{
for(i=;i<=n;i++)
scanf("%s",a[i]+);
memset(visit,,sizeof(visit));
for(i=;i<=n;i++)
for(j=;j<=m;j++)
{
if(a[i][j]=='a')
{
bfs(i,j);
}
}
num=HH;
for(i=;i<=n;i++)
for(j=;j<=m;j++)
{
if(a[i][j]=='r' && visit[i][j]!= && visit[i][j]<num)
num=visit[i][j];
}
if(num==HH)
printf("Poor ANGEL has to stay in the prison all his life.\n");
else
printf("%d\n",num);
}
return ;
}
hdu Rescue 1242的更多相关文章
- hdu Rescue (bfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1242 简单优先队列搜索,自己好久不敲,,,,,手残啊,,,,orz 代码: #include < ...
- hdu Rescue
因为要求的是最少的时间,很明显的是一个利用优先队列的bfs的题目,题目很一般. #include"iostream" #include"algorithm" # ...
- hdu 1242 Rescue
题目链接:hdu 1242 这题也是迷宫类搜索,题意说的是 'a' 表示被拯救的人,'r' 表示搜救者(注意可能有多个),'.' 表示道路(耗费一单位时间通过),'#' 表示墙壁,'x' 代表警卫(耗 ...
- 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)
此刻再看优先队列,不像刚接触时的那般迷茫!这也许就是集训的成果吧! 加油!!!优先队列必须要搞定的! 这道题意很简单!自己定义优先级别! +++++++++++++++++++++++++++++++ ...
- 杭电 HDU 1242 Rescue
http://acm.hdu.edu.cn/showproblem.php?pid=1242 问题:牢房里有墙(#),警卫(x)和道路( . ),天使被关在牢房里位置为a,你的位置在r处,杀死一个警卫 ...
- HDU 1242 Rescue(优先队列)
题目来源: http://acm.hdu.edu.cn/showproblem.php?pid=1242 题目描述: Problem Description Angel was caught by ...
- HDU 1242 Rescue(BFS+优先队列)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1242 题目描述: Problem Description Angel was caught by t ...
- HDU 1242 -Rescue (双向BFS)&&( BFS+优先队列)
题目链接:Rescue 进度落下的太多了,哎╮(╯▽╰)╭,渣渣我总是埋怨进度比别人慢...为什么不试着改变一下捏.... 開始以为是水题,想敲一下练手的,后来发现并非一个简单的搜索题,BFS做肯定出 ...
随机推荐
- Python 获取主机名
import socket print socket.gethostname()
- python小数的进位与舍去
一.基础知识准备 奇进偶舍,又称为四舍六入五成双规则.银行进位法(Banker's Rounding),是一种计数保留法,是一种数值修约规则.从统计学的角度,"奇进偶舍"比&q ...
- leetcode 90. 子集 II JAVA
题目: 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: [1,2,2] 输出: [ [2], [1], [1,2,2] ...
- 盗墓笔记—阿里旺旺ActiveX控件imageMan.dll栈溢出漏洞研究
本文作者:i春秋作家——cq5f7a075d 也许现在还研究Activex就是挖坟,但是呢,笔者是摸金校尉,挖坟,呸!盗墓是笔者的本职工作. 额,不扯了,本次研究的是阿里旺旺ActiveX控件imag ...
- [Python]字典Dictionary、列表List、元组Tuple差异化理解
概述:Python中这三种形式的定义相近,易于混淆,应注意区分. aDict={'a':1, 'b':2, 'c':3, 'd':4, 'e':5} aList=[1,2,3,4,5] aTuple= ...
- redis cluster 添加 删除 重分配 节点
redis cluster配置好,并运行一段时间后,我们想添加节点,或者删除节点,该怎么办呢. 一,redis cluster命令 //集群(cluster) CLUSTER INFO 打印集群的信 ...
- js面试题-数组去重
今天,在聊天群里看到数组去重的话题,面试者的答案如下: 参考答案如下: 程序员思维,做出如下测试: 未考虑到:1,‘1’是不同的,应该不去重 未考虑到对象 所以,参考答案只能去重基础类型 根据以往看过 ...
- Adapter as a WCF Binding - In Depth
WCF LOB Adapter SDK surfaces an adapter as a custom WCF Binding. A WCF Bindingcorresponds to the “H ...
- Highcharts纯Javascript图表使用讲解
Highcharts提供大量的选项配置参数,您可以轻松定制符合用户要求的图表,目前官网只提供英文版的开发配置说明文档,而中文版的文档网上甚少,且零散不全.这里,我把Highcharts常用的最核心的参 ...
- iOS 模拟不同的字体大小
 真的是神器!! 参考 Creating Self-Sizing Table View Cells