#include<graphics.h>
#include<stdlib.h>
#include<conio.h>
#include<time.h>
#include<vector>
#include<queue>
#include<stack>
#include<iostream>
#include <algorithm> using namespace std; struct point{
//横坐标纵坐标
int x;
int y;
};
int **Maze; //初始化迷宫
point **Pre; //保存任意点在路径中的前一步
point move[]={{-,-},{-,},{-,},{,-},{,},{,-},{,},{,}}; //移动方向,横竖斜都可以,八个方向
char s1[]="YES";
char s2[]="NO"; void create_line(int row ,int column)//划线
{
int x, y; // 画格子
for (x=; x<=*(row+); x+=)
for (y=; y<=*(column+); y+=)
{
line(x, , x, *(column+));
line(, y,*(row+), y);
}
} void CreateTable(int row,int column)//初始化创建迷宫障碍区
{
int i,j;
srand((unsigned int)time(NULL));
for(i=; i<row+; i++)//创建迷宫,注意到用0表示可走,1表示墙,将整个输入的迷宫再用墙围着,处理的时候就不用特别注意边界问题
{
Maze[i][] = Maze[i][column+] = ;
}
for(j=; j<column+; j++)
{
Maze[][j] = Maze[row+][j] = ;
} for(i=;i<=row;i++)
for(j=;j<=column;j++)
{
Maze[i][j]=rand()%;
} for(i=;i<=row;i++)
for(j=;j<=column;j++)
{
if(Maze[i][j]==)
Maze[i][j]=rand()%;
} for(i=;i<=row+;i++)
for(j=;j<=column+;j++)
{
if(Maze[i][j]==)
{
setfillcolor(WHITE);
fillrectangle(*j,(+*i),(+*j),*i);
}
}
} bool MazePath(int row,int column,int x,int y){
//判断是否有路径从入口到出口,保存该路径(队列)
if(x == row && y == column)return true;
queue<point> q; //用于广度优先搜索
point now; //当前位置
now.x = x;
now.y = y;
q.push(now);
Maze[now.x][now.y] = -;
while(!q.empty()){
now = q.front();
q.pop();
for(int i=; i<; i++){
if(now.x + move[i].x == row && now.y + move[i].y == column){
Maze[now.x + move[i].x][now.y + move[i].y] = -;
Pre[row][column] = now;
return true;
}
if(Maze[now.x + move[i].x][now.y + move[i].y] == ){
point temp; //下个位置
temp.x = now.x + move[i].x;
temp.y = now.y + move[i].y;
q.push(temp);
Maze[temp.x][temp.y] = -;
Pre[temp.x][temp.y] = now; }
}
}
return false;
} void PrintPath(int row,int column){
//输出最短路径
point temp; //保存位置
stack<point> s; //保存路径序列
temp.x = row;
temp.y = column;
while(temp.x != || temp.y != ){
s.push(temp);
temp = Pre[temp.x][temp.y];
}
setfillcolor(YELLOW);
fillrectangle(*,(+*),(+*),*);
while(!s.empty()){
temp = s.top();
setfillcolor(YELLOW);
fillrectangle(*temp.y,(+*temp.x),(+*temp.y),*temp.x);
s.pop();
}
cout<<endl;
} void result(int row,int column)
{
if(MazePath(row,column,,))
{
//outtextxy(320,320,s1);
PrintPath(row,column);
}
else outtextxy(,,s2);
} void Init(int row,int column)
{
Maze = new int*[row + ];
Pre = new point*[row + ];
for(int i=; i<row+; i++)
{
Maze[i] = new int[column + ];
Pre[i] = new point[column + ];
}
} void main()
{
int row,column;
cin>>row>>column;
Init(row,column);
initgraph(,);
BeginBatchDraw();
setlinecolor(GREEN);//设置字体颜色
create_line(column,row);
CreateTable(row,column);
result(row,column);
FlushBatchDraw();
EndBatchDraw();
getch();
closegraph();
}

BFS迷宫搜索路径的更多相关文章

  1. Java数据结构之回溯算法的递归应用迷宫的路径问题

    一.简介 回溯法的基本思想是:对一个包括有很多结点,每个结点有若干个搜索分支的问题,把原问题分解为对若干个子问题求解的算法.当搜索到某个结点.发现无法再继续搜索下去时,就让搜索过程回溯(即退回)到该结 ...

  2. 算法竞赛——BFS广度优先搜索

    BFS 广度优先搜索:一层一层的搜索(类似于树的层次遍历) BFS基本框架 基本步骤: 初始状态(起点)加到队列里 while(队列不为空) 队头弹出 扩展队头元素(邻接节点入队) 最后队为空,结束 ...

  3. gcc 编译时 include 搜索路径

    这是一个不复杂的问题:但是网上很多回答都不全面:偶找了一个比较全面的(测试过): 引用http://blog.csdn.net/fjb2080/archive/2010/01/23/5247494.a ...

  4. gcc编译时头文件和库文件搜索路径

    特殊情况:用户自定义的头文件使用#include"mylib"时,gcc编译器会从当前目录查找头文件 一.头文件 gcc 在编译时寻找所需要的头文件 :    ※搜寻会从-I开始( ...

  5. 显示python已安装模块及路径,添加修改模块搜索路径

    在python交互模式下输入: help('modules') #可以显示出已安装的模块 在python交互模式下输入: import sys sys.path #可以显示出模块搜索路径 增加搜索路径 ...

  6. Java import以及Java类的搜索路径

    如果你希望使用Java包中的类,就必须先使用import语句导入.import语句与C语言中的 #include 有些类似,语法为:    import package1[.package2-].cl ...

  7. 【转载】Linux动态库搜索路径的技巧

    转自:http://soft.chinabyte.com/os/232/11488732_2.shtml 众所周知,Linux动 态库的默认搜索路径是/lib和/usr/lib.动态库被创建后,一般都 ...

  8. 【转载】Linux下动态共享库加载时的搜索路径详解

    转载自:http://www.eefocus.com/article/09-04/71617s.html 对动态库的实际应用还不太熟悉的读者可能曾经遇到过类似“error while loading ...

  9. Linux动态库的搜索路径

    下面是目录结构: pengdl@localhost:~$ tree test/test/├── fun.c├── Fun.h└── t1    └── main.c 1 directory, 3 fi ...

随机推荐

  1. width: calc(100% - 20px);

    width: calc(100% - 20px); css3 的 calc()函数.这里的意思是设置宽度比100%的宽度少20px. calc()函数用于动态计算长度值. calc()函数支持 &qu ...

  2. [LeetCode] 24. Swap Nodes in Pairs ☆

    Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2-& ...

  3. linux下应用crontab对mysql数据库进行定时备份

    linux下应用crontab对mysql数据库进行定时备份 @(编程) mysql数据库提供了备份命令mysqldump,可以结合crontab命令进行定时备份. 我写了一个mysqlbackup. ...

  4. Eclipse 重构功能的使用与重构快捷键

    重构是什么? 在代码写好之后改进它的设计. 重构分类:物理结构.类层次结构.类内部结构. 名称 快捷键 直译 作用范围 描述 Rename Alt + Shift + R     可以对任意变量.类. ...

  5. 解决 sun.security.validator.ValidatorException: PKIX path building failed

    今天用java HttpClients写爬虫在访问某Https站点报如下错误: sun.security.validator.ValidatorException: PKIX path buildin ...

  6. new Date('2014/04/30') 和 new Date('2014-04-30') 的区别

    new Date('2014/04/30') Wed Apr 30 2014 00:00:00 GMT+0800 (中国标准时间) new Date('2014-04-30'); Wed Apr 30 ...

  7. 在Unity中实现屏幕空间反射Screen Space Reflection(4)

    第四部分讲一下如何在2D屏幕空间步进光线. http://casual-effects.blogspot.com/2014/08/screen-space-ray-tracing.html 中的代码感 ...

  8. Xcode 获取本地IP

    // // // #define MAXADDRS 32 extern char *ip_names[MAXADDRS]; void InitAddresses(); void GetIPAddres ...

  9. python3学习笔记.1.初体验

    最近工作烦得很 就想找点儿别的事情来做,于是想到了学学python. 因为是vs2017,所以就在里面安装了. 第一个程序肯定是Hello World了. 新建一个python应用程序 代码只有一行 ...

  10. WAMP允许外部访问的修改方法

    apache配置文件httpd.conf里的 "Require local"改" Require all granted"