题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=105278#problem/K

K - 迷宫问题

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Submit Status

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表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。

Input

一个5 × 5的二维数组,表示一个迷宫。数据保证有唯一解。

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<cstdio>
#include<cstring>
#include<stack>
#include<queue>
#include<string>
using namespace std;
int mp[][];
struct Node{
int x;
int y;
};
Node fa[][];
bool vis[][];
bool ck(Node k)
{
if(k.x<=&&k.x>=&&k.y>=&&k.y<=) return true;
return false;
} int go[][] = {{,},{-,},{,},{,-}}; void bfs(){
queue<Node>q;
memset(vis,,sizeof(vis));
Node tm;
tm.x = ;
tm.y = ;
q.push(tm);
vis[][] = ;
while(!q.empty()){
Node tm1 = q.front();q.pop();
Node fl;
for(int i = ; i < ; i++){
fl.x = tm1.x+go[i][];
fl.y = tm1.y+go[i][];
if(ck(fl)&&vis[fl.x][fl.y]==&&mp[fl.x][fl.y]==){
q.push(fl);
vis[fl.x][fl.y] = ;
fa[fl.x][fl.y] = tm1;
}
}
}
} int main()
{
for(int i = ; i < ; i++)
for(int j = ; j < ; j++)
scanf("%d",&mp[i][j]);
bfs();
stack<Node>v;
Node ff = fa[][];
while(!(ff.x==&&ff.y==)){
v.push(ff);
ff = fa[ff.x][ff.y];
}
printf("(0, 0)\n");
while(!v.empty()){
printf("(%d, %d)\n",v.top().x,v.top().y);
v.pop();
}
printf("(4, 4)\n");
return ;
}

迷宫问题(bfs+记录路径)的更多相关文章

  1. POJ.3894 迷宫问题 (BFS+记录路径)

    POJ.3894 迷宫问题 (BFS+记录路径) 题意分析 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, ...

  2. Codeforces-A. Shortest path of the king(简单bfs记录路径)

    A. Shortest path of the king time limit per test 1 second memory limit per test 64 megabytes input s ...

  3. HDU1026--Ignatius and the Princess I(BFS记录路径)

    Problem Description The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius has ...

  4. - 迷宫问题 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, 0, 1, ...

  5. hdu 1026 Ignatius and the Princess I (bfs+记录路径)(priority_queue)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1026 Problem Description The Princess has been abducted ...

  6. hdu 1026 Ignatius and the Princess I(优先队列+bfs+记录路径)

    以前写的题了,现在想整理一下,就挂出来了. 题意比较明确,给一张n*m的地图,从左上角(0, 0)走到右下角(n-1, m-1). 'X'为墙,'.'为路,数字为怪物.墙不能走,路花1s经过,怪物需要 ...

  7. (简单) POJ 3414 Pots,BFS+记录路径。

    Description You are given two pots, having the volume of A and B liters respectively. The following ...

  8. poj 3414 Pots 【BFS+记录路径 】

    //yy:昨天看着这题突然有点懵,不知道怎么记录路径,然后交给房教了,,,然后默默去写另一个bfs,想清楚思路后花了半小时写了120+行的代码然后出现奇葩的CE,看完FAQ改了之后又WA了.然后第一次 ...

  9. sdut oj 3058 路线冲突问题(BFS+记录路径算法,回溯路径 )

    路线冲突问题 题目描述 给出一张地图,地图上有n个点,任意两点之间有且仅有一条路.点的编号从1到n. 现在兵团A要从s1到e1,兵团B要从s2到e2,问两条路线是否会有交点,若有则输出交点个数,否出输 ...

随机推荐

  1. Mybatis篇总结

    本文是对慕课网上"搞定SSM开发"路径的系列课程的总结,详细的项目文档和课程总结放在github上了.点击查看 JDBC写法 //sql: String sql = "s ...

  2. C# 内存模型

    C# 内存模型 This is the first of a two-part series that will tell the long story of the C# memory model. ...

  3. Linux 学习记录 一(安装、基本文件操作).

         Linux distributions主要分为两大系统,一种是RPM方式安装软件的系统,包括Red Hat,Fedora,SuSE等都是这类:一种则是使用Debian的dpkg方式安装软件的 ...

  4. 598. Range Addition II

    Given an m * n matrixMinitialized with all0's and several update operations. Operations are represen ...

  5. P神的SDFZ考试题 C题

                                                                      探险[问题描述]          探险家小 T 好高兴! X 国要 ...

  6. 未来五年什么样的IT技术最具颠覆性?这里有你想知道的答案

    据外媒报道称,近日Gartner研讨会在美国弗罗里达州奥兰多举行,智能化.大数据和物联网成为届研讨会的三大主题.市场研究机构Gartner Research的副总裁兼资深研究员大卫·卡利(David ...

  7. 记录Mac下安装pyenv时所遇到的问题

    http://blog.csdn.net/foryouslgme/article/details/51683654  

  8. js必须掌握的基础

    好多人想要学习前端……自学或者培训那么我们在学习过程中到底需要掌握那些基础知识呢!下面分类了JS中必备的知识也是必须要了解学会的!看一看你是否已经将JS的基础知识都了如指掌了呢? 事件: onmous ...

  9. idea创建Maven多模块项目

    最近几天学习到了创建多模块项目,应为自己使用的是Idea,所以想用idea创建多模块,查阅了相关资料后,自己做一个记录. 一.首先创建一个maven项目 Parent Project,创建xxx-ro ...

  10. 6.前端基于react,后端基于.net core2.0的开发之路(6) 服务端渲染(SSR)

    0.源码地址 https://gitee.com/teambp/ScaffoldClient  这个地址下载对应源码. 1.服务端渲染是啥? 就是在服务器进行页面渲染(废话),当页面展示后,显示的就是 ...