求一个迷宫源代码急用!谢谢各位大侠了,没时间看图算法和数据结构了

真你假我 2012-05-12 09:32:43
选择手动或者自动生成一个n×m的迷宫,将迷宫的左上角作入口,右下角作出口,设“0”为通路,“1”为墙,即无法穿越。假设从起点出发,目的为右下角终点,可向“上、下、左、右、左上、左下、右上、右下”8个方向行走。如果迷宫可以走通,则用“■”代表“1”,用“□”代表“0”,用“☆”代表行走迷宫的路径。输出迷宫原型图、迷宫路线图以及迷宫行走路径。如果迷宫为死迷宫,则只输出迷宫原型图。
...全文
301 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
牙牙学语 2012-05-17
  • 打赏
  • 举报
回复
躺着也中枪呀
ktaurus 2012-05-16
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 的回复:]

引用 7 楼 的回复:

楼上各位大神的代码怎么感觉好多?偶省事了,输入的东东直接在主函数里初始化了,自己可以改改就行了


你没有建立这个SqStack.h
[/Quote]

额 回复错了。。。
ktaurus 2012-05-16
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 的回复:]

楼上各位大神的代码怎么感觉好多?偶省事了,输入的东东直接在主函数里初始化了,自己可以改改就行了
[/Quote]

你没有建立这个SqStack.h
真你假我 2012-05-15
  • 打赏
  • 举报
回复
4楼的程序运行了怎么会出现r.cpp
C:\Windows\System32\r.cpp(4) : fatal error C1083: Cannot open include file: 'SqStack.h': No such file or directory是哪里错了啊?怎么改啊?
牙牙学语 2012-05-13
  • 打赏
  • 举报
回复
楼上各位大神的代码怎么感觉好多?偶省事了,输入的东东直接在主函数里初始化了,自己可以改改就行了
牙牙学语 2012-05-13
  • 打赏
  • 举报
回复
#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);

}
牙牙学语 2012-05-13
  • 打赏
  • 举报
回复
#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);

}
ktaurus 2012-05-13
  • 打赏
  • 举报
回复
#include <iostream>
using namespace std;

#include "SqStack.h"

#define MAXSIZE 30 //设迷宫的最大行列为30

struct 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;
}//NextPos

int 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;
}

#ifndef _SQSTACK_H_
#define _SQSTACK_H_

//定义顺序栈类
template <class ElemType>//声明一个类模板
class SqStack
{
public: //顺序栈类的各成员函数
SqStack(int m = 100);
~SqStack();
void Clear();
bool Empty() const;
int Length() const;
ElemType & Top() const;
void Push(const ElemType &e);
void Pop();
private: //顺序栈类的数据成员
ElemType *m_base; //基地址指针
int m_top; //栈顶指针
int m_size; //向量空间大小
};

//构造函数,分配m个结点的顺序空间,构造一个空的顺序栈。
template <class ElemType>
SqStack <ElemType>::SqStack(int m)
{
m_top = 0;
m_base = new ElemType[m];
m_size = m;
}//SqStack

//析构函数,将栈结构销毁。
template <class ElemType>
SqStack <ElemType>::~SqStack()
{
if (m_base != NULL) delete[] m_base;
}//~SqStack

//清空栈。
template <class ElemType>
void SqStack <ElemType>::Clear()
{
m_top = 0;
}//Clear

//判栈是否为空,若为空,则返回true,否则返回false。
template <class ElemType>
bool SqStack <ElemType>::Empty() const
{
return m_top == 0;
}//Empty

//求栈的长度。
template <class ElemType>
int SqStack <ElemType>::Length() const
{
return m_top;
}//Length

//取栈顶元素的值。先决条件是栈不空。
template <class ElemType>
ElemType & SqStack <ElemType>::Top() const
{
return m_base[m_top - 1];
}//Top

//入栈,若栈满,则先扩展空间。插入e到栈顶。
template <class ElemType>
void SqStack <ElemType>::Push(const ElemType &e)
{
if(m_top >= m_size){ //若栈满,则扩展空间。
ElemType *newbase;
newbase = new ElemType[m_size + 10];
for(int j = 0; j < m_top; j++)
newbase[j] = m_base[j];
delete[] m_base;
m_base = newbase;
m_size += 10;
}
m_base[m_top++] = e;
}//Push

//出栈,弹出栈顶元素。先决条件是栈非空。
template <class ElemType>
void SqStack <ElemType>::Pop()
{
m_top--;
}//Pop

#endif
love_kobe 2012-05-13
  • 打赏
  • 举报
回复
#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);
}
}

真你假我 2012-05-13
  • 打赏
  • 举报
回复
能不能多给点注释啊谢谢了
W170532934 2012-05-12
  • 打赏
  • 举报
回复

#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;
}


如果有不符合的就自己修改下吧。

64,282

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧