BFS迷宫搜索路径
#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迷宫搜索路径的更多相关文章
- Java数据结构之回溯算法的递归应用迷宫的路径问题
一.简介 回溯法的基本思想是:对一个包括有很多结点,每个结点有若干个搜索分支的问题,把原问题分解为对若干个子问题求解的算法.当搜索到某个结点.发现无法再继续搜索下去时,就让搜索过程回溯(即退回)到该结 ...
- 算法竞赛——BFS广度优先搜索
BFS 广度优先搜索:一层一层的搜索(类似于树的层次遍历) BFS基本框架 基本步骤: 初始状态(起点)加到队列里 while(队列不为空) 队头弹出 扩展队头元素(邻接节点入队) 最后队为空,结束 ...
- gcc 编译时 include 搜索路径
这是一个不复杂的问题:但是网上很多回答都不全面:偶找了一个比较全面的(测试过): 引用http://blog.csdn.net/fjb2080/archive/2010/01/23/5247494.a ...
- gcc编译时头文件和库文件搜索路径
特殊情况:用户自定义的头文件使用#include"mylib"时,gcc编译器会从当前目录查找头文件 一.头文件 gcc 在编译时寻找所需要的头文件 : ※搜寻会从-I开始( ...
- 显示python已安装模块及路径,添加修改模块搜索路径
在python交互模式下输入: help('modules') #可以显示出已安装的模块 在python交互模式下输入: import sys sys.path #可以显示出模块搜索路径 增加搜索路径 ...
- Java import以及Java类的搜索路径
如果你希望使用Java包中的类,就必须先使用import语句导入.import语句与C语言中的 #include 有些类似,语法为: import package1[.package2-].cl ...
- 【转载】Linux动态库搜索路径的技巧
转自:http://soft.chinabyte.com/os/232/11488732_2.shtml 众所周知,Linux动 态库的默认搜索路径是/lib和/usr/lib.动态库被创建后,一般都 ...
- 【转载】Linux下动态共享库加载时的搜索路径详解
转载自:http://www.eefocus.com/article/09-04/71617s.html 对动态库的实际应用还不太熟悉的读者可能曾经遇到过类似“error while loading ...
- Linux动态库的搜索路径
下面是目录结构: pengdl@localhost:~$ tree test/test/├── fun.c├── Fun.h└── t1 └── main.c 1 directory, 3 fi ...
随机推荐
- tp价格除以100
{$vo['money_num']/100} //正确 {$vo.money_num/100} //错误
- 怎样安装Command Line Tools in OS x Mavericks&Yosemite(Without xcode)--转载
How to Install Command Line Tools in OS X Mavericks & Yosemite (Without Xcode) Mac users who pre ...
- libuv的多线程之间传递消息
官网上给出的例子http://nikhilm.github.io/uvbook/threads.html#inter-thread-communication,中文理解在后边 Inter-thread ...
- 使用quartz.jar 、quartz-jobs.jar 实现定时任务 。实现 定时采集 接口数据
前言 定时任务管理,在java中有很多种的方式 ,有java自带的注解方式@Scheduled 等 ,现在我要说的是一种也是使用比较广泛的一种quartz管理 使用此类 需要的加jar包有 quar ...
- 1.redis设计与实现--简单动态字符串
1.redis没有使用c语言的字符串表示,而是使用更加适合自己的SDS(simple dynamic string),简单动态字符串,结构如下: 2.sys与c字符串的对比: 3.总结: redis采 ...
- [USACO07MAR]黄金阵容均衡Gold Balanced L…
https://www.luogu.org/problem/show?pid=1360 题目描述 Farmer John's N cows (1 ≤ N ≤ 100,000) share many s ...
- 2017.5.27 NOIP模拟赛(hzwer2014-5-16 NOIP模拟赛)
期望得分:100+100+60+30=290 实际得分:100+20+60+0=180 当务之急:提高一次正确率 Problem 1 双色球(ball.cpp/c/pas) [题目描述] 机房来了新一 ...
- protoc
平台安装: 在window 平台使用的工具protoc.zip linux平台的安装方式. 执行在windos平台上执行生成java代码命令: protoc --java_out=./ Keyword ...
- 用C++写一个没人用的ECS
github地址:https://github.com/yangrc1234/Resecs 在做大作业的时候自己实现了一个简单的ECS,起了个名字叫Resecs. 这里提一下一些实现的细节,作为回顾. ...
- 【洛谷 P1772】 [ZJOI2006]物流运输(Spfa,dp)
题目链接 \(g[i][j]\)表示不走在\(i\text{~}j\)时间段中会关闭的港口(哪怕只关\(1\)天)从\(1\)到\(m\)的最短路. \(f[i]\)表示前\(i\)天的最小花费.于是 ...