主题:迷宫问题
以一个m*n的 长方阵表示迷宫,0和1分别表示迷宫中的通路和障碍。设计一个程序,对任意设定的迷宫,求出一条从入口到出口的通路,或得出没有通路的结论。
首先实现一个以链表做存储结构的栈类型,然后编写一个求解迷宫的非递归程序。求得的通路以3元组(i,j,d)的形式输出,其中:(i,j)指示迷宫中的一个坐标,d表示走道下一坐标的方向。如:对于下列数据的迷宫,输出的一条通路为:(1,1,1), (1,2,2),(2,2,2),(3,2,3),(3,1,2),...
想请问一下用栈,其中包括2维数组
typedef struct {
int r,c;
}PosType;
typedef struct {
int m,n;
char arr[m+2][n+2];
}MazeType;
void InitMaze(MazeType &maze,int a[1][1],int row,int col)
bool MazePath (MazeType&maze,PosType start,PosType end)
void PrintMaze (MazeType maze)
typedef struct{
int step;
PosType seat;
directiveType di;
}ElemType;
typedef struct NodeType{
ElemType data;
Nodetype *next;
}NodeType, *LinkType;
typedef struct{
LinkType top;
int size;
}Stack;
Status push(Stack&S,ElemType e){
if(MakeNode(p,e)){
p^.next=S.top;S.top=p;
S.size++;return TRUE;
}
else return FALSE;
}
Status Pop(Stack&S,ElemType&e)
{
if(StackEmpty(S)) return FALSE;
else{
p=S.top;s.top=S.top->next;
e=p->data;S.size--;return TRUE;
}
}
Status MazePath(MazeType maze,PosType start,PosType end)
{
InitStack(S);curpos=start;
curstep=1;found=FALSE;
do{
if(Pass(maze,curpos)){
FootPrint(maze,curpos);
e=(curstep,curpos,1);
Push(S,e);
if(Same(curpos,end)) found=TRUE;
else{
curpos=NextPos(curpos,1);
curstep++;
}//else
}//if
else
if(!StackEmpty(S)){
Pop(S,e);
while(e.di==4&&!StackEmpty(S)){
MarkPrint(maze,e.seat);Pop(S,e);
curstep--;
}//while
if(e.di<4){
e.di++; Push(S,e);
curpos=NextPos(e.seat,e.di);
}//if
}//if
}while(!StackEmpty(S)&&!found);
return found;
}//Mazepath
void main()
{ initialization();
do{
ReadCommand(cmd);
Interpret(cmd);
}while(cmd !='q'&&cmd !='Q');
}//main
void Initialization()
{
clrscr();
CreatMaze--c MazePath--m PrintMaze--p Quit--q;
}//Initialization
void ReadCommand(char &cmd)
{
do{
cmd=getche()
}while (cmd(['c','C','p','P','q','Q','m','M']);
}// ReadCommand
void Interpret(char cmd)
{
switch(cmd){
case 'c','C': InitMaze(ma,a2,rnum,cnum);
break;
case 'm','M':
if(MazePath( ma,from,term))
else;
break;
case 'p','P':PrintMaze(ma);
}//switch
}//Interpret
还需怎么修改啊? 求迷宫的伪码算法我已清楚 请大家帮一下,有急用 谢谢^-^......
首先实现一个以链表做存储结构的栈类型,然后编写一个求解迷宫的非递归程序。求得的通路以3元组(i,j,d)的形式输出,其中:(i,j)指示迷宫中的一个坐标,d表示走道下一坐标的方向。如:对于下列数据的迷宫,输出的一条通路为:(1,1,1), (1,2,2),(2,2,2),(3,2,3),(3,1,2),...
想请问一下用栈,其中包括2维数组
typedef struct {
int r,c;
}PosType;
typedef struct {
int m,n;
char arr[m+2][n+2];
}MazeType;
void InitMaze(MazeType &maze,int a[1][1],int row,int col)
bool MazePath (MazeType&maze,PosType start,PosType end)
void PrintMaze (MazeType maze)
typedef struct{
int step;
PosType seat;
directiveType di;
}ElemType;
typedef struct NodeType{
ElemType data;
Nodetype *next;
}NodeType, *LinkType;
typedef struct{
LinkType top;
int size;
}Stack;
Status push(Stack&S,ElemType e){
if(MakeNode(p,e)){
p^.next=S.top;S.top=p;
S.size++;return TRUE;
}
else return FALSE;
}
Status Pop(Stack&S,ElemType&e)
{
if(StackEmpty(S)) return FALSE;
else{
p=S.top;s.top=S.top->next;
e=p->data;S.size--;return TRUE;
}
}
Status MazePath(MazeType maze,PosType start,PosType end)
{
InitStack(S);curpos=start;
curstep=1;found=FALSE;
do{
if(Pass(maze,curpos)){
FootPrint(maze,curpos);
e=(curstep,curpos,1);
Push(S,e);
if(Same(curpos,end)) found=TRUE;
else{
curpos=NextPos(curpos,1);
curstep++;
}//else
}//if
else
if(!StackEmpty(S)){
Pop(S,e);
while(e.di==4&&!StackEmpty(S)){
MarkPrint(maze,e.seat);Pop(S,e);
curstep--;
}//while
if(e.di<4){
e.di++; Push(S,e);
curpos=NextPos(e.seat,e.di);
}//if
}//if
}while(!StackEmpty(S)&&!found);
return found;
}//Mazepath
void main()
{ initialization();
do{
ReadCommand(cmd);
Interpret(cmd);
}while(cmd !='q'&&cmd !='Q');
}//main
void Initialization()
{
clrscr();
CreatMaze--c MazePath--m PrintMaze--p Quit--q;
}//Initialization
void ReadCommand(char &cmd)
{
do{
cmd=getche()
}while (cmd(['c','C','p','P','q','Q','m','M']);
}// ReadCommand
void Interpret(char cmd)
{
switch(cmd){
case 'c','C': InitMaze(ma,a2,rnum,cnum);
break;
case 'm','M':
if(MazePath( ma,from,term))
else;
break;
case 'p','P':PrintMaze(ma);
}//switch
}//Interpret
还需怎么修改啊? 求迷宫的伪码算法我已清楚 请大家帮一下,有急用 谢谢^-^......