[POJ 3984] 迷宫问题(BFS最短路径的记录和打印问题)
题目链接:http://poj.org/problem?id=3984
宽度优先搜索最短路径的记录和打印问题
#include<iostream>
#include<queue>
#include<cstring>
#include<cstdio>
using namespace std; bool maze[][];
int go[][] = {,,,-,,,-,};
struct node
{
int x,y;
int prex,prey;
}path[][],temp; void bfs()
{
queue<node> Q;
node temp;
int nx,ny;
path[][].x=path[][].y=;
Q.push(path[][]);
while(!Q.empty())
{
temp = Q.front();
Q.pop();
if(temp.x==&&temp.y==)
return;
for(int i=;i<;i++)
{
nx = temp.x + go[i][];
ny = temp.y + go[i][];
if(nx>=&&nx<&&ny>=&&ny<&&!maze[nx][ny])
{
path[nx][ny].x = nx;
path[nx][ny].y = ny;
path[nx][ny].prex = temp.x;
path[nx][ny].prey = temp.y;
maze[nx][ny] = ;
Q.push(path[nx][ny]);
}
}
}
} void print_path(int x,int y)
{
if(x==&&y==)
{
cout<<"("<<path[][].x<<", "<<path[][].y<<")"<<endl;
return ;
}
int px = path[x][y].prex;
int py = path[x][y].prey;
print_path(px,py);
cout<<"("<<path[x][y].x<<", "<<path[x][y].y<<")"<<endl;
}
int main()
{
for(int i=;i<;i++)
for(int j=;j<;j++)
scanf("%d",&maze[i][j]);
bfs();
print_path(,);
return ;
}
[POJ 3984] 迷宫问题(BFS最短路径的记录和打印问题)的更多相关文章
- POJ 3984 迷宫问题 (BFS + Stack)
链接 : Here! 思路 : BFS一下, 然后记录下每个孩子的父亲用于找到一条路径, 因为寻找这条路径只能从后向前找, 这符合栈的特点, 因此在输出路径的时候先把目标节点压入栈中, 然后不断的向前 ...
- POJ - 3984迷宫问题(最短路径输出)
题目链接:http://poj.org/problem?id=3984 题目: 迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submiss ...
- POJ 3984 - 迷宫问题 - [BFS水题]
题目链接:http://poj.org/problem?id=3984 Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, ...
- POJ 3984 迷宫问题 bfs 难度:0
http://poj.org/problem?id=3984 典型的迷宫问题,记录最快到达某个点的是哪个点即可 #include <cstdio> #include <cstring ...
- 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, ...
- poj 3984 迷宫问题 bfs
学会这道水题之后我懂得了不少哈,首先水题也能学到不少知识,尤其像我这样刚入门的小菜鸟,能学到一些小技巧. 然后就是可以从别人的代码里学到不一样的思路和想法. 这题就是求最短的路径,首先想到就是用bfs ...
- POJ - 3984 迷宫问题 bfs解法
#include<stdio.h> #include<string.h> #include<algorithm> #include<stack> usi ...
- BFS(最短路+路径打印) POJ 3984 迷宫问题
题目传送门 /* BFS:额,这题的数据范围太小了.但是重点是最短路的求法和输出路径的写法. dir数组记录是当前点的上一个点是从哪个方向过来的,搜索+,那么回溯- */ /************* ...
- POJ 3984 迷宫问题(简单bfs+路径打印)
传送门: http://poj.org/problem?id=3984 迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
随机推荐
- python: ImportError: cannot import name 'Style' from 'openpyxl.styles' 解决方法
import os, openpyxl from openpyxl.styles import Font, Style os.chdir("C:\\") wb = openpyxl ...
- Hue中hive(hive cli)查询结果中显示列名,不带表名
hive cli中显示列名 进入hive cli后 set hive.cli.print.header=true; 之后出现列名,但是带了表名前缀,由于网上没找到资料,于是到官网肉眼扫描所有参数,总算 ...
- flask 文件上传(单文件上传、多文件上传)
文件上传 在HTML中,渲染一个文件上传字段只需要将<input>标签的type属性设为file,即<input type=”file”>. 这会在浏览器中渲染成一个文件上传字 ...
- C# CheckBox与RadioButton
通常RadioBox称为单选按钮,CheckBox称为多选按钮,这两个控件都是从ButtonBase类中派生,可以将其视为按钮. 多个checkBox之间的选择是互相独立的,互补影响.多个RadioB ...
- Linux基础命令---文本显示od
od 将指定文件的内容以八进制.十进制.十六进制等编码方式显示.此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.SUSE.openSUSE.Fedora. 1.语法 ...
- Maven依赖中的scope详解,在eclipse里面用maven install可以编程成功,到服务器上用命令执行报VM crash错误
Maven依赖中的scope详解 项目中用了<scope>test</scope>在eclipse里面用maven install可以编译成功,到服务器上用命令执行报VM cr ...
- 75.Java异常处理机制-自定义异常
package testDate; //自定义异常 public class MyException extends Exception{ public MyException(){ } public ...
- Codeforces 237A - Free Cash
题目链接:http://codeforces.com/problemset/problem/237/A Valera runs a 24/7 fast food cafe. He magically ...
- SQL非域环境下带自动故障转移数据库镜像的实现方法(包括镜像服务器)
使用数据库镜像来提高数据库的高可用性,在镜像服务器创建镜像数据库的快照以卸载报表查询对生产数据库的负载.TechNet有讲座对此技术进行介绍,但看到大家在讲座的讨论区中遇到了很多问题,下面我把在非域环 ...
- 每天学点Linux命令之grep 和 wc命令
Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regular Expr ession Print,表示全局正则表 ...