POJ.3894 迷宫问题 (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, 0, 1, 0,

};

它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。

一个5 × 5的二维数组,表示一个迷宫。数据保证有唯一解。

简单的BFS,路径记录开一个二维数组就好。

代码总览

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#define nmax 6
using namespace std;
int mp[nmax][nmax];
typedef struct{
int x;
int y;
bool isvis;
}mes;
typedef struct{
int x;
int y;
}point;
mes visit[nmax][nmax];
int spx[5] = {0,1,0,-1};
int spy[5] = {1,0,-1,0};
bool check(point temp)
{
if(temp.x <0 || temp.x >=5 ||temp.y < 0 || temp.y >=5 || visit[temp.x][temp.y].isvis|| mp[temp.x][temp.y] == 1)
return false;
else return true;
}
void bfs()
{
memset(visit,0,sizeof(visit));
queue<point> q;
while(!q.empty()) q.pop();
point temp = {0,0},head;
visit[temp.x][temp.y].isvis = true;
q.push(temp);
while(!q.empty()){
head = q.front(); q.pop();
if(head.x == 4 && head.y == 4){
return;
}
for(int i = 0;i<4;++i){
temp.x = head.x + spx[i];
temp.y = head.y + spy[i];
if(check(temp)){
visit[temp.x][temp.y].isvis = true;
visit[temp.x][temp.y].x = head.x;
visit[temp.x][temp.y].y = head.y;
q.push(temp);
}
}
}
}
void output(point temp)
{
point h;
vector<point> v; v.clear();
while(1){
v.push_back(temp);
if(temp.x == 0 && temp.y == 0) break;
h.x = visit[temp.x][temp.y].x;
h.y = visit[temp.x][temp.y].y;
temp = h;
}
for(int i = v.size()-1;i>=0;--i){
printf("(%d, %d)\n",v[i].x,v[i].y);
}
}
int main()
{
for(int i = 0;i<5;++i){
for(int j = 0;j<5;++j){
scanf("%d",&mp[i][j]);
}
}
bfs();
output({4,4});
return 0;
}

POJ.3894 迷宫问题 (BFS+记录路径)的更多相关文章

  1. poj 3414 Pots 【BFS+记录路径 】

    //yy:昨天看着这题突然有点懵,不知道怎么记录路径,然后交给房教了,,,然后默默去写另一个bfs,想清楚思路后花了半小时写了120+行的代码然后出现奇葩的CE,看完FAQ改了之后又WA了.然后第一次 ...

  2. (简单) POJ 3414 Pots,BFS+记录路径。

    Description You are given two pots, having the volume of A and B liters respectively. The following ...

  3. Codeforces-A. Shortest path of the king(简单bfs记录路径)

    A. Shortest path of the king time limit per test 1 second memory limit per test 64 megabytes input s ...

  4. HDU1026--Ignatius and the Princess I(BFS记录路径)

    Problem Description The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius has ...

  5. - 迷宫问题 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, 0, 1, ...

  6. 迷宫问题(bfs+记录路径)

    题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=105278#problem/K K - 迷宫问题 Time Limit:1000 ...

  7. hdu 1026 Ignatius and the Princess I (bfs+记录路径)(priority_queue)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1026 Problem Description The Princess has been abducted ...

  8. hdu 1026 Ignatius and the Princess I(优先队列+bfs+记录路径)

    以前写的题了,现在想整理一下,就挂出来了. 题意比较明确,给一张n*m的地图,从左上角(0, 0)走到右下角(n-1, m-1). 'X'为墙,'.'为路,数字为怪物.墙不能走,路花1s经过,怪物需要 ...

  9. poj 3984 迷宫问题 bfs

    学会这道水题之后我懂得了不少哈,首先水题也能学到不少知识,尤其像我这样刚入门的小菜鸟,能学到一些小技巧. 然后就是可以从别人的代码里学到不一样的思路和想法. 这题就是求最短的路径,首先想到就是用bfs ...

随机推荐

  1. [.NET] 使用HttpClient操作HFS (HTTP File Server)

    前言 本篇文章介绍如何使用HttpClient操作HFS (HTTP File Server),为自己留个纪录也希望能帮助到有需要的开发人员.关于HTTP File Server的介绍.安装.设定,可 ...

  2. java nio通过ByteBuffer输出文件信息

    1.通过ByteBuffer的get()方法每次读取一个字节转换成char类型输出. fc = new FileInputStream("src/demo20/data.txt") ...

  3. Qt 利用XML文档,写一个程序集合 三

    接上一篇https://www.cnblogs.com/DreamDog/p/9214052.html 滚动区域实现, 滚动区域可以三成分层 第一层,显示内容 中间层,滚动层 第三层,爸爸层 把我们要 ...

  4. ddms+adt+jdk的安装及调试开发安卓

    _______ ddms+adt+jdk的安装及调试开发安卓 目录 阐述 1 1  jdk安装 1 2  sdk安装 3 3  Eclipse安装 6 4  ADT安装 10 5  Ddms使用 16 ...

  5. iOS开发之多线程技术—GCD篇

    本篇将从四个方面对iOS开发中GCD的使用进行详尽的讲解: 一.什么是GCD 二.我们为什么要用GCD技术 三.在实际开发中如何使用GCD更好的实现我们的需求 一.Synchronous & ...

  6. Zabbix_agnet部署

    原文发表于cu:2016-05-18 参考文档: zabbix监控linux主机:http://www.osyunwei.com/archives/8035.html  一.环境 Server:基于C ...

  7. ES6对数组的扩展

    ECMAScript6对数组进行了扩展,为数组Array构造函数添加了from().of()等静态方法,也为数组实例添加了find().findIndex()等方法.下面一起来看一下这些方法的用法. ...

  8. Android 对话框(Dialogs)

    对话框是提示用户作出决定或输入额外信息的小窗口. 对话框不会填充屏幕,通常用于需要用户采取行动才能继续执行的模式事件. 1.对话框设计 如需了解有关如何设计对话框的信息(包括语言建议),请阅读对话框设 ...

  9. CSS中水平居中设置的几种方式

    1.行内元素: 如果被设置元素为文本.图片等行内元素时,水平居中是通过给父元素设置 text-align:center 来实现的. <body> <div class="t ...

  10. struts-resultType属性

    1.默认dispatcher:forward方式,服务器端跳转 2.redirect:客户端跳转 3.chain:Action转发,forward方式,服务器端跳转action 4.redirectA ...