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, ...
随机推荐
- git对象存储
之前提到当存储数据内容时,会在objects文件夹下存储一个git对象.我们花些时间来看看 Git 是如何存储对象的.你将看来如何通过 Ruby 脚本语言存储一个 blob 对象 ,有必要了解一下对象 ...
- 利用javascript实现文本的自动输出
主要利用了setTimeout(),递归和String.substring(); 做出的效果就像是有一个打字员在打字. <!doctype html> <html lang=&quo ...
- Eclipse中绑定java源代码
如何在Eclipse sdk中查看jar源代码如:*.jar 1.点 “window”-> "Preferences" -> "Java" -&g ...
- Nginx的反向代理
先通过简单的图来说明一下正向代理和反向代理吧~ 正向代理 代理其实就是一个中介,A和B本来可以直连,中间插入一个C,C就是中介.刚开始的时候,代理多数是帮助内网client访问外网server用的(比 ...
- 12_复杂查询01_Mapper代理实现
[工程截图] [代码实现] [user.java] package com.Higgin.Mybatis.po; import java.util.Date; public class User { ...
- UVA 11300 Spreading the Wealth (数学推导 中位数)
Spreading the Wealth Problem A Communist regime is trying to redistribute wealth in a village. They ...
- Codevs 1183 泥泞的道路
1183 泥泞的道路 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 传送门 题目描述 Description CS有n个小区,并且任意小区之间都有两条单向道路 ...
- vs的watch使用
VC调试器高级应用----WATCH窗口篇 一.格式化数据和表达式赋值语句. 常用变量格式化符(表达式的值后跟逗号,接格式化符,如"(int)0xFFFF,d"):d :有符号的 ...
- Win32 CreateWindow GdiPlus
#include "stdafx.h" #include "TestGidPlus.h" LRESULT CALLBACK WndProc(HWND, UINT ...
- PHP & Javascript 如何对字符串中包含html标签进行编码 整理
为什么要对字符串编码? 某些字符串中包含html标签,不编码,页面输出就乱了. PHP下怎么对字符串编码? htmlentities vs htmlspecialchars htmlentities ...