hrbust 1621 迷宫问题II 广搜
题目链接:http://acm.hrbust.edu.cn/vj/index.php?/vj/index.php?c=&c=contest-contest&cid=134#problem/7
很简单的广搜题。依然没有顺利的1A。没用优先队列。搞不清是不是还要回溯一下?【啊哈哈。我就是这么想的。】
// hrbust 1621 迷宫问题II
// 优先队列——广搜
// 什么时候需要回溯什么时候不需要? #include <stdio.h>
#include <string.h>
#include <iostream>
#include <queue>
using namespace std;
#define maxn 100000 char mp[][];
int dir[][] = {, , -, , , , , -};
int vis[][];
int r, c;
int step[][];
int stx, sty, edx, edy; bool check(int x, int y) {
if (x >= && x < r && y >= && y < c && !vis[x][y] && mp[x][y] != '#')
return true;
return false;
} struct Node {
friend bool operator < (Node n1, Node n2)
{
return step[n1.x][n1.y] > step[n2.x][n2.y];//"<"为从大到小排列,">"为从小打到排列
}
int x, y;
}node[]; priority_queue<Node> qn; void bfs(int x, int y) {
int head = , tail = ;
memset(vis, , sizeof(vis));
for (int i=; i<r; ++i) {
for (int j=; j<c; ++j) {
step[i][j] = maxn;
}
}
while(!qn.empty()) {
qn.pop();
} vis[x][y] = ;
step[x][y] = ;
Node temp;
temp.x = x;
temp.y = y;
// node[tail++] = temp;
qn.push(temp);
//while(head < tail) {
while(!qn.empty()) {
//Node now = node[head++];
Node now = qn.top();
qn.pop();
for (int i=; i<; ++i) {
temp.x = now.x + dir[i][];
temp.y = now.y + dir[i][];
int tstep = ;
if (check(temp.x, temp.y)) {
if (mp[temp.x][temp.y] >= '' && mp[temp.x][temp.y] <= '')
tstep = mp[temp.x][temp.y] - '' + ;
else tstep = ;
step[temp.x][temp.y] = min(step[temp.x][temp.y], step[now.x][now.y] + tstep);
vis[temp.x][temp.y] = ;
//node[tail++] = temp;
qn.push(temp);
if (temp.x == edx && temp.y == edy) break;
}
}
}
return;
} int main() {
int t;
cin >> t;
while(t--) {
cin >> r >> c; for (int i=; i<r; ++i) {
for (int j=; j<c; ++j) {
cin >> mp[i][j];
if (mp[i][j] == 'Z') {
stx = i, sty = j;
}
else if (mp[i][j] == 'W') {
edx = i, edy = j;
}
}
} bfs(stx, sty);
int ans = step[edx][edy];
if (ans == maxn) {
cout << "IMPOSSIBLE\n";
}
else cout << ans << endl;
}
return ;
}
hrbust 1621 迷宫问题II 广搜的更多相关文章
- poj 3984:迷宫问题(广搜,入门题)
迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7635 Accepted: 4474 Description ...
- hdu 1072 广搜(逃离爆炸迷宫)
题意: 在n×m的地图上,0表示墙,1表示空地,2表示人,3表示目的地,4表示有定时炸弹重启器.定时炸弹的时间是6,人走一步所需要的时间是1.每次可以上.下.左.右移动一格.当人走到4时如果炸弹的时间 ...
- POJ 3984 迷宫问题 记录路径的广搜
主要是学一下如何在广搜中记录路径:每找到一个点我就记录下这个点是由那个点得来的,这样我找到最后个点后,我就可以通过回溯得到我走过的路径,具体看代码吧~ #include<cstdio> # ...
- P3818 小A和uim之大逃离 II(bfs,有条件的广搜)
题目背景 话说上回……还是参见 https://www.luogu.org/problem/show?pid=1373 吧 小a和uim再次来到雨林中探险.突然一阵南风吹来,一片乌云从南部天边急涌过来 ...
- HDU 1253 (简单三维广搜) 胜利大逃亡
奇葩!这么简单的广搜居然爆内存了,而且一直爆,一直爆,Orz 而且我也优化过了的啊,尼玛还是一直爆! 先把代码贴上睡觉去了,明天再来弄 //#define LOCAL #include <ios ...
- 解救小哈——bfs广搜
问题描述: 小哈去玩迷宫,结果迷路了,小哼去救小哈.迷宫由n行m列的单元格组成(n和m都小于等于50),每个单元格要么是空地,要么是障碍物. 问题:帮小哼找到一条从迷宫的起点通往小哈所在位置的最短路径 ...
- hdu 1253 胜利大逃亡 (广搜)
题目链接 Problem Description Ignatius被魔王抓走了,有一天魔王出差去了,这可是Ignatius逃亡的好机会. 魔王住在一个城堡里,城堡是一个ABC的立方体,可以被表示成A个 ...
- HDU 1010 Tempter of the Bone (广搜+减枝)
题目链接 Problem Description The doggie found a bone in an ancient maze, which fascinated him a lot. How ...
- poj 3131 Cubic Eight-Puzzle 双向广搜 Hash判重
挺不错的题目,很锻炼代码能力和调试能力~ 题意:初始格子状态固定,给你移动后格子的状态,问最少需要多少步能到达,如果步数大于30,输出-1. 由于单向搜索状态太多,搜到二十几就会爆了,所以应该想到双向 ...
随机推荐
- Python开发【Django】:缓存、信号
缓存 由于Django是动态网站,所有每次请求均会去数据进行相应的操作,当程序访问量大时,耗时必然会更加明显,最简单解决方式是使用:缓存,缓存将一个某个views的返回值保存至内存或者memcache ...
- 大话https演化过程(对称加密、非对称加密、公钥、私钥、数字签名、数字证书)
大话https演化过程(包括概念:对称加密.非对称加密.公钥.私钥.数字签名.数字证书.https访问全过程) 在网络上发送数据是非常不安全的,非常容易被劫持或是被篡改,所以每次定向发送数据你都可 ...
- Ubuntu18.04 英文系统下安装中文输入法
今天尝试了Ubuntu18.04LTS(依旧装的英文版)发现按照之前的方法( http://www.cnblogs.com/asmer-stone/p/5227188.html)安装中文输入法不行了, ...
- api收录
ip地址查询api http://ip.taobao.com/service/getIpInfo.php?ip= 如: http://ip.taobao.com/service/getIpInfo.p ...
- matplotlib常见绘图基础代码小结:折线图、散点图、条形图、直方图、饼图
一.折线图 二.散点图 三.条形图 四.直方图 五.饼图 一.折线图折线图用于显示随时间或有序类别的变化趋势 from matplotlib import pyplot as plt x = rang ...
- (1.4)DML增强功能-Output
Output在CRUD的区别 1.对于INSERT,可以引用inserted表以查询新行的属性.在insert into table output . 2.对于DELETE,可以引用deleted表以 ...
- 模仿Masonary写一个计算器
1.CaculatorMaker @interface CaculatorMaker : NSObject @property(nonatomic,assign)int result; -(Cacul ...
- rewrite or internal redirection cycle while processing "/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/jenkins/
折腾了很久,跟nginx配置没有关系.最终是把php版本从7.1降到5.6才解决的,是跟tp3.2匹配的
- C++语言中的四种类型转换
1 引子 这篇笔记是根据StackOverflow上面的一个问题整理而成,主要内容是对C/C++当中四种类型转换操作进行举例说明.在之前其实对它们都是有所了解的,而随着自己在进行总结,并敲了一些测试示 ...
- Java StringBuffer 和 StringBuilder 类
当对字符串进行修改的时候,需要使用 StringBuffer 和 StringBuilder 类. 和 String 类不同的是,StringBuffer 和 StringBuilder 类的对象能够 ...