传送门:

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. mysql 的用法

    SELECT CASEWHEN    //当count(userId) = 0的时候 让其为null 不然报警告 // count(DISTINCT userId) 不用处理 count(userId ...

  2. java JDBC链接sqlserver/mysql/oracle

    今天初学数据库的一些简单创建数据库和表,并进行简单的查询,插入. 接下学习的就是java工程中怎么链接数据库呢.主要的方法和用到的类如下. 切记,mysql需要的jar包 mysql-connecto ...

  3. div 旋转

    -webkit-transform:rotate(120deg); -moz-transform:rotate(120deg); -o-transform:rotate(120deg); /* fil ...

  4. iis添加共享目录为虚拟目录

    注意物理路径处不能直接选择映射成的本地盘符!!!

  5. node定时任务

    var schedule = require('node-schedule') require('shelljs/global'); function scheduleCronstyle(){ sch ...

  6. Android解析ActivityManagerService(二)ActivityTask和Activity栈管理

    前言 关于AMS,原计划是只写一篇文章来介绍,但是AMS功能繁多,一篇文章的篇幅远远不够.这一篇我们接着来学习与AMS相关的ActivityTask和Activity栈管理. 1.ActivitySt ...

  7. 第三次Scrum编码冲刺

    1.小组成员 周 斌 舒 溢 许嘉荣 唐 浩 黄欣欣 廖帅元 刘洋江 薛思汝 2.个人在小组第三次冲刺的任务及其完成情况描述. 这次小组活动中我负责测试程序的实现情况,确保程序的每个模块都能正常运行, ...

  8. intellij-maven-imports-have-broken-classpath

    公司自己搭得maven私服,然后使用git下载 公司mvn的依赖包时候,报错: 之后再idea里面发现依赖的jar包 有红色的错误信息,提示 maven imports have broken cla ...

  9. ArrayList排序Sort()方法(转)

    //使用Sort方法,可以对集合中的元素进行排序.Sort有三种重载方法,声明代码如下所//示. public void Sort(); //使用集合元素的比较方式进行排序 public void S ...

  10. Linux ->> Chmod命令改变文件/文件夹属性

    简介 chmod命令用于改变linux系统文件或目录的访问权限,控制用户/用户组对文件或目录的访问权限. 用法: 两种用法:1)用字母r(读).w(写).x(执行)表示权限类型:2)用数字表示,4代表 ...