定义一个二维数组:

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)
#include <iostream>
#include<string.h>
#include<algorithm>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int flag[]={};
long long ans,m;
char str[][];
int n,k;
#define QSize 50
int a[][];
int dis[][]={{-,},{,},{,-},{,}};
struct Node{
int x,y,pre;
}queue[QSize];//设置一个50个格子的队列
int front=;
int rear=;//设置队头和队尾,头指针指向头元素,尾指针指向队尾的下一个位置
int visit[][];//记录是否访问过的数组
//广度优先遍历
void bfs(int beginX,int beginY,int endX,int endY)
{
queue[].x =beginX,queue[].y =beginY,queue[].pre =-;
rear=rear+;
visit[beginX][beginY]=;
while(front<rear)//如果队列不为空
{
for(int i=;i<;i++)
{
int newx=queue[front].x +dis[i][];
int newy=queue[front].y +dis[i][];
if(newx<||newx>||newy<||newy>||a[newx][newy]==||visit[newx][newy]==)
continue;
//进队
queue[rear].x =newx;
queue[rear].y =newy;
queue[rear].pre =front;
rear++;
visit[newx][newy]=;//给走过的位置标记
if(newx==endX&&newy==endY)
return;
}
front++;//出队
}
}
void print(Node now){
if(now.pre ==-)
cout<<"("<<now.x <<","<<now.y <<")"<<endl;
else{
print(queue[now.pre ]);
cout<<"("<<now.x <<","<<now.y <<")"<<endl;
}
}
int main()
{
int maze[][];
for(int i=;i<;i++)
{
for(int j=;j<;j++)
{
cin>>a[i][j];
}
}
bfs(,,,);
print(queue[rear-]);
/* for(int i=0;i<rear;i++)
{
cout<<"("<<queue[i].x <<","<<queue[i].y <<")"<<endl;
}*/
return ;
}

POJ3984(迷宫问题)的更多相关文章

  1. poj3984迷宫问题 广搜+最短路径+模拟队列

    转自:http://blog.csdn.net/no_retreats/article/details/8146585   定义一个二维数组: int maze[5][5] = { 0, 1, 0, ...

  2. poj3984迷宫问题

    一个5 × 5的二维数组,表示一个迷宫.其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线. 很简单的一道题,迷宫问题,一般都选择两种优先搜索 ...

  3. [poj3984]迷宫问题_bfs

    迷宫问题 题目大意:给你一个5*5的矩阵,求左上角到左下角的最短路径. 注释:0或1的矩阵,1表示不能走,0表示能走,保证有唯一最短路径. 想法:bfs爆搜练习题.通过其实点,定义方向数组,然后进行b ...

  4. poj3984迷宫问题(DFS广搜)

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

  5. Poj3984 迷宫问题 (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, ...

  6. POJ-3984.迷宫问题(BFS + 路径输出)

    昨天中午做的这道题,结果蛙了一整天,就因为一行代码困住了,今天算是见识到自己有多菜了.流泪.jpg 本题大意:给一个5 * 5的迷宫,1表示墙壁,0表示通路,从左上角走到右下角并输出路径. 本题思路: ...

  7. poj3984迷宫问题(dfs+stack)

    迷宫问题 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 35426   Accepted: 20088 Descriptio ...

  8. POJ3984 迷宫问题 —— BFS

    题目链接:http://poj.org/problem?id=3984 迷宫问题 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions ...

  9. POJ3984——迷宫问题

    迷宫问题 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 31616   Accepted: 18100 Descriptio ...

  10. 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, ...

随机推荐

  1. cornerstone提示“SQLite-database disk image is malformed”

    当点击workingCopy时错误如下 google了一下,有是有解决的办法,可是这些都是直接使用sqlite时产生的问题. sqlite错误 The database disk image is m ...

  2. iOS9全新的联系人相关框架——Contacts Framework

    iOS9全新的联系人相关框架——Contacts Framework 一.引言 在以前iOS开发中,涉及联系人相关的编程,代码都非常繁琐,并且框架的设计也不是Objective-C风格的,这使开发者用 ...

  3. Delphi 版FindWindow 和 FindWindowEx 的语法和用法

    FindWindow(lpClassName,        {窗口的类名}lpWindowName: PChar {窗口的标题}): HWND;              {返回窗口的句柄; 失败返 ...

  4. Maria-DB

    mysql客户端可用选项: -A, --no-auto-rehash 禁止补全 -u, --user= 用户名,默认为root -h, --host= 服务器主机,默认为localhost -p, - ...

  5. python-三级菜单的优化实现

    三级菜单需求: 1.可依次选择进入各子菜单 2.可从任意一层往回退到上一层 3.可从任意一层退出程序 所需新知识点:列表.字典 先通过字典建立数据结构 #创建字典 city_dic = { " ...

  6. python 中 pynlpir错误 Cannot Open Configure file pynlpir\Data\Configure.xml 解决

    在用python做分词.数据处理的时候,想调用pynlpir库,pynlpir.open()时出现错误,更新一下授权文件还是错误, 仔细一看错误是:Cannot Open Configure file ...

  7. jenkins邮件发送jmeter接口测试报告

    在Jenkins中配置实现邮件通知,Jenkins提供了两种方式的配置. 一种是Jenkins内置默认的邮件通知,但是它本身有很多局限性,比如它的邮件通知无法提供详细的邮件内容.无法定义发送邮件的格式 ...

  8. latex01-LaTeX环境的安装与配置

    以Tex Live (跨平台的发行版软件)为例. 1.官网下载iso镜像文件 2.用advanced.bat 安装(管理员权限) 选择要安装的包(主要是去掉多余的语言包) 测试Tex Live 是否正 ...

  9. 652. Find Duplicate Subtrees

    /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...

  10. 人人都会设计模式:观察者模式--Observer

    https://segmentfault.com/a/1190000012295887 观察者模式是抽像通知者和观察者,达到具体通知者跟具体观察者没有偶合.能达到不管是切换通知者,或者是切换观察者,都 ...