#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. go defer注意点,很容易出错的!!!

    1:defer是在return之前执行的  函数返回的过程是这样的:先给返回值赋值,然后调用defer表达式,最后才是返回到调用函数中 返回值 = xxx 调用defer函数 空的return fun ...

  2. 跟我一起写Makefile(五)

    使用变量———— 在Makefile中的定义的变量,就像是C/C++语言中的宏一样,他代表了一个文本字串,在Makefile中执行的时候其会自动原模原样地展开在所使用的地方.其与C/C++所不同的是, ...

  3. Codeforces 803E - Roma and Poker

    http://codeforces.com/problemset/problem/803/E E. Roma and Poker  time limit per test           2 se ...

  4. JS DOM之表格操作

    一个能给添加行的表格 <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type&qu ...

  5. CSS浏览器兼容问题集-第四部分

    12.FireFox下如何使连续长字段自动换行 众所周知IE中直接使用 word-wrap:break-word 就可以了, FF中我们使用JS插入 的方法来解决 <style type=&qu ...

  6. 【BZOJ】1875: [SDOI2009]HH去散步 矩阵快速幂

    [题意]给定n个点m边的无向图,求A到B恰好经过t条边的路径数,路径须满足每条边都和前一条边不同.n<=20,m<=60,t<=2^30. [算法]矩阵快速幂 [题解]将图的邻接矩阵 ...

  7. python并发编程之Queue线程、进程、协程通信(五)

    单线程.多线程之间.进程之间.协程之间很多时候需要协同完成工作,这个时候它们需要进行通讯.或者说为了解耦,普遍采用Queue,生产消费模式. 系列文章 python并发编程之threading线程(一 ...

  8. Python3 re模块正则表达式中的re.S

    在Python的正则表达式中,有一个参数为re.S.它表示"."(不包含外侧双引号,下同)的作用扩展到整个字符串,包括"\n".看如下代码: import re ...

  9. centos 6.5配置ftp服务器,亲测可用

    设置开机启动 1 chkconfig vsftpd on 启动服务 1 /sbin/service vsftpd start 配置FTP用户组/用户以及相应权限 添加用户组 1 groupadd ft ...

  10. shell脚本执行方式

    # BY THE WAY, 其实这块内容算是比较简单的,但是都比较常记得它最基本的两种方式,另外两种却忘记了 1. 利用sh或bash命令执行 sh test.sh bash test.sh 2. 在 ...