K - 迷宫问题
/*
定义一个二维数组:
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 - 迷宫问题的更多相关文章
- 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, ...
- [kuangbin带你飞]专题一 简单搜索 - K - 迷宫问题
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #i ...
- POJ 3984 迷宫问题
K - 迷宫问题 Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Sta ...
- 迷宫问题(bfs+记录路径)
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=105278#problem/K K - 迷宫问题 Time Limit:1000 ...
- poj3984《迷宫问题》暑假集训-搜索进阶
K - 迷宫问题 Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:65536KB 64bit ...
- django模型操作
Django-Model操作数据库(增删改查.连表结构) 一.数据库操作 1.创建model表
- hdu1728 逃离迷宫---转弯次数不超过k+BFS
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1728 题目大意: 给你一幅图,给出起点终点和最大转弯次数,判断是否能从起点到终点.'*'表示障碍物. ...
- POJ 2251 Dungeon Master(3D迷宫 bfs)
传送门 Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 28416 Accepted: 11 ...
- HDU 1272 小希的迷宫 并查集
小希的迷宫 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
随机推荐
- UITableView优化
作为iOS开发,UITableView可能是平时我们打交道最多的UI控件之一,其重要性不言而喻. 关于TableView,我想最核心的就是UITableViewCell的重用机制了. 简单来说呢就是当 ...
- SharePoint 2013 图文开发系列之代码定义列表
在SharePoint的开发中,用Visual Studio自定义列表是经常会用到的,因为很多时候,我们并不会手动创建列表,而手动创建列表在测试服务器和正式机之间同步字段,也很麻烦,所以我们经常用代码 ...
- Android-socket服务端断重启后,android客户端自动重连
今天研究这个问题搞了整整一天啊!终于出来了,不过我没有多大的成就感,为什么呢?因为这不是我的劳动成果.同样的问题,我却没想出来!心塞的很啊…… 不过还是要给大家分享一下,希望给大家带来帮助! 先声明一 ...
- iOS开发之功能模块--模糊效果
1.先介绍一个好用的实现模糊效果的框架:https://github.com/YouXianMing/UIImageBlur 2.iOS8 中 UIVisualEffectView 模糊效果的使用 , ...
- 最新版Android开发工具
最新版Android开发工具 JUN 27TH, 2014 Android Tools ADT Bundle ADT Bundle包含了Eclipse.ADT插件和SDK Tools,是已经集成好的I ...
- You cannot change a partition into an extended one or vice versa Delete it first
在Linux扩展LVM时,使用fdisk创建分区时,在磁盘上新建扩展分区(逻辑分区),修改分区格式,指定分区类型为8e时,报错"You cannot change a partition i ...
- Linux命令学习总结:dos2unix - unix2dos
命令简介: dos2unix是将Windows格式文件转换为Unix.Linux格式的实用命令.Windows格式文件的换行符为\r\n ,而Unix&Linux文件的换行符为\n. dos2 ...
- 如何查看MapReduce执行的程序中的输出日志
我们开发程序的时候,好多人都喜欢用sysout输出内容来查看运行情况.但是在MR程序里写了之后,却不知道去哪里查找,可以参考这篇文章. 第一种方法,我们可以在MapReduce任务查看页面找到这些日志 ...
- union和union all用法
工作中,遇到同事之前写的oracle语句中有一个union all,并且很多地方都用到了.便在网上查了一下用法,以下是自己的理解. union (联合)将两个或者多个结果集合并. 在使用时,两个结果 ...
- 分布式搜索引擎Elasticsearch的查询与过滤
一.写入 先来一个简单的官方例子,插入的参数为-XPUT,插入一条记录. curl -XPUT 'http://localhost:9200/test/users/1' -d '{ "use ...