⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 maze1.cpp

📁 数据结构迷宫
💻 CPP
字号:
#include <iostream.h>
#include <iomanip.h>

int Maze[6][6];

//位置点数据结构

struct point
{
	int x;		    //point对应的x坐标
	int y;			//point对应的y坐标
	int comefrom;
	int type;
	point *pre;
};

//迷宫通道堆栈
class passage
{
public:
	passage();
	bool Push( point * );
	bool Pop();
	bool output();
private:
	point *base;
	point *top;
};
passage::passage()
{
	base = new point;
	top  = base;
}
bool passage::Push(point * e)
{
	e->pre = top->pre ;
	top->pre = e;
	return true;
}
bool passage::Pop()
{
	if( top == base )
	{
		cout<<"该堆栈为空。"<<endl;
		return false;
	}
	top = top->pre;
	return true;
}

//构造迷宫
void intputMaze()
{
	cout<<"*************构造迷宫阵*************"<<endl; 
	cout<<" 0 通道,1 障碍物,9 起始点,6 终点 "<<endl;
	cout<<"************************************"<<endl;
	for(int i=1; i<6; i++)
	{
		for(int j=1; j<6; j++)
		{
			Maze[i][j]=0;
		}
	}
	for(int j=0; j<7; j++)
	{
		Maze[0][j] = 1;
		Maze[6][j] = 1;
		Maze[j][0] = 1;
		Maze[j][6] = 1;
	}
	int x = rand(6) + 1;
	Maze[0][x] = 9;
	int y;
	for(int e=0; e<100; e++)
	{
		x = rand(6)+1;
		y = rand(6)+1;
		Maze[y][x] = 1
	}
	for(int i=1; i<6; i++)
	{
		for(int j=1; j<6; j++)
		{
			cout<<setw(3)<<Maze[i][j];
		}
		cout<<endl;
	}
	cout<<"-------------------------------------"<<endl;
}

//走迷宫
bool FindPath()
{
	int x;
	int y;
	int comefrom=1;
	int type=9;
	for(int i=0; i<7; i++)
	{
		if(Maze[0][i] == 9)
		x = i;
		y = 0;
	}
	passage test;
	point newtest;
	newtest.x = x;
	newtest.y = y;
	newtest.type = 9;
	test.Push( &newtest );
	for(;;)
	{
		if( Maze[y+1][x] == 0)
		{
		}
	}
	return true;
}

//测试
void main()
{
	cout<<"**************迷宫测试**************"<<endl;
	intputMaze();
	if(FindPath())
		cout<<"走出迷宫"<<endl;
	else
		cout<<"该迷宫无出口"<<endl;
	cout<<"测试完毕,敬请指教"<<endl;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -