题目链接: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 广搜的更多相关文章

  1. poj 3984:迷宫问题(广搜,入门题)

    迷宫问题 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7635   Accepted: 4474 Description ...

  2. hdu 1072 广搜(逃离爆炸迷宫)

    题意: 在n×m的地图上,0表示墙,1表示空地,2表示人,3表示目的地,4表示有定时炸弹重启器.定时炸弹的时间是6,人走一步所需要的时间是1.每次可以上.下.左.右移动一格.当人走到4时如果炸弹的时间 ...

  3. POJ 3984 迷宫问题 记录路径的广搜

    主要是学一下如何在广搜中记录路径:每找到一个点我就记录下这个点是由那个点得来的,这样我找到最后个点后,我就可以通过回溯得到我走过的路径,具体看代码吧~ #include<cstdio> # ...

  4. P3818 小A和uim之大逃离 II(bfs,有条件的广搜)

    题目背景 话说上回……还是参见 https://www.luogu.org/problem/show?pid=1373 吧 小a和uim再次来到雨林中探险.突然一阵南风吹来,一片乌云从南部天边急涌过来 ...

  5. HDU 1253 (简单三维广搜) 胜利大逃亡

    奇葩!这么简单的广搜居然爆内存了,而且一直爆,一直爆,Orz 而且我也优化过了的啊,尼玛还是一直爆! 先把代码贴上睡觉去了,明天再来弄 //#define LOCAL #include <ios ...

  6. 解救小哈——bfs广搜

    问题描述: 小哈去玩迷宫,结果迷路了,小哼去救小哈.迷宫由n行m列的单元格组成(n和m都小于等于50),每个单元格要么是空地,要么是障碍物. 问题:帮小哼找到一条从迷宫的起点通往小哈所在位置的最短路径 ...

  7. hdu 1253 胜利大逃亡 (广搜)

    题目链接 Problem Description Ignatius被魔王抓走了,有一天魔王出差去了,这可是Ignatius逃亡的好机会. 魔王住在一个城堡里,城堡是一个ABC的立方体,可以被表示成A个 ...

  8. HDU 1010 Tempter of the Bone (广搜+减枝)

    题目链接 Problem Description The doggie found a bone in an ancient maze, which fascinated him a lot. How ...

  9. poj 3131 Cubic Eight-Puzzle 双向广搜 Hash判重

    挺不错的题目,很锻炼代码能力和调试能力~ 题意:初始格子状态固定,给你移动后格子的状态,问最少需要多少步能到达,如果步数大于30,输出-1. 由于单向搜索状态太多,搜到二十几就会爆了,所以应该想到双向 ...

随机推荐

  1. 解决li中文字行高对齐

    <div class="A_content_1"> <li>座右铭: <input class="A_ct1" type=&quo ...

  2. php array_merge和运算符+

    其实很多时候我都很疑惑为什么同维度的数组不能直接使用运算+直接进行相加,然后结果就是两个数组合并的在一起的新结果,这个就有点跟array_merge合并函数类似了,接下来就来看下这两种合并的方式到底有 ...

  3. 奔小康赚大钱---hdu2255(最大带权匹配)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2255 带权匹配问题的模板: 运用KM算法: #include<stdio.h> #incl ...

  4. pandas 从txt读取DataFrame&DataFrame格式化保存到txt

    前提 首先保证你txt里的文本内容是有规律可循的(例如,列与列之间通过“\t”.“,”等指定的可识别分隔符分隔): 例如我需要读取的数据,(\t)分隔: (此文件内容是直接以DataFrame格式化写 ...

  5. Java char 和 String 的区别: 字符编码及其存储

    一. ASCII码 上个世纪60年代,美国制定了一套字符编码,对英语字符与二进制位之间的关系,做了统一规定.这被称为ASCII码,一直沿用至今.一个字节(8bit)一共 可以用来表示256种不同的状态 ...

  6. python os模块 os.chmod

    os.chmod() 方法用于更改文件或目录的权限. os.chmod(path, mode) 参数 path -- 文件名路径或目录路径. flags -- 可用以下选项按位或操作生成, 目录的读权 ...

  7. gitlab卸载

    1.停止gitlab gitlab-ctl stop 2.卸载gitlab(注意这里写的是gitlab-ce) rpm -e gitlab-ce 3.查看gitlab进程 ps aux | grep ...

  8. java之简单工厂

    1.使用步骤 创建抽象/接口产品类,定义具体产品的公共接口方法:(产品接口类) 创建具体产品类,是继承抽象产品类的:(产品接口实现类) 创建工厂类,通过创建静态方法根据传入不同参数从而创建不同具体产品 ...

  9. centos6 pip install python-ldap报错

    error: Setup script exited with error: command 'gcc' failed with exit status 1 解决方法: 原因是版本不兼容,centos ...

  10. React 函数传参

    import React, { Component } from 'react'; import { render } from 'react-dom'; class GroceryList exte ...