/*
定义一个二维数组: 
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. 理解CSV文件以及ABAP中的相关操作

    在很多ABAP开发中,我们使用CSV文件,有时候,关于CSV文件本身的一些问题使人迷惑.它仅仅是一种被逗号分割的文本文档吗? 让我们先来看看接下来可能要处理的几个相关组件的词汇的语义. Separat ...

  2. 基于WebGL的三维地形渲染

    1.生成WebMap页面 #!/usr/bin/env python # -*- coding: utf-8 -*- import subprocess from jinja2 import Envi ...

  3. web安全攻防----环境搭建篇

    1.安装虚拟机vMware. 2.在虚拟机上安装kali系统. *Kali为linux操作系统的一个发行版. 3.安装Xshell *Xshell是一个强大的安全终端模拟软件,它支持SSH1, SSH ...

  4. IOS开发基础知识--碎片30

    1:ios 相册操作 ALAssetsLibrary 知识点 a ALAssetsLibrary 实例为我们提供了获取相册(照片app)中的图片和视频的功能.在ios8 photos framewor ...

  5. 初学RunLoop

    RunLoop 运行循环,跑圈 可以看出每条线程都有一个与之对应的RunLoop对象 主线程的RunLoop已经自动创建好了,子线程的RunLoop需要主动创建. 基本作用:保持程序的持续运行 处理A ...

  6. Microsoft SQL Server 2005 Service fails to start

    今天碰到一雷死人的事情,在Windows Server 2012 R2上安装SQL SERVER 2005标准版过程中一直遇到"The SQL Server service failed t ...

  7. ORACLE调整SGA_TARGET值耗费时间长案例

    在一数据库版本为(标准版)Oracle Database 10g Release 10.2.0.4.0 - 64bit Production 的服务器上调整 sga_target时,遇到命令执行了非常 ...

  8. SQL2005/2008 无法连接错误

    SQL2005/2008 .或者是localhost可以访问,但是127.0.0.1或者IP无法访问 打开[SQL Server 配置管理器](如果是MSSQL2005,在运行中输入SQLServer ...

  9. 2016 最佳 Linux 发行版排行榜

    2015年,不管在企业市场还是个人消费市场都是 Linux非常重要的一年.作为一个自2005年起就开始使用 Linux的 Linuxer ,我门见证了 Linux在过去十年的成长.2016 Linux ...

  10. 好压(HaoZip)的命令行模式用法介绍

    好压压缩软件,又叫“2345好压”,是一款国产的优秀压缩软件,目前是免费的,据官网介绍,该软件永久免费.官网地址:http://haozip.2345.com/ 本文主要对该软件的命令行模式用法进行介 ...