POJ3984(迷宫问题)
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
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(迷宫问题)的更多相关文章
- poj3984迷宫问题 广搜+最短路径+模拟队列
转自:http://blog.csdn.net/no_retreats/article/details/8146585 定义一个二维数组: int maze[5][5] = { 0, 1, 0, ...
- poj3984迷宫问题
一个5 × 5的二维数组,表示一个迷宫.其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线. 很简单的一道题,迷宫问题,一般都选择两种优先搜索 ...
- [poj3984]迷宫问题_bfs
迷宫问题 题目大意:给你一个5*5的矩阵,求左上角到左下角的最短路径. 注释:0或1的矩阵,1表示不能走,0表示能走,保证有唯一最短路径. 想法:bfs爆搜练习题.通过其实点,定义方向数组,然后进行b ...
- poj3984迷宫问题(DFS广搜)
迷宫问题 Time Limit: 1000MSMemory Limit: 65536K Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, ...
- 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, ...
- POJ-3984.迷宫问题(BFS + 路径输出)
昨天中午做的这道题,结果蛙了一整天,就因为一行代码困住了,今天算是见识到自己有多菜了.流泪.jpg 本题大意:给一个5 * 5的迷宫,1表示墙壁,0表示通路,从左上角走到右下角并输出路径. 本题思路: ...
- poj3984迷宫问题(dfs+stack)
迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 35426 Accepted: 20088 Descriptio ...
- POJ3984 迷宫问题 —— BFS
题目链接:http://poj.org/problem?id=3984 迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
- POJ3984——迷宫问题
迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 31616 Accepted: 18100 Descriptio ...
- 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, ...
随机推荐
- Shiro登录中遇到了问题
Shiro登录中遇到了问题 记录二次开发中遇到的问题, 如果系统学习Shiro, 推荐跟我学Shrio. 问题 项目是要将验证从本地改为LDAP验证, 但是因为jeecms的验证和授权中, 用户和角色 ...
- kali linux 安装谷歌浏览器
kali linux 版本 2018.2 先下载谷歌浏览器安装包 wget https://dl.google.com/linux/direct/google-chrome-stable_curren ...
- PHP队列之理论篇
定义: 特殊的线性表. 特点: 1.先进先出:连结性. 2.作为一种特殊性的表,主要是在表前端进行删除操作,我们称删除的端为对头(front):只能在表的后端进行插入操作,我们称之为称插入 ...
- 基于OMAPL138的Linux字符驱动_GPIO驱动AD9833(二)之cdev与read、write
基于OMAPL138的Linux字符驱动_GPIO驱动AD9833(二)之cdev与read.write 0. 导语 在上一篇博客里面,基于OMAPL138的字符驱动_GPIO驱动AD9833(一)之 ...
- [Codeforces888E]Maximum Subsequence(暴力+meet-in-the-middle)
题意:给定n.m.有n个数,选出若干数加起来对m取模,求最大值 n<=35 如果直接暴力就是235,会T, 这里用到一个思想叫meet-in-the-middle, 就是把数列分成两半分别搜索, ...
- HDU暑假多校第三场H.Monster Hunter
一.题意 给定一个树状地图,每个树节点上有一只怪物,打死一只怪物的过程中将会消耗A点HP,打死之后将会获得B点HP.因为树状结构,所以每只怪物必须先打死父节点的怪兽之后在打死子节点的怪物.现在,给定每 ...
- howto:在构建基于debian的docker基础镜像时,更换国内包源
debian经常被用作构建应用镜像的基础镜像,如微软在构建linux下的dotnetcore基础镜像时,提供了基于debian 8(jessie)和debian 9(stretch)的镜像. 由于这些 ...
- jsp 添加jstl标签
jsp页面中添加下列代码即可使用jstl标签. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix=" ...
- PHP将两个数组相加
$arr_a=[1=>1,2=>2,3=>3];$arr_b=[1=>'a',4=>4];print_r($arr_a+$arr_b);返回结果:Array ( [1] ...
- zabbix 通过key(键值)获取信息
在agent端进行修改264行,例如: UserParameter=get.os.type,head -1 /etc/issue 保存重启agent 验证 zabbix_get -s IP -k ge ...