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. 由于单向搜索状态太多,搜到二十几就会爆了,所以应该想到双向 ...
随机推荐
- nginx+lua构建简单waf网页防火墙
需求背景 类似于论坛型的网站经常会被黑掉,除了增加硬件防护感觉效果还是不太好,还会偶尔被黑,waf的功能正好实现了这个需求. waf的作用: 防止sql注入,本地包含,部分溢出,fuzzing测试,x ...
- mysql 数据操作 单表查询 where 约束 目录
mysql 数据操作 单表查询 where约束 between and or mysql 数据操作 单表查询 where约束 is null in mysql 数据操作 单表查询 where约束 li ...
- android 模仿今日头条ViewPager+TabLayout
导入依赖库: compile 'com.android.support:design:25.3.1' 1.fg_content_demo2.xml <?xml version="1.0 ...
- tools-eclipse-004-UML图安装
git:https://github.com/takezoe/amateras-modeler 下载:http://sourceforge.jp/projects/amateras/downloads ...
- How To Mine Bitcoins 比特币挖矿
linux 下查看 gpu 的信息: sudo lshw -C display windows下查看cuda信息:In directory C:\Program Files\NVIDIA Corpor ...
- (转)通过HTTP RESTful API 操作elasticsearch搜索数据
样例数据集 这是编造的JSON格式银行客户账号信息文档,文档schema如下: { “account_number”: 0, “balance”: 16623, “firstname”: “Brads ...
- Amber安装并行
现在简单介绍一下amber12中安装openmpi并行的过程. 1. 下载openmpi版本在1.5-1.9之间的(openmpi-1.6.5.tar.bz2) 这是因为$AMBERHOME/Ambe ...
- AVAudioFoundation(6):时间和媒体表示
本文转自:AVAudioFoundation(6):时间和媒体表示 | www.samirchen.com 本文主要内容来自 AVFoundation Programming Guide. 基于时间的 ...
- opencv-ios开发笔记9 使用透视变换矫正扭曲的图片
http://blog.csdn.net/baixiaozhe/article/details/51762086 摄像头观察一个矩形的图片时往往只能得到一个扭曲的图片: 原图: 实际情况是摄像头经常从 ...
- 结合grabcut和inpaint,实现人像去除
在OpenCV提供更多函数中,grabcut能够实现抠图,inpaint能够实现修补.那么把两者结合起来,就能够实现简单的“人像去除”功能,也就是框选一个人后,使用周围的景象对人像进行修补.虽然效果比 ...