# 迷宫的四重求解

## 程序代码：

#include<stdio.h>
#include<stdlib.h>
#define ROW 6
#define COL 6
//Round1《使用递归的方法》
typedef struct maze1{
int maze[ROW][COL];
}Maze;

//结点的建立
typedef struct node{
int row;
int col;
}Node;
//顺序栈的建立，栈中的元数类型为Node
typedef struct{
Node arr1[COL*ROW];
int size;
}seqstack;

int arr[ROW][COL]={
{0,1,0,0,0,0},
{0,1,1,1,0,0},
{0,1,0,1,0,0},
{0,1,0,1,1,1},
{0,1,0,0,0,0},
{0,1,0,0,0,0}
// {0,1,0,0},
// {0,1,1,1},
// {0,1,0,0},
// {0,1,0,0},
//{0,1},
//{0,1}
};
//迷宫地图的初始化
void MazeInit(Maze* maz){
if(maz == NULL){
//非法输入
return;
}
int i=0;
for(;i<ROW;++i){
int j=0;
for(;j<COL;++j){
maz->maze[i][j] = arr[i][j];
}
}
}

//输出迷宫地图
void printMaze(Maze* maz,char*msg){
printf("\n************%s************\n",msg);
int i=0;
for(;i<ROW;++i){
int j=0;
for(;j<COL;++j){
printf("%d ",maz->maze[i][j]);
}
printf("\n");
}
}
//初始化结点
NodeInit(Node * enter,int r,int c){
enter->row = r;
enter->col = c;
}
//栈的初始化
return;
}
//创建新结点
Node* CreateNewNode(int x,int y){
Node* new_node;
new_node->row = x;
new_node->col = y;
return new_node;
}
//入栈
//非法输入
return;
}
return;
}
//出栈
//非法输入
return;
}
//空栈
return;
}
return;
}
//得到栈顶元素
return 0;
}
return 1;
}
//判断俩个结点是否相等
int is_mull(Node* cur,Node* enter){
if(cur->row==enter->row&&cur->col==enter->col){
return 0;
}else{
return 1;
}
}
//查找迷宫接口出口函数
void GetPath(Maze* maz,Node* cur,Node*enter){
//判断当前点是否能落脚
while(cur->col>=0&&cur->col<COL&&cur->row>=0&&cur->row<ROW){
if(maz->maze[cur->row][cur->col]==1){
//判断是否为出口
if(is_mull(cur,enter)){
if((cur->col)==3){
//if(cur->row==0||cur->col==COL-1||cur->row==ROW-1||cur->col==0){
printf("find path\n");
return;
}
}
maz->maze[cur->row][cur->col]=2;
Node cur1 ;              //上结点
NodeInit(&cur1,cur->row-1,cur->col);
GetPath(maz,&cur1,enter);
Node cur2;              //右结点
NodeInit(&cur2,cur->row,cur->col+1);
GetPath(maz,&cur2,enter);
Node cur3;               //下结点
NodeInit(&cur3,cur->row+1,cur->col);
GetPath(maz,&cur3,enter);
Node cur4;               //左结点
NodeInit(&cur4,cur->row,cur->col-1);
GetPath(maz,&cur4,enter);
return;
}
break;
}
}
void TextMaze(){
Maze maz;
MazeInit(&maz);
Node enter;
NodeInit(&enter,0,1);
GetPath(&maz,&enter,&enter);//第一个参数是地图，第二个参数是下一个要走的点，第三个参数是入口点
printMaze(&maz,"Rount 1");
}
//不使用递归求解迷宫问题
//Round2:查找迷宫接口出口函数
Node cur;
NodeInit(&cur,enter->row,enter->col);
//判断当前点是否能落脚
if(cur.col>=0&&cur.col<COL&&cur.row>=0&&cur.row<ROW){
if(maz->maze[cur.row][cur.col]==1){
while(1){
if(maz->maze[cur.row][cur.col]==1){
maz->maze[cur.row][cur.col]=2;
if((cur.row==0||(cur.col==COL-1)||(cur.row==ROW-1)||cur.col==0)&&is_mull(&cur,enter)){
printf("find path\n");
break;
}
}
Node cur1;              //上结点
NodeInit(&cur1,cur.row-1,cur.col);
if(cur1.col>=0&&cur1.col<COL&&cur1.row>=0&&cur1.row<ROW){
if(maz->maze[cur1.row][cur1.col]==1){
cur = cur1;
continue;
}
}

Node cur2;              //右结点
NodeInit(&cur2,cur.row,cur.col+1);
if(cur2.col>=0&&cur2.col<COL&&cur2.row>=0&&cur2.row<ROW){
if(maz->maze[cur2.row][cur2.col]==1){
cur = cur2;
continue;
}
}
Node cur3;               //下结点
NodeInit(&cur3,cur.row+1,cur.col);
if(cur3.col>=0&&cur3.col<COL&&cur3.row>=0&&cur3.row<ROW){
if(maz->maze[cur3.row][cur3.col]==1){
cur = cur3;
continue;
}
}
Node cur4;               //左结点
NodeInit(&cur4,cur.row,cur.col-1);
if(cur4.col>=0&&cur4.col<COL&&cur4.row>=0&&cur4.row<ROW){
if(maz->maze[cur4.row][cur4.col]==1){
cur = cur4;
continue;
}
}
if(ret == 0){
return;
}
}
}
}
}

void TextMaze1(){
Maze maz;
MazeInit(&maz);
Node enter;
NodeInit(&enter,0,1);
printMaze(&maz,"Rount 2");
}
//Round3:查找迷宫接口出口函数
Node cur;
NodeInit(&cur,enter->row,enter->col);
//判断当前点是否能落脚
if(cur.col>=0&&cur.col<COL&&cur.row>=0&&cur.row<ROW){
if(maz->maze[cur.row][cur.col]==1){
while(1){
if(maz->maze[cur.row][cur.col]==1){
maz->maze[cur.row][cur.col]=2;
if((cur.row==0||(cur.col==COL-1)||(cur.row==ROW-1)||cur.col==0)&&is_mull(&cur,enter)){
printf("find path\n");
if(ret == 0){
return;
}
}
}
Node cur1;              //上结点
NodeInit(&cur1,cur.row-1,cur.col);
if(cur1.col>=0&&cur1.col<COL&&cur1.row>=0&&cur1.row<ROW){
if(maz->maze[cur1.row][cur1.col]==1){
cur = cur1;
continue;
}
}

Node cur2;              //右结点
NodeInit(&cur2,cur.row,cur.col+1);
if(cur2.col>=0&&cur2.col<COL&&cur2.row>=0&&cur2.row<ROW){
if(maz->maze[cur2.row][cur2.col]==1){
cur = cur2;
continue;
}
}
Node cur3;               //下结点
NodeInit(&cur3,cur.row+1,cur.col);
if(cur3.col>=0&&cur3.col<COL&&cur3.row>=0&&cur3.row<ROW){
if(maz->maze[cur3.row][cur3.col]==1){
cur = cur3;
continue;
}
}
Node cur4;               //左结点
NodeInit(&cur4,cur.row,cur.col-1);
if(cur4.col>=0&&cur4.col<COL&&cur4.row>=0&&cur4.row<ROW){
if(maz->maze[cur4.row][cur4.col]==1){
cur = cur4;
continue;
}
}
if(ret == 0){
return;
}
}
}
}
}

void TextMaze2(){
Maze maz;
MazeInit(&maz);
Node enter;
NodeInit(&enter,0,1);
printMaze(&maz,"Rount 3");
}
//栈的拷贝
int i=0;
}
return;
}
int i=0;
}
}
//Round3:查找迷宫接口出口函数
int count = 0;//计算找到的路径条数
Node cur;
NodeInit(&cur,enter->row,enter->col);
//判断当前点是否能落脚
if(cur.col>=0&&cur.col<COL&&cur.row>=0&&cur.row<ROW){
if(maz->maze[cur.row][cur.col]==1){
while(1){
if(maz->maze[cur.row][cur.col]==1){
maz->maze[cur.row][cur.col]=2;
if((cur.row==0||(cur.col==COL-1)||(cur.row==ROW-1)||cur.col==0)&&is_mull(&cur,enter)){
printf("find path\n");
++count;
if(count==1){
}
}
if(ret == 0){
return;
}
}
}
Node cur1;              //上结点
NodeInit(&cur1,cur.row-1,cur.col);
if(cur1.col>=0&&cur1.col<COL&&cur1.row>=0&&cur1.row<ROW){
if(maz->maze[cur1.row][cur1.col]==1){
cur = cur1;
continue;
}
}

Node cur2;              //右结点
NodeInit(&cur2,cur.row,cur.col+1);
if(cur2.col>=0&&cur2.col<COL&&cur2.row>=0&&cur2.row<ROW){
if(maz->maze[cur2.row][cur2.col]==1){
cur = cur2;
continue;
}
}
Node cur3;               //下结点
NodeInit(&cur3,cur.row+1,cur.col);
if(cur3.col>=0&&cur3.col<COL&&cur3.row>=0&&cur3.row<ROW){
if(maz->maze[cur3.row][cur3.col]==1){
cur = cur3;
continue;
}
}
Node cur4;               //左结点
NodeInit(&cur4,cur.row,cur.col-1);
if(cur4.col>=0&&cur4.col<COL&&cur4.row>=0&&cur4.row<ROW){
if(maz->maze[cur4.row][cur4.col]==1){
cur = cur4;
continue;
}
}
if(ret == 0){
return;
}
}
}
}
}

void TextMaze3(){
Maze maz;
MazeInit(&maz);
Node enter;
NodeInit(&enter,0,1);
printMaze(&maz,"Rount 4");
}
//使用二位数组表示地图、约定0表示墙，1表示路
int main(){
TextMaze();
TextMaze1();
TextMaze2();
TextMaze3();
return 0;
}

## 检测结果：

[chaiyandong@localhost shujujiegou]\$ ./mate
find path

************Rount 1************
0 2 0 0 0 0
0 2 2 1 0 0
0 2 0 1 0 0
0 2 0 1 1 1
0 2 0 0 0 0
0 2 0 0 0 0
find path

************Rount 2************
0 2 0 0 0 0
0 2 2 2 0 0
0 1 0 2 0 0
0 1 0 2 2 2
0 1 0 0 0 0
0 1 0 0 0 0
find path
find path

************Rount 3************
0 2 0 0 0 0
0 2 2 2 0 0
0 2 0 2 0 0
0 2 0 2 2 2
0 2 0 0 0 0
0 2 0 0 0 0
find path
find path

************Rount 4************
0 2 0 0 0 0
0 2 2 2 0 0
0 2 0 2 0 0
0 2 0 2 2 2
0 2 0 0 0 0
0 2 0 0 0 0
0,1
1,1
2,1
3,1
4,1
5,1

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120