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做肯定出 ...
随机推荐
- 弦论(tjoi2015,bzoj3998)(sam(后缀自动机))
对于一个给定长度为\(N\)的字符串,求它的第\(K\)小子串是什么. Input 第一行是一个仅由小写英文字母构成的字符串\(S\) 第二行为两个整数\(T\)和\(K\),\(T\)为0则表示不同 ...
- Heap-451. Sort Characters By Frequency
Given a string, sort it in decreasing order based on the frequency of characters. Example 1: Input: ...
- IntelliJ IDEA 2016 破解旗舰版
JRebel 授权服务器http://idea.lanyus.com/ilanyulanyu19950316@gmail.com
- jdbc连接1(可以注入)
package demo3class; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Prepa ...
- c++类定义和类实现
预备知识: c++中我们cpp文件和.h文件的区别是,cpp文件是需要编译的文件,成为一个独立的编译单元,而h文件从来是不需要编译,只是用于预处理. 通常我们在cpp文件中,完成函数的实现,然后在h中 ...
- leetcode-914-卡牌分组
题目描述: 给定一副牌,每张牌上都写着一个整数. 此时,你需要选定一个数字 X,使我们可以将整副牌按下述规则分成 1 组或更多组: 每组都有 X 张牌. 组内所有的牌上都写着相同的整数. 仅当你可选的 ...
- 【ARC072F】 Dam 单调队列
题目大意: 有一个水库,容量为$L$,一开始是空的.有$n$天. 对于第i天,每天早上有$v_i$单位的,水温为$t_i$的水流进来.每天晚上你可以放掉一些水,多少自定.但是必须保证第二天水库不会溢出 ...
- Flowable BPMN 简单使用
1.Flowable是什么? Flowable是一个使用Java编写的轻量级业务流程引擎.Flowable流程引擎可用于部署BPMN 2.0流程定义(用于定义流程的行业XML标准), 创建这些流程定义 ...
- ContentProvider类的设计分析
ContentProvider的类设计很好,Transport作为成员存在,完成Binder的功能,有点像组合模式,把完成转发/通信功能 封装为一个内部类,便于转发外部调用给外部类,这种设计在Andr ...
- python 把一文件包含中文的字符写到另外文件乱码 UnicodeDecodeError: 'gbk' codec can't decode byte 0xac in position
报错的代码是: file2 = open('target.txt','w')for line in open('test.txt'): file2.write(line)原因:文件编码不一致导致解决方 ...