栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C语言 实现可视化迷宫寻路 (广度优先算法)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C语言 实现可视化迷宫寻路 (广度优先算法)


本来是想用esayx写的 发现好像用不着,直接改的窗口背景颜色做的,后来还改进了鼠标绘制地图 ,地图大小可以设置,寻路的过程也可以设置放慢的速度,作为大一的一个实践小项目,第一次发csdn,和大家一起分享。

#include
#include
#include 
#include

#define RED 	1
#define YELLOW 	21
#define HYELLOW 22
#define GREEN 	3
#define HGREEN  32
#define BLUE 	4
#define PURPLE	5
#define GRAY	6
#define BLACK	7
#define WHITE	8

#define START	1
#define END		-2
#define	WALL	-1
#define WAY		0 

#define YES		1
#define NO		0
#define SLEEPTIME 0
struct Size{//尺寸 
	int width;
	int height;	
};
struct MazeMap{//地图 
	Size size;
	short** EachLine;//该数组存放每一行的头指针 
}; 
struct NodeIndex{//作为坐标标记出入队列 
	int X;
	int Y;
	NodeIndex* nextnode;
};
struct Queue{//链队列 
	NodeIndex* head;
	NodeIndex* tail;
	int num;
};
void InitDataQueue(Queue &indexqueue){//初始化队列 
	indexqueue.head = NULL;
	indexqueue.tail = NULL;
	indexqueue.num = 0;
} 
NodeIndex OutputData(Queue &indexqueue){
	NodeIndex nodeindex;
	NodeIndex* p;
	nodeindex.nextnode = NULL;
	nodeindex.X = -1;
	nodeindex.Y = -1;
	if(indexqueue.num == 0){
		return nodeindex;
	}
	nodeindex = *indexqueue.head;
	p = indexqueue.head->nextnode;
	free(indexqueue.head);
	indexqueue.head = p;
	indexqueue.num--;
	return nodeindex;
}
void InputData(Queue &indexqueue,NodeIndex nodeindex){//尾插法直接数据入队 
	NodeIndex* p;
	p = (NodeIndex*)malloc(sizeof(NodeIndex));
	if(p == NULL){
		printf("系统忙,无法运行n"); 
		exit(-1);
	}
	*p = nodeindex;
	p->nextnode = NULL;
	if(indexqueue.num == 0){
		indexqueue.tail = p;
		indexqueue.head = indexqueue.tail;
	}else{
		indexqueue.tail->nextnode = p;
		indexqueue.tail = p;
	}
	indexqueue.num++;
}
void InitMazeMap(MazeMap &mazemap,Size size){//初始化地图数据 
	mazemap.size = size;
	mazemap.EachLine = (short**)malloc(sizeof(short*)*size.height);
	if(mazemap.EachLine == NULL){
		printf("系统忙,无法运行n"); 
		exit(-1);
	}
	short** p,start;
	p = mazemap.EachLine; 
	for(int i=0;i0 && pxy.x0){
					if(mazemap.EachLine[pxy.y][pxy.x] == START){
						Sset = 0;
					}else if(mazemap.EachLine[pxy.y][pxy.x] == END){
						Eset = 0;
					}
					mazemap.EachLine[pxy.y][pxy.x] = 0;
					GoTo_XY(pxy.x,pxy.y);
					Color(GRAY,handle_out);
					printf("  ");
					Color(WHITE,handle_out);
				}
				break;
			}
			case 112:{//鼠标定位 
				Rx = pxy.x;
				Ry = pxy.y;
				break;
			}
			case 115:{//起点绘制 
				if(Sset == 1){
					break;
				}
				pxy.x = (pxy.x-Rx)/16;
				pxy.y = (pxy.y-Ry)/16;
				if(pxy.x0 && value[i]50){
		printf("输入数据超出限制,请重新输入");
		scanf("%d",&map_size.width); 
	} 
	printf("高度度(10--50较为适宜):");
	scanf("%d",&map_size.height ); 
	while(map_size.height<10 || map_size.height>50){
		printf("输入数据超出限制,请重新输入");
		scanf("%d",&map_size.height); 
	}
	POINT start = {0,0};
	POINT end = {0,0}; 
	int srcstatue = NO,nodevalue = 0;
	SetHandle(handle_out,handle_in,map_size);
	HideCursor();
	InitMazeMap(mazemap,map_size);//初始化操作 
	InitDataQueue(indexqueue);
	for(int i=0;i
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/630264.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号