hdu1242 优先队列+bfs
Rescue
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 22034 Accepted Submission(s): 7843
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...
..#..#.#
#...##..
.#......
........
#include <stdio.h>
#include <string.h>
#include <queue>
#include <algorithm>
using namespace std; struct Point
{
int x,y,t;
bool operator < (const Point &a)const
{
return t>a.t;
}
}; char map[][];
Point start;
int n,m;
int diect[][]={{,},{-,},{,},{,-}}; int bfs()
{
priority_queue<Point> que;
Point cur,next;
int i,j; map[start.x][start.y]='#';
que.push(start);
while(!que.empty())
{
cur=que.top();
que.pop();
for(i=;i<;i++)
{
next.x=cur.x+diect[i][];
next.y=cur.y+diect[i][];
next.t=cur.t+;
if(next.x< || next.x>=n || next.y< || next.y>=m)
continue;
if(map[next.x][next.y]=='#')
continue;
if(map[next.x][next.y]=='r')
return next.t;
if(map[next.x][next.y]=='.')
{
map[next.x][next.y]='#';
que.push(next);
}
else if(map[next.x][next.y]=='x')
{
map[next.x][next.y]='#';
next.t++;
que.push(next);
}
}
}
return -;
} int main()
{
int i,j,ans;
char *p;
while(scanf("%d %d",&n,&m)!=EOF)
{
for(i=;i<n;i++)
{
scanf("%s",map[i]);
if(p=strchr(map[i],'a'))
{
start.x=i;
start.y=p-map[i];
start.t=;
}
}
ans=bfs();
if(ans==-)
printf("Poor ANGEL has to stay in the prison all his life.\n");
else
printf("%d\n",ans);
}
return ;
}
hdu1242 优先队列+bfs的更多相关文章
- hdu 1026 Ignatius and the Princess I【优先队列+BFS】
链接: http://acm.hdu.edu.cn/showproblem.php?pid=1026 http://acm.hust.edu.cn/vjudge/contest/view.action ...
- ZOJ 649 Rescue(优先队列+bfs)
Rescue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- 【POJ3635】Full Tank 优先队列BFS
普通BFS:每个状态只访问一次,第一次入队时即为该状态对应的最优解. 优先队列BFS:每个状态可能被更新多次,入队多次,但是只会扩展一次,每次出队时即为改状态对应的最优解. 且对于优先队列BFS来说, ...
- Codeforces 677D - Vanya and Treasure - [DP+优先队列BFS]
题目链接:http://codeforces.com/problemset/problem/677/D 题意: 有 $n \times m$ 的网格,每个网格上有一个棋子,棋子种类为 $t[i][j] ...
- POJ 2449 - Remmarguts' Date - [第k短路模板题][优先队列BFS]
题目链接:http://poj.org/problem?id=2449 Time Limit: 4000MS Memory Limit: 65536K Description "Good m ...
- HDU1242 Rescue(BFS+优先队列)
Rescue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- hdu1242 Rescue bfs+优先队列
直接把Angle的位置作为起点,广度优先搜索即可,这题不是步数最少,而是time最少,就把以time作为衡量标准,加入优先队列,队首就是当前time最少的.遇到Angle的朋友就退出.只需15ms A ...
- Rescue HDU1242 (BFS+优先队列) 标签: 搜索 2016-05-04 22:21 69人阅读 评论(0)
Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is describe ...
- hdu1242 Rescue(BFS +优先队列 or BFS )
http://acm.hdu.edu.cn/showproblem.php?pid=1242 题意: Angel被传说中神秘的邪恶的Moligpy人抓住了!他被关在一个迷宫中.迷宫的长.宽不超 ...
随机推荐
- BizTalk开发系列(二十九) 宏的使用
在BizTalk中可以使用宏集合动态的让BizTalk发送处理程序使用单独的值来替换宏.常用的使用宏的发送程序有:文件发送适配器和SMTP发送适 配器.在表达式中可以使用同时使用多个宏.例如:在文件发 ...
- IOS第18天(2,CALayer自定义图层)
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typica ...
- rados命令
chen@admin-node:/etc/ceph$ rados --help usage: rados [options] [commands] POOL COMMANDS lspools list ...
- Pinyin 输入法安装 opensuse 13 gnome
1 安装 拼音输入法 zypper in pinyin (scim 包含) 2 安装包 scim,scim-m17n,scim-pinyin,scim-qtimm,scim-tables,s ...
- Visual Studio中编写C程序
相信很多科班出身的程序猿和我一样,第一个接触到的编程语言是C语言,第一个写的程序是“Hello World!”. 对于一个.Net程序猿,VS肯定是个非常熟悉的工具,但是如何使用VS编写一个C语言程序 ...
- C#中判断字符是否大写
在C#中,通常判断一个字符是否为大写字母,有些人可能会第一时间想到用正则表达式,那除了正则表达式,是否还有其他方式呢? 答案是肯定的,先一睹为快,具体代码如下: using System; using ...
- 《Linux内核分析》第七周 读书笔记
<深入理解计算机系统>CHAPTER7阅读梳理 [学习时间:3hours] [学习内容:链接需要的代码&数据:链接机制:链接生成的目标文件] 一.链接概述 1.链接 定义:链接是将 ...
- [LeetCode]题解(python):120 Triangle
题目来源 https://leetcode.com/problems/triangle/ Given a triangle, find the minimum path sum from top to ...
- oracle 用户锁定及到期
select * from dba_users where username='HR'--查询用户状态 alter user HR identified by HR;--重新更新密码alter use ...
- php日期时间函数和数学函数
<?php //第一部分:日期和时间函数 ----------------------------- time(); //int time(void),返回当前时间的时间戳 mktime(); ...