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笔记(三)207. Course Schedule
题目描述(原题目链接) There are a total of n courses you have to take, labeled from 0 to n-1. Some courses may ...
- Intellij IDEA切换maven
问题描述: IDEA自带Maven,但不想用,想用自己安装的. 解决方案: File->Settings(快捷键:Ctrl+Alt+S) 这里分为了两个,竟然还有默认配置一说,上面的只是修改了当 ...
- Vue使用json-server来进行后端数据模拟
正开发过程中 前后端分离或者不分离 ,接口多半是之后与页面的开发 ,所以建立rest的APL的接口 给前端提供虚拟的数据是非常必要的 所以这里我使用了json-server作为工具,支持CORS和JS ...
- python查询mysql数据
>>>cur.execute("select * from 表名") >>>lines=cur.fetchall() >>>f ...
- pycharm中每次创建py文件时就自动生成代码头,以及出现SyntaxError:Non-ASCII 。。。问题
我们在pycharm中执行py文件的时候,可能会出现以下错误 这是因为你没有制定编码格式,这时候你需要在文件最开始制定编码格式,代码如下 #!/user/bin/env python #-*- cod ...
- 关于nodejs DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
const mongoose = require('mongoose') mongoose.connect("mongodb://localhost:27017/study", { ...
- Django的命令操作,python
忘记时候,查看命令用:python manage.py 1 建立项目的命令: django-admin.py startproject project_name 2 在项目的目录下建立app: dja ...
- P1196 银河英雄传说(加权并查集)
P1196 银河英雄传说 题目描述 公元五八○一年,地球居民迁移至金牛座α第二行星,在那里发表银河联邦 创立宣言,同年改元为宇宙历元年,并开始向银河系深处拓展. 宇宙历七九九年,银河系的两大军事集团在 ...
- Android AppWidget偶尔无响应原因及解决办法
Android AppWidget偶尔会出现无响应问题,如按钮点击失效,数据不更新等等. 测试后发现,一般出现在手机用清理工具(或系统自己)清理后发生,或手机重启后发生. 目前经过测试,找到的办法是把 ...
- 各种网站,app的手机号绑定真坑爹
各种网站,app的手机号绑定真坑爹,无力吐槽,哎