迷宫问题,二维数组模拟迷宫;

这个程序使用顺序栈来解决二维数组模拟的迷宫问题,通过判断上下左右四个方向是否可以通过,寻找从起点到终点的路径。如果迷宫无通路,程序会输出相应提示。用户可以输入迷宫的宽度和高度,然后逐行输入迷宫的数据。程序会显示迷宫模型并尝试找到通路。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

// Datastructure2.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "iostream"

using namespace std;
static int w;
static int h;
static int column = 0;
static int row = 0;
static int **A;		//声明二级指针A,指向一个指针数组

//***************************************************************************
//顺序栈实现
//***************************************************************************
template<class T>
class ArrayStack
{
private:
	int size;//栈的大小
	int tos;//栈顶索引
	T*contain;
public:
	ArrayStack():size(0),tos(-1),contain(NULL){}
	ArrayStack(int maxsize);
	~ArrayStack();
	void Push(const T&element);
	T& GetTop();
	T&Pop();
	bool IsEmpty();
	void MakeEmpty();
};
template<class T>
ArrayStack<T>::ArrayStack(int maxsize)
{
	size=maxsize;
	tos=-1;
	contain=new T[maxsize];
}
template<class T>
ArrayStack<T>::~ArrayStack()
{
	delete contain;
	tos=-1;
	size=0;
}
template<class T>
void ArrayStack<T>::Push(const T&element)
{
	if(tos==size-1)
	{
		//cout<<"栈已经满"<<endl;
		return;
	}
	else
	{
		tos++;
		contain[tos]=element;
	}
}
template<class T>
T& ArrayStack<T>::GetTop()
{
	return contain[tos];
}
template<class T>
T& ArrayStack<T>::Pop()
{
	if(tos==-1)
	{
		int i=0;
		//cout<<"栈为空"<<endl;
		return contain[tos--];
	}
	else
	{
		return contain[tos--];
	}
}
template<class T>
bool ArrayStack<T>::IsEmpty()
{
	if(tos==-1)
		return true;
	else
		return false;
}
template<class T>
void ArrayStack<T>::MakeEmpty()
{
	delete contain;
	tos=-1;
	size=0;
}

bool Right(int column,int row){
	if (column + 1 < w) {
			if (A[column + 1][row] == 0) {
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
}

bool Left(int column,int row){
	if (column - 1 >= 0) {
			if (A[column - 1][row] == 0) {
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
}

bool Down(int column,int row){
	if (row + 1 < h) {
			if (A[column][row + 1] == 0) {
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
}

bool Up(int column,int row){
	if (row - 1 >=
#include <stdio.h> #include <stdlib.h> #include <graphics.h> #include <time.h> #include <conio.h> #define MAXSIZE 20 typedef struct { int x; int y; }PosType; typedef struct { int di1; int di2; int di3; int di4; }Direct; typedef int MazeElem; typedef struct { MazeElem color[MAXSIZE+2][MAXSIZE+2]; PosType start; PosType end; }MazeType; MazeType mazes; int unit,rows,columns,ratio; PosType offset; PosType NextPos( PosType curpos,int di) { switch(di) {case 1: curpos.y++;break; /*右*/ case 2: curpos.x++;break; /*下*/ case 3: curpos.y--;break; /*左*/ case 4: curpos.x--;break;/*上*/ } return curpos; } void Near(PosType start,PosType end) { int i=0; PosType curpos; while(++i<=4) { curpos=NextPos(start,i); if(curpos.x==end.x && curpos.y==end.y) { printf(" success!\n"); getch(); closegraph(); exit(0); } } } void direction(Direct *direct,PosType start,PosType end) { if(start.y<=end.y) { if(start.x<=end.x) { if(end.y-start.y<=end.x-start.x) { direct->di1=2; /*左*/ direct->di2=3; /*下*/ direct->di3=4; /*上*/ direct->di4=1; /*右*/ } else { direct->di1=1; direct->di2=2; direct->di3=3; direct->di4=4; } } else { if(end.y-start.y<=start.x-end.x) { direct->di1=4; /*上 */ direct->di2=1; /*右*/ direct->di3=3; /*左 */ direct->di4=2; /*下*/ } else { direct->di1=1; direct->di2=4; direct->di3=3; direct->di4=2; } } } else { if(start.x<=end.x) { if(start.y-end.y<=end.x-start.x) { direct->di1=2; direct->di2=3; direct->di3=1; direct->di4=4; } else { direct->di1=3; direct->di2=2; direct->di3=4; direct->di4=1; } } else { if(start.y-end.y<=start.x-end.x) { direct->di1=4; direct->di2=3; direct->di3=1; direct->di4=2; } else { direct->di1=3; direct->di2=4; direct->di3=2; direct->di4=1; } } } } void Init(void)/*图形初始化*/ { int gd=DETECT,gm; initgraph(&gd,&gm,"c:\\tc"); cleardevice(); } void CreateMaze() { int i,j; char m ; int gdriver, gmode; detectgraph(&gdriver, &gmode); registerbgidriver(EGAVGA_driver); clrscr(); setbkcolor(2); settextstyle(0,0,2); outtextxy(30,60,"《**This program is designed by heqiuyun**》" ); settextstyle(0,0,1); outtextxy(60,100,"Operate handbook:"); outtextxy(70,140,"1.Black square denotations unpassable;" ); outtextxy(70,180,"2.White square denotations passable;" ); outtextxy(70,220,"3.Ratio mens white squares&&black squares" ); outtextxy(70,260,"4.Input rows,colums and ratio "); scanf("{%d,%d,%d}",&rows,&columns,&ratio); if(rows>MAXSIZE || rows<1 || columns>MAXSIZE ||columns<1) { getchar(); clrscr(); outtextxy(30,60,"attention! "); outtextxy(60,110,"1.1<=rows<=MAXSIZE"); outtextxy(60,160,"2.1<=columns<=MAXSIZE"); outtextxy(60,210,"3.MAXSIZE is defined 20"); outtextxy(60,240," Please enter the \"enter\" key" ); } getchar(); rows=20; columns=20; ratio=2; for(i=1;i<=MAXSIZE;i++) for(j=1;j<=MAXSIZE;j++) mazes.color[i][j]=WHITE; for(i=1;i<=rows;i++) for(j=1;j<=columns;j++) if(random(3)%(ratio)) mazes.color[i][j]=BLACK; } void PointColor(PosType curpos) { setcolor(GREEN); setfillstyle(SOLID_FILL,mazes.color[curpos.y][curpos.x]); bar((curpos.x+offset.x)*unit,(curpos.y+offset.y)*unit,(curpos.x+offset.x+1)*unit,(curpos.y+offset.y+1)*unit); rectangle((curpos.x+offset.x)*unit,(curpos.y+offset.y)*unit,( curpos.x+offset.x+1)*unit,(curpos.y+offset.y+1)*unit); } void DrawMaze() { int i,j,k,m,n; PosType curpos; clrscr(); m=getmaxx(); n=getmaxy(); k=(m>n?n:m); unit=k/(MAXSIZE*2); m=m/unit; n=n/unit; offset.x=m/4; offset.y=n/4; for(i=1;i<=rows;i++) for(j=1;j<=columns;j++) { curpos.x=j; curpos.y=i; PointColor(curpos); } printf("\n red square denotations maze start;yellow square denotations maze end.\n define maze start and end using (rowth,columnth).\n for example (2,3),(7,8) : "); scanf("(%d,%d),(%d,%d)",&mazes.start.y,&mazes.start.x,&mazes.end.y,&mazes.end.x); if(!mazes.start.y||!mazes.start.y||!mazes.end.y||!mazes.end.x) { mazes.start.y=1; mazes.start.x=1; mazes.end.y=20; mazes.end.x=20 ;} else if(mazes.start.x<1||mazes.start.x>columns||mazes.start.y<1||mazes.start.y>rows||mazes.end.x<1||mazes.end.x>columns||mazes.end.y<1||mazes.end.y>rows) { printf(" attention! 1<=x<=rows,1<=y<=columns, you define rows=%d,columns=%d",rows,columns); getch(); exit(0); } mazes.color[mazes.start.y][mazes.start.x]=RED; PointColor(mazes.start); sleep(1); mazes.color[mazes.end.y][mazes.end.x]=YELLOW; PointColor(mazes.end); sleep(1); i=0; while(++i<=4) { curpos=NextPos(mazes.end,i); if(mazes.color[curpos.y][curpos.x]==WHITE) break; } if(i==5) { printf(" maze end unpassable!\n"); getch(); exit(0); } } void MazePath(PosType start,PosType end) { Direct direct; if(start.x==end.x && start.y==end.y) { printf(" success!\n"); getch(); closegraph(); exit(0); } if(mazes.color[start.y][start.x]==RED ||mazes.color[start.y][start.x]==WHITE) { if(mazes.color[start.y][start.x]==WHITE) { mazes.color[start.y][start.x]=BLUE; PointColor(start); sleep(1); } Near(start,end); direction(&direct,start,end); MazePath(NextPos(start,direct.di1),end); MazePath(NextPos(start,direct.di2),end); MazePath(NextPos(start,direct.di3),end); MazePath(NextPos(start,direct.di4),end); if(mazes.color[start.y][start.x]!=RED) { mazes.color[start.y][start.x]=DARKGRAY; PointColor(start); sleep(1); } } } main() { Init(); CreateMaze(); DrawMaze(); MazePath(mazes.start,mazes.end); printf(" failed!\n"); getch(); closegraph(); }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值