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, ...
随机推荐
- leetcode笔记(七)529. Minesweeper
题目描述 Let's play the minesweeper game (Wikipedia, online game)! You are given a 2D char matrix repres ...
- springboot中有用的几个有用aware以及bean操作和数据源操作
本文参考了: https://blog.csdn.net/derrantcm/article/details/76652951 https://blog.csdn.net/derrantcm/arti ...
- 三种for循环遍历
import java.util.ArrayList;import java.util.Iterator;import java.util.List; public class For{ publi ...
- bootstrap-daterangepicker插件运用
引入:daterangepicker.css.daterangepicker.js.moment.js.moment.min.js 链接:https://files.cnblogs.com/files ...
- dedecms添加/编辑文章如何把附加选项去掉默认勾选状态
1.去掉添加时默认勾选状态. 在 系统->系统基本参数->其它选项 中,如图中的三个选项选择否即可. 设置完后可以看到添加时已经默认不勾选,但是编辑文章时还是默认勾选状态. 2.去掉编辑时 ...
- MR执行流程
1.Map任务处理 1.1 读取HDFS中的文件.每一行解析成一个<k,v>.每一个键值对调用一次map函数. <0,hello you> <10,hello me& ...
- numpy数组用法大全
机器学习的最基础模块就是numpy模块了,而numpy模块中的数组操作又是重中之重,所以我们要把数组的各种方法弄得明明白白的,以下就是数组的一些常用方法 1.创建各种各样的数组: import num ...
- Python3 利用pip安装BeautifulSoup4模块(Windows版)
一.找到Python3的安装文件夹 二.将路径复制 三.Windows10 打开Windows PowerShell(管理员).Windows 8.8.1.7使用cmd 切换到相应目录 四.此目录下的 ...
- (数据科学学习手札06)Python在数据框操作上的总结(初级篇)
数据框(Dataframe)作为一种十分标准的数据结构,是数据分析中最常用的数据结构,在Python和R中各有对数据框的不同定义和操作. Python 本文涉及Python数据框,为了更好的视觉效果, ...
- 【MySql】mysql 慢日志查询工具之mysqldumpslow
当使用--log-slow-queries[=file_name]选项启动时,mysqld写一个包含所有执行时间超过long_query_time秒的SQL语句的日志文件.获得初使表锁定的时间不算 ...