迷宫的四重求解

程序代码:

#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;
}
//栈的初始化
void seqstackInit(seqstack*head){
head->size = 0;
return;
}
//创建新结点
Node* CreateNewNode(int x,int y){
Node* new_node;
new_node->row = x;
new_node->col = y;
return new_node;
}
//入栈
void stackPush(seqstack* head,Node* value){
if(head == NULL){
//非法输入
return;
}
head->arr1[head->size++]=*value;
return;
}
//出栈
void stackPop(seqstack* head){
if(head ==NULL){
//非法输入
return;
}
if(head->size==0){
//空栈
return;
}
--head->size;
return;
}
//得到栈顶元素
int stackTop(seqstack* head,Node* value){
if(head->size==0){
return 0;
}
int num = head->size-1;
*value=head->arr1[num];
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:查找迷宫接口出口函数
void GetPath1(Maze* maz,Node*enter,seqstack* head){
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;
stackPush(head,&cur);
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;
}
}
stackPop(head);
int ret = stackTop(head,&cur);
if(ret == 0){
return;
}
}
}
}
}


void TextMaze1(){
Maze maz;
MazeInit(&maz);
Node enter;
NodeInit(&enter,0,1);
seqstack head;
seqstackInit(&head);
GetPath1(&maz,&enter,&head);//第一个参数是地图,第二个参数是下一个要走的点,第三个参数是入口点
printMaze(&maz,"Rount 2");
}
//Round3:查找迷宫接口出口函数
void GetPath2(Maze* maz,Node*enter,seqstack* head){
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;
stackPush(head,&cur);
if((cur.row==0||(cur.col==COL-1)||(cur.row==ROW-1)||cur.col==0)&&is_mull(&cur,enter)){
printf("find path\n");
stackPop(head);
int ret = stackTop(head,&cur);
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;
}
}
stackPop(head);
int ret = stackTop(head,&cur);
if(ret == 0){
return;
}
}
}
}
}


void TextMaze2(){
Maze maz;
MazeInit(&maz);
Node enter;
NodeInit(&enter,0,1);
seqstack head;
seqstackInit(&head);
GetPath2(&maz,&enter,&head);//第一个参数是地图,第二个参数是下一个要走的点,第三个参数是入口点
printMaze(&maz,"Rount 3");
}
//栈的拷贝
void stackCopy(seqstack*head,seqstack*head1){
int i=0;
for(;i<head->size;++i){
head1->arr1[i] = head->arr1[i];
}
head1->size = head->size;
return;
}
void printstack(seqstack*head){
int i=0;
for(;i<head->size;++i){
printf("%d,%d\n",head->arr1[i].row,head->arr1[i].col);
}
}
//Round3:查找迷宫接口出口函数
void GetPath3(Maze* maz,Node*enter,seqstack* head,seqstack*head1){
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;
stackPush(head,&cur);
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){
stackCopy(head,head1);
}
if(head->size < head1->size){
stackCopy(head,head1);
}
stackPop(head);
int ret = stackTop(head,&cur);
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;
}
}
stackPop(head);
int ret = stackTop(head,&cur);
if(ret == 0){
return;
}
}
}
}
}


void TextMaze3(){
Maze maz;
MazeInit(&maz);
Node enter;
NodeInit(&enter,0,1);
seqstack head;
seqstackInit(&head);
seqstack head1;
seqstackInit(&head1);
GetPath3(&maz,&enter,&head,&head1);//第一个参数是地图,第二个参数是下一个要走的点,第三个参数是入口点
printMaze(&maz,"Rount 4");
printstack(&head1);
}
//使用二位数组表示地图、约定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

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页