迷宫问题
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 20665   Accepted: 12100

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

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
int mp[][],vis[][];
int dir[][]={,,-,,,,,-};
struct node{
int x,y;
node(int a,int b):x(a),y(b){}
node(){}
}pre[][],no;
void bfs()
{
queue<node>q;
q.push(node(,));
while(!q.empty()){
no=q.front();q.pop();
for(int i=;i<;i++){
int x=no.x+dir[i][],y=no.y+dir[i][];
if(x<||x>=||y<||y>=||mp[x][y]) continue;
if(vis[x][y]) continue;
vis[x][y]=;
q.push(node(x,y));
pre[x][y]=node(no.x,no.y);
if(x==&&y==) return;
}
}
}
int main()
{
for(int i=;i<;i++)
for(int j=;j<;j++)
scanf("%d",&mp[i][j]);
memset(vis,,sizeof(vis));
bfs();
int ans[],cnt=,x=,y=;
while(){
ans[++cnt]=x;
ans[++cnt]=y;
if(x==&&y==) break;
int xx=x,yy=y;
x=pre[xx][yy].x;
y=pre[xx][yy].y;
}
for(int i=cnt;i>=;i-=)
printf("(%d, %d)\n",ans[i-],ans[i]);
return ;
}

POJ 3984 BFS的更多相关文章

  1. Q - 迷宫问题 POJ - 3984(BFS / DFS + 记录路径)

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

  2. POJ - 3984 bfs [kuangbin带你飞]专题一

    bfs搜索过程中将路径保存下即可. AC代码 #include<cstdio> #include<cstring> #include<algorithm> #inc ...

  3. - 迷宫问题 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, ...

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

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

  5. POJ 3984 迷宫问题

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

  6. POJ 3984(DFS入门题 +stack储存路径)

    POJ 3984 Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, ...

  7. POJ 3984 - 迷宫问题 - [BFS水题]

    题目链接:http://poj.org/problem?id=3984 Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, ...

  8. [POJ 3984] 迷宫问题(BFS最短路径的记录和打印问题)

    题目链接:http://poj.org/problem?id=3984 宽度优先搜索最短路径的记录和打印问题 #include<iostream> #include<queue> ...

  9. POJ 3984 迷宫问题 bfs 难度:0

    http://poj.org/problem?id=3984 典型的迷宫问题,记录最快到达某个点的是哪个点即可 #include <cstdio> #include <cstring ...

随机推荐

  1. mysql中的select语句where条件group by ,having , order by,limit的顺序及用法

    -- 语法: SELECT select_list FROM table_name [ WHERE search_condition ] [ GROUP BY group_by_expression ...

  2. 从零开始的Python学习Episode 6——字符串操作

    字符串操作 一.输出重复字符串 print('smile'*6) #输出6个smile 二.通过引索输出部分字符串 print('smile'[1:]) print('smile'[1:3]) #输出 ...

  3. CVPR2018 关于视频目标跟踪(Object Tracking)的论文简要分析与总结

    本文转自:https://blog.csdn.net/weixin_40645129/article/details/81173088 CVPR2018已公布关于视频目标跟踪的论文简要分析与总结 一, ...

  4. 一:yarn 介绍

        yarn的了出现主要是为了拆分jobtracker的两个核心功能:资源管理和任务监控,分别对应resouceManager(RM)和applicationManager(AM).yarn中的任 ...

  5. Python3 数据类型-元组

    Python 的元组与列表类似,不同之处在于元组的元素不能修改. 元组使用小括号,列表使用方括号. 元组创建很简单,只需要在括号中添加元素,并使用逗号隔开即可. 实例1(Python3.0+): tu ...

  6. “Hello World!”团队第三周召开的第五次会议

    一.会议时间 二.会议地点 三.会议成员 四.会议内容 五.todo list 六.会议照片 七.燃尽图 八.代码地址 一.会议时间 2017年10月31日  11:45-12:17 二.会议地点: ...

  7. Android 上实现非root的 Traceroute -- 非Root权限下移植可执行二进制文件 脚本文件

    作者 : 万境绝尘 转载请著名出处 : http://blog.csdn.net/shulianghan/article/details/36438365 示例代码下载 : -- CSDN : htt ...

  8. 求gcd(最大公因数),lcm(最小公倍数)模板

    gcd(最大公因数),lcm(最小公倍数) #include<iostream> using namespace std; int gcd(int a,int b)//辗转相除法(欧几里德 ...

  9. 3dContactPointAnnotationTool开发日志(九)

      今天的任务是实现选中接触点功能并添加模型或接触点的属性改变功能,先从最简单的位置x,y,z改变入手,于是创建了一个面板(PanelStatus),添加了几个InputField来让用户输入数值改变 ...

  10. 【Linux】- ps 命令

    Linux ps命令用于显示当前进程 (process) 的状态. 语法 ps [options] [--help] 参数: -A     列出所有的行程 -w    显示加宽可以显示较多的资讯 -a ...