传送门:

http://poj.org/problem?id=3984

迷宫问题
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 33105   Accepted: 18884

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,

0, 0, 0, 1, 0,

};

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

Input

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

Output

左上角到右下角的最短路径,格式如样例所示。

Sample Input

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

Sample Output

(0, 0)
(1, 0)
(2, 0)
(2, 1)
(2, 2)
(2, 3)
(2, 4)
(3, 4)
(4, 4)

Source

分析:

虽然bfs写得多一点,但路径打印的这还是第1个!!!

利用栈的特性打印出来就好

code:

#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <algorithm>
#include <string.h>
#include<queue>
#include<stack>
using namespace std;
int G[][];
int dir[][]={,,,,-,,,-};
int vis[][];
struct node
{
int x,y;
}pre[][]; void pri()
{
stack<node> s;
node p;
p.x=,p.y=; while()
{
s.push(p);
if(p.x==&&p.y==)
break;
p=pre[p.x][p.y];
}
int x,y;
while(!s.empty())
{
x=s.top().x;
y=s.top().y;
printf("(%d, %d)\n",x,y);
s.pop();
}
}
void bfs(int x,int y)
{
queue<node> q;
node p,next; p.x=x,p.y=y;
q.push(p);
vis[x][y]=; while(!q.empty())
{
p=q.front();
q.pop(); if(p.x==&&p.y==)
{
pri();
return ;
}
for(int i=;i<;i++)
{
next.x=p.x+dir[i][];
next.y=p.y+dir[i][]; if(next.x>=&&next.x<&&next.y>=&&next.y<&&vis[next.x][next.y]==&&G[next.x][next.y]==)
{
pre[next.x][next.y]=p;
vis[next.x][next.y]=;
q.push(next);
}
}
}
}
int main()
{
memset(G,,sizeof(G));
for(int i=;i<;i++)
{
for(int j=;j<;j++)
{
cin>>G[i][j];
}
}
memset(vis,,sizeof(vis));
memset(pre,,sizeof(pre));
bfs(,);
return ;
}

POJ 3984 迷宫问题(简单bfs+路径打印)的更多相关文章

  1. poj 3984 迷宫问题【bfs+路径记录】

    迷宫问题 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10103   Accepted: 6005 Description ...

  2. POJ 3984 迷宫问题【BFS/路径记录/手写队列】

    迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 31428 Accepted: 18000 Description 定义 ...

  3. (简单) POJ 3984 迷宫问题,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, ...

  4. POJ 3984 迷宫问题(BFS)

    迷宫问题 Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, ...

  5. POJ - 3984 迷宫问题 【BFS】

    题目链接 http://poj.org/problem?id=3984 思路 因为要找最短路 用BFS 而且 每一次 往下一层搜 要记录当前状态 之前走的步的坐标 最后 找到最短路后 输出坐标就可以了 ...

  6. POJ——3984迷宫问题(BFS+回溯)

    迷宫问题 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14568   Accepted: 8711 Description ...

  7. BFS(最短路+路径打印) POJ 3984 迷宫问题

    题目传送门 /* BFS:额,这题的数据范围太小了.但是重点是最短路的求法和输出路径的写法. dir数组记录是当前点的上一个点是从哪个方向过来的,搜索+,那么回溯- */ /************* ...

  8. POJ.3894 迷宫问题 (BFS+记录路径)

    POJ.3894 迷宫问题 (BFS+记录路径) 题意分析 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, ...

  9. POJ 3984 迷宫问题

    K - 迷宫问题 Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Sta ...

随机推荐

  1. centos自带python2.6升级到python2.7。并解决yum pip easy_install pip等模块兼容性问题

    参考原文:  https://www.cnblogs.com/kimyeee/p/7250560.html   https://www.cnblogs.com/galaxy-gao/p/5796488 ...

  2. javaweb servlet jsp简单笔记

    第二章: 1: web 俗称 : 万维网  www 2: web开发 的三大核心: HTML(网页) ,URL(定位),HTTP:(协议) 页面的分类: 静态页面: html+css 动态页面:jsp ...

  3. Python-常用模块2

    今天我们继续来看模块的那些事儿 一.os模块 所有和操作系统相关内容都在os模块 os.makedirs('dirname1/dirname2') 可生成多层递归目录 os.removedirs('d ...

  4. vuex的初始化

    创建store文件夹 1.功能:放异步操作 文件:actions.js 内容: 2.功能:获取state里数据 文件:getters.js 内容: export const singer = stat ...

  5. VC++中出现错误“ error c2065 'printf' undeclared identifier”的处理方法

    原文:http://blog.csdn.net/panpan639944806/article/details/20135311 有两种可能: 1.未加头文件 #include <stdio.h ...

  6. Android 高速录像(1)

    package com.kirin.voltage.activity; import java.io.File;import java.io.IOException;import java.util. ...

  7. JS高级程序设计第三版——变量、作用域和内存问题

    JavaScript变量: 由于JavaScript变量松散类型的本质,决定了它只是在特定时间用于保存特定值的一个名字而已.由于不存在定义某个变量必须要保存何种数据类型值的规则,变量的值及其数据类型可 ...

  8. mysql主键问题

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/qq_22314145/article/details/80824660 MySQL主键 一. MyS ...

  9. 关于cookie的详解

    http://www.cnblogs.com/fish-li/archive/2011/07/03/2096903.html

  10. leetcode-surrounded regions-ZZ

    Problem Statement (link): Given a 2D board containing 'X' and 'O', capture all regions surrounded by ...