HDOJ1242(延时迷宫BFS+优先队列)
Rescue
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 25485 Accepted Submission(s): 9022
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.
- #include <iostream>
- #include <queue>
- using namespace std;
- const int MAXN=;
- const int INF=0x3f3f3f3f;
- struct Node{
- int y,x,step;
- Node(){}
- Node(int y,int x,int step)
- {
- this->y=y;
- this->x=x;
- this->step=step;
- }
- bool operator<(const Node &no)const
- {
- return step > no.step;
- }
- };
- int n,m;
- char mz[MAXN][MAXN];
- int t[MAXN][MAXN];
- int dy[]={,,,-};
- int dx[]={,,-,};
- int sy,sx;
- int gy,gx;
- void bfs()
- {
- for(int i=;i<MAXN;i++)
- for(int j=;j<MAXN;j++) t[i][j]=INF;
- priority_queue<Node> que;
- que.push(Node(sy,sx,));
- t[sy][sx]=;
- while(!que.empty())
- {
- Node now=que.top();que.pop();
- if(now.y==gy&&now.x==gx)
- {
- cout<<now.step<<endl;
- return ;
- }
- for(int i=;i<;i++)
- {
- int ny=now.y+dy[i];
- int nx=now.x+dx[i];
- if(<=ny&&ny<n&&<=nx&&nx<m&&mz[ny][nx]!='#')
- {
- int nstep;
- if(mz[ny][nx]=='x') nstep=now.step+;
- else nstep=now.step+;
- if(nstep<t[ny][nx])
- {
- t[ny][nx]=nstep;
- que.push(Node(ny,nx,nstep));
- }
- }
- }
- }
- cout<<"Poor ANGEL has to stay in the prison all his life."<<endl;
- }
- int main()
- {
- while(cin>>n>>m)
- {
- for(int i=;i<n;i++)
- for(int j=;j<m;j++)
- {
- cin>>mz[i][j];
- if(mz[i][j]=='a')
- {
- sy=i;
- sx=j;
- }
- else if(mz[i][j]=='r')
- {
- gy=i;
- gx=j;
- }
- else ;
- }
- bfs();
- }
- return ;
- }
HDOJ1242(延时迷宫BFS+优先队列)的更多相关文章
- HDU1026(延时迷宫:BFS+优先队列)
Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- hdu 1728 逃离迷宫 BFS加优先队列 DFS()
http://acm.hdu.edu.cn/showproblem.php?pid=1728 题意就是能否在规定的转弯次数内从起点走到终点.刚走时那步方向不算. 只会bfs(),但想到这题需要记录转弯 ...
- BFS+优先队列+状态压缩DP+TSP
http://acm.hdu.edu.cn/showproblem.php?pid=4568 Hunter Time Limit: 2000/1000 MS (Java/Others) Memo ...
- hdu 2102 A计划 具体题解 (BFS+优先队列)
题目链接:pid=2102">http://acm.hdu.edu.cn/showproblem.php?pid=2102 这道题属于BFS+优先队列 開始看到四分之中的一个的AC率感 ...
- POJ 1724 ROADS(BFS+优先队列)
题目链接 题意 : 求从1城市到n城市的最短路.但是每条路有两个属性,一个是路长,一个是花费.要求在花费为K内,找到最短路. 思路 :这个题好像有很多种做法,我用了BFS+优先队列.崔老师真是千年不变 ...
- hdu 1242 找到朋友最短的时间 (BFS+优先队列)
找到朋友的最短时间 Sample Input7 8#.#####. //#不能走 a起点 x守卫 r朋友#.a#..r. //r可能不止一个#..#x.....#..#.##...##...#.... ...
- HDU 1428 漫步校园 (BFS+优先队列+记忆化搜索)
题目地址:HDU 1428 先用BFS+优先队列求出全部点到机房的最短距离.然后用记忆化搜索去搜. 代码例如以下: #include <iostream> #include <str ...
- hdu1839(二分+优先队列,bfs+优先队列与spfa的区别)
题意:有n个点,标号为点1到点n,每条路有两个属性,一个是经过经过这条路要的时间,一个是这条可以承受的容量.现在给出n个点,m条边,时间t:需要求在时间t的范围内,从点1到点n可以承受的最大容量... ...
- POJ - 2312 Battle City BFS+优先队列
Battle City Many of us had played the game "Battle city" in our childhood, and some people ...
随机推荐
- centos出现-bash: /usr/bin/php: 没有那个文件或目录解决方法
造成这个的原因是因为找不到php的执行文件导致的,原先我是安装的php5.4,然后卸载了重新安装php7,导致php可执行文件没有放到$PATH中,可以在终端测试:php -v,如果报错bash: / ...
- 我的Android进阶之旅------>android中getLocationInWindow 和 getLocationOnScreen的区别
View.getLocationInWindow(int[] location) 一个控件在其父窗口中的坐标位置 View.getLocationOnScreen(int[] location) 一个 ...
- 解决IntelliJ IDEA无法读取配置文件的问题(转发:https://www.cnblogs.com/Sinte-Beuve/p/5730572.html)
最近在学Mybatis,按照视频的讲解在项目的某个包里建立配置文件,然后读取配置文件,但是一直提示异常. 读取配置文件的为官方代码: String resource = "mybatis-c ...
- Javaweb--- EL表达式 JSTL标准标签库
一.EL表达式(expression language): 语法 ${...} jsp中page指令有一个属性叫isELIgnored, 用来标记此页面是否忽略EL表达式, 默认为false 举个例 ...
- C#读取excel 找不到可安装的ISAM
实在没有办法了 就仔细的查看了 一下数据链接字符串: string strConn = "Provider=Microsoft.Jet.Oledb.4.0;Data Source=" ...
- Django模型系统——ORM表结构对应关系
对于数据库来说一般表结构只会有三种对应关系,分别是一对一.一对多和多对一,下面分别介绍: 1.一对多 何为一对多,例如一个学生只可能有一个班级,一个班级却又多个学生,班级表和学生表就是一对多的关系. ...
- [算法]String to Integer(atoi)
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- <linux报错解决>在Fedora21下安装vmware报错的解决办法
关于VMWARE WORKSTATION在Fedora21下的安装问题 (1)在Fedora21下安装vmware如果在终端下启动,提示你找不到内核头文件Kernel Headers的话使用命令: s ...
- 使用 sqoop 将mysql数据导入到hdfs(import)
Sqoop 将mysql 数据导入到hdfs(import) 1.创建mysql表 CREATE TABLE `sqoop_test` ( `id` ) DEFAULT NULL, `name` va ...
- vs2015配置boost c++
参考:https://blog.csdn.net/zengraoli/article/details/70187556 https://blog.csdn.net/misterfm/article/d ...