(简单) POJ 3984 迷宫问题,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,
0, 0, 0, 1, 0,
};
它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。
#include<iostream>
#include<cstring>
#include<queue>
#include<utility> using namespace std; typedef struct pair<int,int> pii; int vis[][];
int fa[][]; bool judge(int x,int y)
{
if(x<||y<||x>||y>)
return ; if(vis[x][y])
return ; return ;
} void slove()
{
queue < pii > que;
pii temp,temp1;
int t1,t2; que.push(make_pair(,));
vis[][]=; while(!que.empty())
{
temp=que.front();
que.pop(); t1=temp.first/;
t2=temp.first%;
fa[t1][t2]=temp.second; if(t1==&&t2==)
return; --t1;
if(judge(t1,t2))
{
vis[t1][t2]=;
temp1=make_pair(t1*+t2,temp.first);
que.push(temp1);
}
t1+=;
if(judge(t1,t2))
{
vis[t1][t2]=;
que.push(make_pair(t1*+t2,temp.first));
}
--t1;
--t2;
if(judge(t1,t2))
{
vis[t1][t2]=;
que.push(make_pair(t1*+t2,temp.first));
}
t2+=;
if(judge(t1,t2))
{
vis[t1][t2]=;
que.push(make_pair(t1*+t2,temp.first));
}
}
} void showans()
{
int cou=;
int ans[];
int temp=; while(temp)
{
ans[cou++]=temp;
temp=fa[temp/][temp%];
} cout<<"(0, 0)"<<endl;
for(int i=cou-;i>=;--i)
cout<<'('<<ans[i]/<<", "<<ans[i]%<<')'<<endl;
} int main()
{
ios::sync_with_stdio(false); while(cin>>vis[][])
{
for(int j=;j<;++j)
cin>>vis[][j]; for(int i=;i<;++i)
for(int j=;j<;++j)
cin>>vis[i][j]; slove();
showans();
} return ;
}
(简单) POJ 3984 迷宫问题,BFS。的更多相关文章
- 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最短路径的记录和打印问题)
题目链接:http://poj.org/problem?id=3984 宽度优先搜索最短路径的记录和打印问题 #include<iostream> #include<queue> ...
- 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 ...
- POJ 3984 迷宫问题 (BFS + Stack)
链接 : Here! 思路 : BFS一下, 然后记录下每个孩子的父亲用于找到一条路径, 因为寻找这条路径只能从后向前找, 这符合栈的特点, 因此在输出路径的时候先把目标节点压入栈中, 然后不断的向前 ...
- BFS(最短路+路径打印) POJ 3984 迷宫问题
题目传送门 /* BFS:额,这题的数据范围太小了.但是重点是最短路的求法和输出路径的写法. dir数组记录是当前点的上一个点是从哪个方向过来的,搜索+,那么回溯- */ /************* ...
- POJ 3984 迷宫问题(简单bfs+路径打印)
传送门: http://poj.org/problem?id=3984 迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
随机推荐
- 【转】How to build and install PHP 5.6.9 from source on Ubuntu 14.04 VPS
原文 https://vpsineu.com/blog/how-to-build-and-install-php-5-6-9-from-source-on-ubuntu-14-04-vps/ In t ...
- HDU5907 Find Q 数学
题目大意:求当前串中只含q的连续子串的个数 题目思路:水题,但要注意的是计算过程中可能超int范围; #include<iostream> #include<algorithm> ...
- How to write a probeContentType() and Usage?
Files.probeContentType() is an instance of FileTypeDetector class's abstract method String FileTypeD ...
- jsp base标签与meta标签学习小结
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"% ...
- heartbeat集群安装配置
安装配置高可用集群需要注意:1.节点名称:集群每个节点的名称都得能互相解析 /etc/hosts hosts主机名的正反解析结果必须跟"uname -n"的结果保持一致2.时间必须 ...
- Android如何使用API
转自:http://www.cnblogs.com/vanezkw/archive/2012/07/03/2574559.html 本文针对Android开发如何使用API文档进行一些经验分享. 1. ...
- Entity Framework 学习初级篇2--ObjectContext、ObjectQuery、ObjectStateEntry、ObjectStateManager类的介绍
本节,简单的介绍EF中的ObjectContext.ObjectQuery.ObjectStateEntry.ObjectStateManager这个几个比较重要的类,它们都位于System.Data ...
- C# 经典入门15章 RichTextBox
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAsEAAAIRCAIAAAAk7fcMAAAgAElEQVR4nOy9+SOU3/////pHuswYyz
- 手写js代码(一)javascript数组循环遍历之forEach
注:原文地址http://blog.csdn.net/oscar999/article/details/8671546 我这里是仿照学习! 1.js的数组循环遍历 ①数组的遍历首先想到的是for()循 ...
- ural1097 Square Country 2
Square Country 2 Time limit: 1.0 secondMemory limit: 64 MB The Square Parliament of the Square count ...