#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. varchar字段

    varchar  最长26000多,实际使用最好不要超过255,会占内存 可以考虑text

  2. oracle实用的sqlplus命令

    有时候难免没有工具,得自己手动输入sqlplus命令 执行SQL文件:@sql文件,例如:@/home/myuser/sql/test.sql查看数据库存在的存储过程:Select object_na ...

  3. C++ string的那些坑

    1. size_type find_first_of( const basic_string &str, size_type index = 0 ); 查找在字符串中第一个与str中的某个字符 ...

  4. Jquery中find与each方法使用详解

    本文实例讲述了jQuery中find与each方法用法.分享给大家供大家参考.具体如下: 一.find()方法 jquery选择器非常强大,利用css的命名规约,可以更快更方便的找出想要的元素. 图解 ...

  5. JAVA多线程提高十:同步工具CyclicBarrier与CountDownLatch

    今天继续学习其它的同步工具:CyclicBarrier与CountDownLatch 一.CyclicBarrier CyclicBarrier是一个同步辅助类,它允许一组线程互相等待,直到到达某个公 ...

  6. 面向对象 ( OO ) 的程序设计——理解对象

    本文地址:http://www.cnblogs.com/veinyin/p/7607938.html  1 创建自定义对象 创建自定义对象的最简单方法为创建 Object 的实例,并添加属性方法,也可 ...

  7. 利用Addon Domain和A记录使两个域名同时指向同一个网站

    今天碰到这样的需求:已有网站A.com, 以及新注册的域名B.net, 现需要将B.net指向与A.com相同的内容. 这里提出的方法是在空间后台添加Addon domain, 以及在域名B.net后 ...

  8. 自定义 feign 反序列化时间字符格式

    参考 : https://blog.csdn.net/forezp/article/details/73480304 feign client 默认配置类:默认的配置类为FeignClientsCon ...

  9. Spring提供的iBatis的SqlMap配置

    1.    applicationContext.xml <!-- Spring提供的iBatis的SqlMap配置--> <bean id="sqlMapClient&q ...

  10. Spring是什么+控制反转和依赖注入

    Spring是一个开源框架,是一个轻量级的控制反转(IOC)和面向切面(AOP)的容器框架. 原因: (1)通过控制反转(IOC)达到松耦合,IOC也就是把控制权交出去,在使用中直接得到对象 (2)提 ...