POJ3984 迷宫问题
典型BFS。
#include <iostream>
#include <memory.h>
#include <queue>
#include <map> #define LEN 5
using namespace std; int graph[LEN][LEN];
int ans = 0;
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1}; void printAns(map<int, int> &visit, int p) {
if (p == -1) return;
int prev = visit[p];
printAns(visit, prev);
int x = p / 5;
int y = p - x * 5;
cout << "(" << x << ", " << y << ")" << endl;
} int main()
{
memset(graph, 0, sizeof(graph));
int n = 5;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> graph[i][j];
}
}
queue<int> que; // (3,4) is 3*5+4 = 19
map<int, int> visit; // points to it's prev
que.push(0);
visit[0] = -1;
int dest = 4 * 5 + 4;
while (!que.empty()) {
int point = que.front();
que.pop();
if (point == dest) break;
int x = point / 5;
int y = point - x * 5;
for (int i = 0; i < 4; i++) {
int next_x = x + dx[i];
int next_y = y + dy[i];
if (next_x >= 5 || next_x < 0 || next_y >=5 || next_y < 0) continue;
if (graph[next_x][next_y] == 1) continue;
int key = next_x * 5 + next_y;
if (visit.count(key) != 0) continue;
visit[key] = point;
que.push(key);
}
} // print ans from dest
printAns(visit, dest);
return 0;
}
POJ3984 迷宫问题的更多相关文章
- poj3984迷宫问题 广搜+最短路径+模拟队列
转自:http://blog.csdn.net/no_retreats/article/details/8146585 定义一个二维数组: int maze[5][5] = { 0, 1, 0, ...
- poj3984迷宫问题
一个5 × 5的二维数组,表示一个迷宫.其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线. 很简单的一道题,迷宫问题,一般都选择两种优先搜索 ...
- [poj3984]迷宫问题_bfs
迷宫问题 题目大意:给你一个5*5的矩阵,求左上角到左下角的最短路径. 注释:0或1的矩阵,1表示不能走,0表示能走,保证有唯一最短路径. 想法:bfs爆搜练习题.通过其实点,定义方向数组,然后进行b ...
- poj3984迷宫问题(DFS广搜)
迷宫问题 Time Limit: 1000MSMemory Limit: 65536K Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, ...
- Poj3984 迷宫问题 (BFS + 路径还原)
Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, ...
- POJ-3984.迷宫问题(BFS + 路径输出)
昨天中午做的这道题,结果蛙了一整天,就因为一行代码困住了,今天算是见识到自己有多菜了.流泪.jpg 本题大意:给一个5 * 5的迷宫,1表示墙壁,0表示通路,从左上角走到右下角并输出路径. 本题思路: ...
- poj3984迷宫问题(dfs+stack)
迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 35426 Accepted: 20088 Descriptio ...
- POJ3984 迷宫问题 —— BFS
题目链接:http://poj.org/problem?id=3984 迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
- POJ3984——迷宫问题
迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 31616 Accepted: 18100 Descriptio ...
- POJ-3984 迷宫问题(BFS找最短路径并保存)
问题: 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, ...
随机推荐
- java.sql.SQLException: Before start of result set
在使用JDBC查询数据库报了这么一个错误 CREATE TABLE `d_user` ( `id` int(10) NOT NULL, `name` varchar(10) DEFAULT NULL, ...
- java基础加强
一.泛型 Generic 1.集合泛型: 在没有泛型之前,集合中存入的数据,类型就会丢失掉,在取出数据时,需要做强制类型转换,就有转换失败的风险,而这种风险,在编译阶段是没有办法检查出来的 引入泛型后 ...
- 2014年的Google I/O app设计中的材料设计-渣渣的翻译
又是一篇翻译,用了三个多小时.http://android-developers.blogspot.co.id/2014/08/material-design-in-2014-google-io-ap ...
- SQL SERVER格式化字符串位数,不足补零
本文举例在SQLSERVER中将1格式化为001的方法: 1.方法一SQL语句执行如下: ,) as col 2.方法二SQL语句执行如下: ,) ,) as col 下面是C#代码实现方法: ; & ...
- mssql死锁问题
在网上查看了很多死锁与阻塞的资料,为什么会出现死锁或者阻塞? 阻塞在大数据量的数据库中经常出现,在我现在的其中一个项目出现的频率很高,根据网上查到死锁跟阻塞的资料,当时分析出来,主要是多台设备同时调用 ...
- 16_MyBatis中期小结
[MyBatis是什么] MyBatis是一个持久层框架,Mybatis是一个不完全的ORM框架,SQL语句需要程序员自己去编写,但是MyBatis也有映射(输入参数映射.输出结果映射). MyBat ...
- OpenJudge/Poj 1517 u Calculate e
1.链接地址: http://bailian.openjudge.cn/practice/1517 http://poj.org/problem?id=1517 2.题目: 总时间限制: 1000ms ...
- 360极速浏览器在XP系统下的一个bug
今天在做页面开发的时候,发现360浏览器在XP系统下不支持focus事件,而主流的IE(包括IE6),firefox,chrome都没有这个问题.前段开发的悲剧啊,各种浏览器兼容性的问题.
- 通过shell脚本实现代码自动化部署
通过shell脚本实现代码自动化部署 一.传统部署方式及优缺点 1.传统部署方式 (1)纯手工scp (2)纯手工登录git pull.svn update (3)纯手工xftp往上拉 (4)开发给打 ...
- shell实现查询oracle数据库表,并写到本地txt文件
1.表结构 create table t_student( id ) primary key, name ), birthday date ); increment ; insert into t_s ...