/*
定义一个二维数组: 
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表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。
这里最短路径好求,BFS即可,但是如何保存路径是个问题,在这里我采用了一次BFS,即从终点向起点搜索同时用stack保存路径上的点。
*/
#include<iostream>
#include<stack>
#include<queue>
using namespace std;
struct node
{
int x;
int y;
int step;
};
int g[][];
int been[][];
int dis[][];
int dir[][]={{,},{,},{-,},{,-}};
stack<node> S;
bool judge(int x,int y)
{
if(!g[x][y]&&!been[x][y]&&x>=&&y>=&&x<&&y<)
return true;
return false;
}
void Read()
{
int i,j;
for(i=;i<;i++)
for(j=;j<;j++)
cin>>g[i][j];
}
int bfs(int i,int j)
{
node a;
a.x = i;
a.y = j;
a.step = ;
been[i][j] = ;
dis[i][j] = ;
queue<node> Q;
Q.push(a);
while(!Q.empty())
{
node temp = Q.front();
Q.pop();
if(temp.x==&&temp.y==)
return temp.step;
for(int i =;i<;i++)
{
if(judge(temp.x+dir[i][],temp.y+dir[i][]))
{
node new_node;
new_node.x = temp.x+dir[i][];
new_node.y = temp.y+dir[i][];
new_node.step = temp.step + ;
been[new_node.x][new_node.y] = ;
Q.push(new_node);
if(dis[new_node.x][new_node.y] > temp.step + )
dis[new_node.x][new_node.y]=temp.step + ;
}
}
}
return -;
}
void route(int i,int j,int d);
int main()
{
int i,j,ans;
for(i = ;i<;i++)
for(j=;j<;j++)
dis[i][j] = ;
Read();
ans = bfs(,);
route(,,ans);
while(!S.empty())
{
node p = S.top();
S.pop();
cout<<'('<<p.x<<','<<' '<<p.y<<')'<<endl;
}
cout<<'('<<<<','<<' '<<<<')'<<endl;
return ;
}
void route(int i,int j,int d)
{
if(i==&&j==)
{
return ;
}
else
{
for(int k =;k<;k++)
{
int r = i+dir[k][],c = j+dir[k][];
if(r>=&&c>=&&r<&&c<&&(dis[r][c]==d-)&&(been[r][c]))
{
node ct;
ct.x = r;
ct.y = c;
S.push(ct);
route(r,c,d-);
}
}
}
return ;
}

K - 迷宫问题的更多相关文章

  1. K - 迷宫问题 POJ - 3984

    定义一个二维数组: 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, ...

  2. [kuangbin带你飞]专题一 简单搜索 - K - 迷宫问题

    #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #i ...

  3. POJ 3984 迷宫问题

    K - 迷宫问题 Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Sta ...

  4. 迷宫问题(bfs+记录路径)

    题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=105278#problem/K K - 迷宫问题 Time Limit:1000 ...

  5. poj3984《迷宫问题》暑假集训-搜索进阶

    K - 迷宫问题 Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:65536KB     64bit ...

  6. django模型操作

    Django-Model操作数据库(增删改查.连表结构) 一.数据库操作 1.创建model表        

  7. hdu1728 逃离迷宫---转弯次数不超过k+BFS

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1728 题目大意: 给你一幅图,给出起点终点和最大转弯次数,判断是否能从起点到终点.'*'表示障碍物. ...

  8. POJ 2251 Dungeon Master(3D迷宫 bfs)

    传送门 Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 28416   Accepted: 11 ...

  9. HDU 1272 小希的迷宫 并查集

    小希的迷宫 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

随机推荐

  1. DOM事件揭秘-事件流

    事件:文档/窗口中发生的特定的交互瞬间 瀑布流,图片轮播 动作都是通过事件触发的 课程内容: 1,理解事件流 2,使用时间处理程序 3,不同的事件类型 ie4.0以后, 事件流:描述的是从页面中接收事 ...

  2. js数组中数字从小到大排列

    function findMin(start,arr){ var iMin = 99999; var iMinIndex = -1; for(var i = start;i<arr.length ...

  3. 统计和分析系统性能【IO CPU 内存】的工具集合

    统计和分析系统性能[IO CPU 内存]的工具集合 blktrace http://www.oschina.net/p/blktrace 获取磁盘写入的信息 root@demo:~/install/p ...

  4. Android动画例子。

    例子一: 补间动画效果,从右进,从左出. ImageSwitcher mImageSwitcher = new ImageSwitcher(this); mImageSwitcher.setFacto ...

  5. JAVA适配器模式(从现实生活角度理解代码原理)

    说道JAVA中的适配器模式,不得不引用该设计模式的固定表述"适配器模式(Adapter ):将一个类的接口转换成客户希望的另外一个接口,适配器模式使得原本由于接口不兼容而不能一起工作的那些类 ...

  6. Intent属性详解一 component属性

    先看效果图 概述 在介绍Component之前,我们首先来了解ComponentName这个类:ComponentName与Intent同位于android.content包下,我们从Android官 ...

  7. java设计模式 策略模式Strategy

    本章讲述java设计模式中,策略模式相关的知识点. 1.策略模式定义 策略模式,又叫算法簇模式,就是定义了不同的算法族,并且之间可以互相替换,此模式让算法的变化独立于使用算法的客户.策略模式属于对象的 ...

  8. UITableViewController和XML解析还有地图的简单结合

    然后我的代码就按照上面的这个顺序输出. #import <Foundation/Foundation.h> #import <MapKit/MapKit.h> @interfa ...

  9. php new self()和new static()

    class A { public static function get_self() { return new self(); } public static function get_static ...

  10. yii2实战教程之第一个Yii程序

    之前考虑过要不要砍掉该章节,直接上手教你搭建简单的博客系统.出于实战基础加之自C语言的书籍出版以来,几乎所有的编程书籍都讲述了一个Hello World的例子作为开始.虽然我们仅仅是学习Yii2,但是 ...