#include<iostream>
#include<cstdlib>
#include<cstring>
#include<string>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int Status;
typedef int DirectiveType;
#define RANGE 100 //迷宫大小
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
typedef struct {
int row;
int col;
}PosType;
typedef struct {
int step;
PosType seat;
DirectiveType di;
}SElemType;
typedef struct {
SElemType *base ;
SElemType *top;
int stacksize;
}SqStack;
Status InitStack(SqStack &s){
s.base = (SElemType * ) malloc(STACK_INIT_SIZE * sizeof (SElemType));
if (!s.base ) exit(OVERFLOW);
s.top=s.base ;
s.stacksize=STACK_INIT_SIZE;
return OK;
}
Status GetTop(SqStack s, SElemType &e ){
if ( s.top == s.base ) return ERROR;
e = *(s.top-1 );
return OK;
}
Status Push(SqStack &s, SElemType e){
if (s.top-s.base >= s.stacksize){
s.base = (SElemType *)realloc(s.base ,(s.stacksize+STACKINCREMENT)*sizeof (SElemType));
if (!s.base ) exit(OVERFLOW);
s.top = s.base + s.stacksize;
s.stacksize += STACKINCREMENT;
}
*s.top++ = e;
return OK;
}
Status Pop(SqStack &s, SElemType &e){
if (s.top==s.base )return ERROR;
e = * --s.top;
return OK;
}
int StackEmpty(SqStack s){
return s.base == s.top;
}
#define ROW 10
#define COL 10
typedef struct {
int m,n;
int arr[RANGE][RANGE];
}MazeType;
Status InitMaze(MazeType &maze, int a[][COL], int row, int col){
for (int i=1 ;i<=row;i++){
for (int j=1 ;j<=col;j++){
maze.arr[i][j] = a[i-1 ][j-1 ];
}
}
for (int j=0 ;j<=col+1 ;j++){
maze.arr[0 ][j] = maze.arr[row+1 ][j]=1 ;
}
for (int i=0 ;i<=row+1 ;i++){
maze.arr[i][0 ] = maze.arr[i][col+1 ]=1 ;
}
maze.m = row, maze.n = col;
return OK;
}
Status Pass(MazeType maze,PosType curpos){
if (maze.arr[curpos.row][curpos.col] == 0 )
return 1 ;
else return 0 ;
}
Status FootPrint(MazeType &maze,PosType curpos){
maze.arr[curpos.row][curpos.col]=2 ;
return OK;
}
Status MarkPrint(MazeType &maze,PosType curpos){
maze.arr[curpos.row][curpos.col]=3 ;
return OK;
}
SElemType CreateSElem(int step, PosType pos, int di){
SElemType e;
e.step = step;
e.seat = pos;
e.di = di;
return e;
}
PosType NextPos(PosType curpos, DirectiveType di){
PosType pos = curpos;
switch (di)
{
case 1 :
pos.col++;
break ;
case 2 :
pos.row++;
break ;
case 3 :
pos.col--;
break ;
case 4 :
pos.row--;
break ;
}
return pos;
}
Status PosEquare(PosType pos1, PosType pos2){
if (pos1.row==pos2.row && pos1.col==pos2.col)
return 1 ;
else return 0 ;
}
void PrintMaze(MazeType maze,int row,int col){
int i,j;
printf (" " );
for (i=0 ;i<=COL+1 ;i++)
printf ("-" ,i);
printf ("\n" );
for (i=0 ;i<=row+1 ;i++){
printf ("=" ,i);
for (j=0 ;j<=col+1 ;j++){
switch (maze.arr[i][j]){
case 0 :
printf (" " );
break ;
case 2 :
printf ("* " );
break ;
case 3 :
printf ("@ " );
break ;
case 1 :
printf ("# " );
break ;
}
}
printf ("\n" );
}
}
Status MazePath(MazeType &maze,PosType start, PosType end){
SqStack s; SElemType e;
InitStack(s);
PosType curpos = start;
int curstep = 1 ;
do {
if ( Pass(maze,curpos) ){
FootPrint(maze,curpos);
e = CreateSElem(curstep,curpos,1 );
Push(s,e);
if ( PosEquare(curpos,end) ) return TRUE;
curpos =NextPos(curpos,1 );
curstep++;
}else {
if (!StackEmpty(s)){
Pop(s,e);
while (e.di==4 && !StackEmpty(s) ){
MarkPrint(maze,e.seat); Pop(s,e);
}
if (e.di<4 ){
e.di++; Push(s,e);
curpos = NextPos(e.seat,e.di);
}
}
}
}while (!StackEmpty(s));
return FALSE;
}
#include "base.h"
#include "stack.h"
#include "maze.h"
int main(){
char cmd;
int i;
PosType start,end;
MazeType maze;
int a[ROW][COL]={
{0 ,0 ,1 ,0 ,0 ,0 ,0 ,1 ,0 ,1 },
{1 ,0 ,1 ,0 ,1 ,1 ,0 ,1 ,1 ,0 },
{1 ,0 ,0 ,0 ,1 ,1 ,0 ,0 ,0 ,1 },
{0 ,1 ,0 ,1 ,1 ,0 ,0 ,1 ,1 ,1 },
{0 ,1 ,0 ,0 ,1 ,1 ,0 ,1 ,0 ,0 },
{1 ,0 ,1 ,0 ,1 ,1 ,0 ,0 ,1 ,0 },
{0 ,1 ,1 ,0 ,0 ,0 ,1 ,0 ,1 ,1 },
{0 ,1 ,0 ,1 ,1 ,0 ,0 ,0 ,0 ,0 },
{0 ,1 ,1 ,0 ,0 ,1 ,1 ,1 ,1 ,0 },
{1 ,0 ,1 ,0 ,1 ,1 ,0 ,0 ,1 ,0 }
};
printf ("\n----------原始迷宫(不加外围围墙)(0 表示通路,1 表示障碍)---------\n" );
printf (" " );
for (i=1 ;i<=COL;i++)
printf ("-" ,i);
printf ("\n" );
for (i=0 ;i<ROW;i++){
printf ("=" ,i+1 );
for (int j=0 ;j<COL;j++)
printf ("-" ,a[i][j]);
printf ("\n" );
}
InitMaze(maze,a,ROW,COL);
do {
int flag=0 ;
do {
printf ("\n输入迷宫入口坐标( <1,1>到<10,10>之间 ): " );
scanf_s("%d%d" ,&start.row,&start.col);
if (start.col>maze.n || start.row>maze.m ||start.col<=0 ||start.row<=0 ){
printf ("越界!" );
flag=1 ;
}
else flag=0 ;
}while (flag);
do {
printf ("输入迷宫出口坐标( <1,1>到<10,10>之间 ): " );
scanf_s("%d%d" ,&end.row , &end.col);
if ( end.col>maze.n || end.row>maze.m ||end.col<=0 ||end.row<=0 ){
printf ("越界!\n" );
flag=1 ;
}
else flag=0 ;
}while (flag);
if (MazePath(maze,start,end)){
printf ("\n-----上帝与你同在,从当前入口到出口有通路(加上了外围围墙)( * 组成的路径)----------\n" );
PrintMaze(maze,ROW,COL);
}
else printf ("\n---------你死定了!从当前入口到出口没有通路!-----\n" );
printf ("\n To be Continue ?y(Y)/n: " );
scanf_s(" %c" ,&cmd);
}while (cmd=='Y' ||cmd=='y' );
return 0 ;
}