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. CDQ分治_占坑

    准备系统地学习一波CDQ分治,持续更新中... 首先,CDQ分治也还是分治的一种,只不过普通分治是独立的解决两个子问题,而CDQ分治还要计算第一个子问题对于第二个的影响. CDQ分治几乎都是用来解决多 ...

  2. dp算法之硬币找零问题

    题目:硬币找零 题目介绍:现在有面值1.3.5元三种硬币无限个,问组成n元的硬币的最小数目? 分析:现在假设n=10,画出状态分布图: 硬币编号 硬币面值p 1 1 2 3 3 5 编号i/n总数j ...

  3. 福大软工1816:Beta(1/7)

    Beta 冲刺 (1/7) 队名:第三视角 组长博客链接 本次作业链接 团队部分 团队燃尽图 工作情况汇报 张扬(组长) 过去两天完成了哪些任务 文字/口头描述 答辩 组织会议 复习课本 展示GitH ...

  4. 严重: Failed to destroy end point associated with ProtocolHandler ["http-nio-8080"] java.lang.NullPointer

    刚接触servlet类,按照课本的方法使用eclipse新建了一个servlet类. 新建完成后,在web.xml里面进行注册 这时候就会报错了. 五月 07, 2016 11:23:28 上午 or ...

  5. Navicat for mysql导入.sql数据库大小受限制

    把导入单个表的最大限制调一下就行(在my.ini里面就算改了max_allowed_packet也不一定行,因为Navicat貌似并不调用,实际他有自己的一套默认配置,所以需要在Navicat上调整) ...

  6. 初识 es6

    es6 可能出来已经有一段时间了,但是我到今天才发现他的好,却不是很了解他,也不知道各个浏览器的兼容性怎么样?今天就把他们都弄明白. 新增命令 let ES6新增了let命令,用来声明变量.它的用法类 ...

  7. 操作系统作业一——仿CMD

    实验一.CMD实验 2014商软2  卓宇靖  4238 一.        实验目的 (1)掌握命令解释程序的原理: (2)掌握简单的DOS调用方法: (3)掌握C语言编程初步. 二.        ...

  8. vbs习题

    练习题: 1.输入3个数,输出其中最大的那个值. Option Explicit Dim intA,intB,intC intA=CInt(InputBox("请输入a:")) i ...

  9. (转) Elasticsearch 5.0 安装 Search Guard 5 插件

    一.Search Guard 简介 Search Guard  是 Elasticsearch 的安全插件.它为后端系统(如LDAP或Kerberos)提供身份验证和授权,并向Elasticsearc ...

  10. Matlab里面.M文件不能运行,预期的图像也显示不出来的一个原因

    matlab中function函数的函数名与保存的文件名需要一样: 函数名是GAconstrain,文件名保存成GAconstrain.m,不要使用复制时候产生副本GAconstrain(1).m.