求一个迷宫源代码急用!谢谢各位大侠了,没时间看图算法和数据结构了
选择手动或者自动生成一个n×m的迷宫,将迷宫的左上角作入口,右下角作出口,设“0”为通路,“1”为墙,即无法穿越。假设从起点出发,目的为右下角终点,可向“上、下、左、右、左上、左下、右上、右下”8个方向行走。如果迷宫可以走通,则用“■”代表“1”,用“□”代表“0”,用“☆”代表行走迷宫的路径。输出迷宫原型图、迷宫路线图以及迷宫行走路径。如果迷宫为死迷宫,则只输出迷宫原型图。
[解决办法]
- C/C++ code
#include <iostream>#include <cmath>using namespace std;char maze[100][100];int N,M,T;int ptStartX,ptStartY,ptDoorX,ptDoorY;bool bFound;void Recur(int x,int y){ char cTmp=maze[x][y]; if(bFound) return; if ( x==ptDoorX&&y==ptDoorY) { bFound=true; cout<<"YES"<<endl; return ; } maze[x][y]='X'; if (bFound==false && x>=1 && (maze[x-1][y]=='0')) { Recur(x-1,y); } if (bFound==false &&x+1<N && (maze[x+1][y]=='0')) { Recur(x+1,y); } if (bFound==false &&y>=1 && (maze[x][y-1]=='0')) { Recur(x,y-1); } if (bFound==false &&y+1<M && (maze[x][y+1]=='0')) { Recur(x,y+1); } if (bFound==false&& x>=1&&y>=1&&maze[x-1][y-1]=='0') { Recur(x-1,y-1); } if (bFound==false&&x>=1&&y<M-1&&maze[x-1][y+1]=='0') { Recur(x-1,y+1); } if (bFound==false &&x+1<N&&y>=1&&maze[x+1][y-1]=='0') { Recur(x+1,y-1); } if (bFound==false&&x+1<N&&y+1<M&&maze[x+1][y+1]=='0') { Recur(x+1,y+1); } maze[x][y]=cTmp;}int main(void){ int i,j; do { bFound=false; cin>>N>>M; if (N==0 && M==0 &&T==0) { break; } ptStartX=0; ptStartY=0; ptDoorX=N-1; ptDoorY=M-1; for (i=0;i<N;++i) for (j=0;j<M;++j) { cin>>maze[i][j]; } Recur(ptStartX,ptStartY); if (bFound==false) {//输出迷宫地图,表示没有出路 cout<<"NO"<<endl; } else {//有出路,输出 } } while (1); return 0;}
[解决办法]
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MaxSize 100
int Ma[20][20]={0};
typedef struct
{
int row,col;
int di;
}ElemType;
typedef struct
{
ElemType data[MaxSize];
int top;
}Stack;
typedef struct
{
ElemType data[MaxSize];
int rear;
int front;
}Queue;
int InitStack(Stack *s);
int Push(Stack *s,ElemType e);
int Pop(Stack *s,ElemType *e);
void InitQueue(Queue *q);
int EnQueue(Queue *q,ElemType e);
int InitMaze(int r,int c);
void PrintMaze(int r,int c);
int MazePath(Queue *q,int r,int c);
void PrintPath(Queue *q,Stack *s);
void main()
{
Stack s;
Queue q;
int r,c,i;
clrscr();
InitStack(&s);
InitQueue(&q);
printf("input row and col:\n");
scanf("%d%d",&r,&c);
InitMaze(r,c);
PrintMaze(r,c);
if(MazePath(&q,r,c))
PrintPath(&q,&s);
else
printf("not find!\n");
printf("%d\n",MazePath(&q,r,c));
printf("%d\n",q.rear);
for(i=0;i<=q.rear-1;i++)
printf("%d %d %d %d\n",i,q.data[i].row,q.data[i].col,q.data[i].di);
}
/*-------stack--------*/
int InitStack(Stack *s)
{
s->top=-1;
return 1;
}
int Push(Stack *s,ElemType e)
{
if(s->top==MaxSize-1)
return 0;
else
s->data[++s->top]=e;
return 1;
}
int Pop(Stack *s,ElemType *e)
{
if(s->top==-1)
return 0;
else
*e=s->data[s->top--];
return 1;
}
/*---------Queue----------*/
void InitQueue(Queue *q)
{
int i;
q->front=0;
q->rear=-1;
for (i=0;i<MaxSize;i++)
q->data[i].di=-2;
}
int EnQueue(Queue *q,ElemType e)
{
if(q->rear==MaxSize-1)
return 0;
else
q->data[++q->rear]=e;
return 1;
}
/*----------Maze----------*/
int InitMaze(int r,int c)
{
int i,j;
srand(time(NULL));
if(r<=3||c<=3)
exit(0);
for(i=0;i<r;i++)
for(j=0;j<c;j++)
if(i==0||j==0||i==r-1||j==c-1)
Ma[i][j]=1;
else
Ma[i][j]=rand()%2;
Ma[1][1]=0;
Ma[r-2][c-2]=0;
return 1;
}
void PrintMaze(int r,int c)
{
int i,j;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
printf("%3d",Ma[i][j]);
printf("\n\n");
}
}
int MazePath(Queue *q,int r,int c)
{
ElemType e;
int i=1,j=1,n=0,flag=1;
e.row=i;
e.col=j;
e.di=-1;
EnQueue(q,e);
while(flag)
{
Ma[q->data[n].row][q->data[n].col]=-1;
for(i=q->data[n].row-1;i<=q->data[n].row+1;i++)
for(j=q->data[n].col-1;j<=q->data[n].col+1;j++)
if(Ma[i][j]==0)
{
e.row=i;
e.col=j;
e.di=n;
EnQueue(q,e);
Ma[i][j]=-1;
}
n++;
if(q->data[q->rear].row==r-2&&q->data[q->rear].col==c-2)
return 1;
else
if(q->data[n].di==-2) flag=0;
}
return 0;
}
void PrintPath(Queue *q,Stack *s)
{
int di;
ElemType e;
Push(s,q->data[q->rear]);
di=q->data[q->rear].di;
while(di!=-1)
{
Push(s,q->data[di]);
di=q->data[di].di;
}
printf("row col\n");
while(s->top+1)
{
Pop(s,&e);
printf("%3d%3d\n",e.row,e.col);
}
}
[解决办法]
- C/C++ code
#include <iostream>using namespace std;#include "SqStack.h"#define MAXSIZE 30 //设迷宫的最大行列为30struct PosType //迷宫坐标位置类型{ int x; //行值 int y; //列值};struct DataType //栈的元素类型{ PosType seat; //点在迷宫中的"坐标位置" int di; //从此点走向下一点的"方向"(0~3表示东~北)};class Maze{public: Maze(int, int); ~Maze(); bool MazePath(PosType, PosType); void Input(); void Print();private: PosType NextPos(PosType, int); int **m_maze; //迷宫数组 int m_row; //迷宫的行数 int m_col; //迷宫的列数};//构造函数Maze::Maze(int m, int n){ m_row = m + 2; m_col = n + 2; m_maze = new int * [m_row]; for (int i = 0; i < m_row; i++) m_maze[i] = new int [m_col];}//Maze//析构函数Maze::~Maze(){ for (int i = 0; i < m_row; i++) if (m_maze[i] != NULL) delete [] m_maze[i]; if (m_maze != NULL) delete[] m_maze;}//~Maze//若迷宫m_maze中存在从入口start到出口end的通路,则求得一条路径//存放在栈中(从栈底到栈顶),并返回true;否则返回false。通过路径为足迹(以路径上的序号表示)bool Maze::MazePath(PosType start, PosType end){ SqStack<DataType> path(MAXSIZE); //创建栈 PosType curpos; DataType e; curpos = start; int curstep = 1; //当前足迹,初值为1 do { if (m_maze[curpos.x][curpos.y] == 0) { //当前位置可以通过,即是未曾走到过的点 m_maze[curpos.x][curpos.y]=curstep; //留下足迹,迷宫m_maze的curpos点变为序号curstep e.seat.x = curpos.x; e.seat.y = curpos.y; e.di = 0; path.Push(e); //入栈当前位置及方向 curstep++; //足迹加1 if(curpos.x == end.x && curpos.y == end.y) //到达终点(出口) return true; curpos = NextPos(curpos, e.di); } else { //当前位置不能通过 if (!path.Empty()) { e = path.Top(); //退栈到前一位置 path.Pop(); curstep--; while (e.di == 3 && !path.Empty()) { //该位置已到最后一个方向(北) m_maze[e.seat.x][e.seat.y] = -1; //留下不能通过的标记(-1) e = path.Top(); //退回一步 path.Pop(); curstep--; } if (e.di < 3) { //没到最后一个方向(北) e.di++; //换下一个方向探索 path.Push(e); curstep++; curpos = NextPos(e.seat, e.di); //设定当前位置是该新方向上的相邻点 } } } } while (!path.Empty()); return false;}//MazePath//输入迷宫void Maze::Input(){ int i, j; cout << "请输入" << m_row - 2 << "*" << m_col - 2 << "的迷宫:" << endl; for (i = 1; i <= m_row - 2; i++) for (j = 1; j <= m_col - 2; j++) cin >> m_maze[i][j]; //周边设围墙,定义周边值为1(墙) for (i = 0; i < m_row; i++) { m_maze[i][0] = 1; //行周边 m_maze[i][m_col - 1] = 1; } for (j = 1; j < m_col - 1;j++) { m_maze[0][j] = 1; //列周边 m_maze[m_row - 1][j] = 1; }}//Input//输出迷宫的解void Maze::Print(){ int i,j; for (i = 1; i < m_row - 1; i++) { for (j = 1; j < m_col - 1; j++) if (m_maze[i][j] >= 0 && m_maze[i][j] < 10) //输出对齐 cout << " " << m_maze[i][j] ; else cout << " " << m_maze[i][j] ; cout << endl; }}//Print//根据当前点的位置c及移动方向d,返回下一位置,移动方向,依次为东南西北PosType Maze::NextPos(PosType c, int d){ PosType direct[4] = {{0,1}, {1,0}, {0,-1}, {-1,0}}; //{行增量,列增量} c.x += direct[d].x; c.y += direct[d].y; return c;}//NextPosint main(){ PosType begin, end; int m, n; cout << "请输入迷宫的行数,列数:"; cin >> m >> n; Maze maze(m, n); maze.Input(); cout << "请输入入口位置(行数,列数):" ; cin >> begin.x >> begin.y; cout << "请输入出口位置(行数,列数):"; cin >> end.x >> end.y; if (maze.MazePath(begin,end)) { //求得一条通路 cout << "此迷宫从入口到出口的一条路径如下:" << endl; maze.Print(); //输出此通路 } else cout << "此迷宫没有从入口到出口的路径" << endl; system("pause"); return 0;}
[解决办法]
#include<stdio.h>
#define MAXSIZE 100
#define M 10
#define N 10
struct
{
int i; //当前方块的行号
int j;//当前方块的列号
int di;//下一个可走的方位的方位号
}st[MAXSIZE];
int top=-1; //初始化栈指针
void mgpath(int mg[10][10])
{
int i,j,di,find,k;
top++; //初始方块进栈
st[top].i=1;
st[top].j=1;
st[top].di=-1;
mg[1][1]=-1;
while(top>-1) //栈不空时循环
{
i=st[top].i;
j=st[top].j;
di=st[top].di;
if(i==M-2&&j==N-2)//找到出口
{
printf("迷宫路径如下:\n");
for(k=0;k<=top;k++)
{
printf("(%d,%d)",st[k].i,st[k].j);
if((k+1)%5==0)
printf("\n");
}
printf("\n");
}
find=0;
while(di<4&&find==0)//找下一块可走方块 顺时针探索
{
di++;
switch(di)
{
case 0:i=st[top].i-1;j=st[top].j;break;
case 1:i=st[top].i; j=st[top].j+1;break;
case 2:i=st[top].i+1;j=st[top].j;break;
case 3:i=st[top].i;j=st[top].j-1;break;
}
if(mg[i][j]==0)
find=1;
}
if(find==1)//找到可走的方块
{
st[top].di=di;//修改原栈元素的di值
top++;
st[top].i=i;
st[top].j=j;
st[top].di=-1;//重置di
mg[i][j]=-1;
}
else //没有路径可走 退栈
{
mg[st[top].i][st[top].j]=3;
top--;
}
}
printf("没有路径可走!\n");
}
void main()
{
int mg[10][10]=
{
{1,1,1,1,1,1,1,1,1,1},
{1,0,0,1,0,0,0,1,0,1},
{1,0,0,1,0,0,0,1,0,1},
{1,0,0,0,0,1,1,0,0,1},
{1,0,1,1,1,0,0,0,0,1},
{1,0,0,0,1,0,0,0,0,1},
{1,0,1,0,0,0,1,0,0,1},
{1,0,1,1,1,0,1,1,0,1},
{1,1,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1}
};
mgpath(mg);
}
[解决办法]
#include<stdio.h>
#define MAXSIZE 100
#define M 10
#define N 10
struct
{
int i; //当前方块的行号
int j;//当前方块的列号
int di;//下一个可走的方位的方位号
}st[MAXSIZE];
int top=-1; //初始化栈指针
void mgpath(int mg[10][10])
{
int i,j,di,find,k;
top++; //初始方块进栈
st[top].i=1;
st[top].j=1;
st[top].di=-1;
mg[1][1]=-1;
while(top>-1) //栈不空时循环
{
i=st[top].i;
j=st[top].j;
di=st[top].di;
if(i==M-2&&j==N-2)//找到出口
{
printf("迷宫路径如下:\n");
for(k=0;k<=top;k++)
{
printf("(%d,%d)",st[k].i,st[k].j);
if((k+1)%5==0)
printf("\n");
}
printf("\n");
}
find=0;
while(di<4&&find==0)//找下一块可走方块 顺时针探索
{
di++;
switch(di)
{
case 0:i=st[top].i-1;j=st[top].j;break;
case 1:i=st[top].i; j=st[top].j+1;break;
case 2:i=st[top].i+1;j=st[top].j;break;
case 3:i=st[top].i;j=st[top].j-1;break;
}
if(mg[i][j]==0)
find=1;
}
if(find==1)//找到可走的方块
{
st[top].di=di;//修改原栈元素的di值
top++;
st[top].i=i;
st[top].j=j;
st[top].di=-1;//重置di
mg[i][j]=-1;
}
else //没有路径可走 退栈
{
mg[st[top].i][st[top].j]=3;
top--;
}
}
printf("没有路径可走!\n");
}
void main()
{
int mg[10][10]=
{
{1,1,1,1,1,1,1,1,1,1},
{1,0,0,1,0,0,0,1,0,1},
{1,0,0,1,0,0,0,1,0,1},
{1,0,0,0,0,1,1,0,0,1},
{1,0,1,1,1,0,0,0,0,1},
{1,0,0,0,1,0,0,0,0,1},
{1,0,1,0,0,0,1,0,0,1},
{1,0,1,1,1,0,1,1,0,1},
{1,1,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1}
};
mgpath(mg);
}
