/*
定义一个二维数组: 
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. mysql时间加减函数

    先来看一个例子: select now(),now()+0; 可以看到now()函数可以返回一个时间也可以返回一个数字,就看大家如何使用.如果相对当前时间进行增加或减少,并不能直接加上或减去一个数字而 ...

  2. 列表屏幕(List Screen)

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  3. iOS之2016面试题二

    前言 招聘高峰期来了,大家都非常积极地准备着跳槽,那么去一家公司面试就会有一堆新鲜的问题,可能不会,也可能会,但是了解不够深.本篇文章为群里的小伙伴们去要出发公司的笔试题,由笔者整理并提供笔者个人参考 ...

  4. 【代码笔记】iOS-获取字符串的宽度,高度

    一,代码. - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, ...

  5. 在 ASP.NET MVC 中充分利用 WebGrid (microsoft 官方示例)

    在 ASP.NET MVC 中充分利用 WebGrid https://msdn.microsoft.com/zh-cn/magazine/hh288075.aspx Stuart Leeks 下载代 ...

  6. sizeof与strlen的区别

    1 sizeof是操作符,而strlen是库函数: 2 sizeof的参数可以为任意变量或类型,而strlen必须以char*做参数,且字符串必须以‘/0’结尾: 3 数组名用作sizeof参数时不会 ...

  7. 初识sqoop

    Sqoop 产生背景 Sqoop 的产生主要源于以下几种需求: 1.多数使用 Hadoop 技术处理大数据业务的企业,有大量的数据存储在传统的关系型数据库(RDBMS)中. 2.由于缺乏工具的支持,对 ...

  8. JavaScript:内存泄露、性能调优

    1.在进行JS内存泄露检查之前,先要了解JS的内存管理: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Memory_Manageme ...

  9. JVM之Parallel Old收集器

    Parallel Scavenge的老年代版本 标记-整理算法 注重吞吐量及cpu资源敏感环境.

  10. log4j配置详解

    Log4J的配置文件(Configuration File)就是用来设置记录器的级别.存放器和布局的,它可接key=value格式的设置或xml格式的设置信息.通过配置,可以创建出Log4J的运行环境 ...