定义一个二维数组:

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. c#实现的HTTP服务端

    这次在整理一个服务组件的时候,需要涉及到HTTP的请求,HTTP是应用层,建立在TCP之上的.因此,可以用TCP服务端接收HTTP请求,只需要解析请求内容.HTPP有固定的格式,大家可以直接搜索.网上 ...

  2. 调用微信JS上传照片接口上传图片

    public ActionResult UploadImge(string serverId) { var headPath = "/UploadImage/" + DateTim ...

  3. HTTP头部信息解释分析

    HTTP 头部解释 1. Accept:告诉WEB服务器自己接受什么介质类型,*/* 表示任何类型,type/* 表示该类型下的所有子类型,type/sub-type. 2. Accept-Chars ...

  4. (Linux 日常命令)[20171225]

    目的:记录Linux日常所用命令 [20171222]Linux环境下查看硬件组件型号 cat /proc/cpuinfo及lspci 查看CPU [root@t-redhat- ~]# cat /p ...

  5. 解决WordPress设置错误的url网站不能访问的问题

    通过WordPress后台首选项更改了网站url地址之后,网站就会出现访问不了的情况,一般来说,网站后台也登陆不上去了,我从网上寻找到了四种方法,这四种方法前三种都是需要登陆到后台的,但实际上出错后, ...

  6. POCO TCPServer 解析

    POCO C++ Libraries提供一套 C++ 的类库用以开发基于网络的可移植的应用程序,功能涉及线程.文件.流,网络协议包括:HTTP.FTP.SMTP 等,还提供 XML 的解析和 SQL ...

  7. Element-ui树形控件el-tree获取父级节点的id

    Element-ui官网给的方法 getCheckedKeys() { console.log(this.$refs.tree.getCheckedKeys()); }, 这种只有在所有子级都被选中的 ...

  8. node 写api几个简单的问题

    最近出了一直在做无聊的管理后台,还抽空做了我公司的计费终端,前端vue,后端node,代码层面没啥太多的东西.由于自己node版本是8.0.0,node自身是不支持import和export的,要想基 ...

  9. 微信小程序横向滚动

    <scroll-view scroll-x="true" style=" white-space: nowrap; display: flex" > ...

  10. YII2.0 用GII创建视图文件后访问404

    使用GII的CRUD Generator创建searchModelClass 和控制器类文件,视图文件后,访问控制器地址后出现404的情况. 创建过程如图所示 后来发现是控制器类 Controller ...