题目链接: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 ;
}

最新文章

  1. 11、项目经理要阅读的书籍 - IT软件人员书籍系列文章
  2. EasyUI中在表单提交之前进行验证
  3. PHP : Reflection API
  4. MVC5为WebAPI添加命名空间的支持
  5. Nginx 笔记与总结(15)nginx 实现反向代理 ( nginx + apache 动静分离)
  6. php获取某年某月的天数 【转】
  7. hdu 1031 (partial sort problem, nth_element, stable_partition, lambda expression) 分类: hdoj 2015-06-15 17:47 26人阅读 评论(0) 收藏
  8. Struts2的简单案例
  9. 遍历、显示ftp下的文件夹和文件信息
  10. GitHub Top 100 简介
  11. dtree实现上下级关系的显示
  12. vuejs+nodejs支持服务端渲染的博客系统
  13. (转).tar.gz文件和.rpm文件的区别
  14. dotnetcore ueditor
  15. 设置光标聚焦输入框(EditText)并弹出软键盘(在适配器中设置)
  16. 在 2016 年学 JavaScript 是一种什么样的体验?(React从入门到放弃)
  17. ios中NSObject分类(2)
  18. 实习培训——Servlet(6)
  19. hdu-4289 最大流Dinic模板题
  20. Java基础教程(10)--类

热门文章

  1. elk----es settings--logstash--performance---bigdesk---logstash plugin online/offline
  2. 比特币BTC全节点搭建
  3. 流畅的python 读书笔记 第二章 序列构成的数组 列表推导
  4. Openstack(九)部署nova服务(控制节点)
  5. log4j2配置日志大小,个数等
  6. 2.2 The Object Model -- Reopening Classes and Instances
  7. zw版【转发&#183;台湾nvp系列Delphi例程】HALCON SqrtImage
  8. C++ Primer 5th Edition自学笔记(1)
  9. springboot+mybatis项目自动生成
  10. js 变量、函数提升 与js的预编译有关