C语言链栈以及回溯算法解决迷宫问题

来源:互联网 发布:中文学位论文数据库 编辑:程序博客网 时间:2024/05/20 13:05

回溯算法介绍:回溯算法

问题描述:

已知迷宫的入口和出口,找出从入口到出口的一条路径

代码如下

maze.h

/***利用回溯算法以及栈结构来寻找路径*基本需求:已知迷宫的入口和出口,找到一条路径*迷宫墙壁用‘笑脸’表示,路用‘空格’表示*找到的路径使用方向键表示^ v < >*//***算法描述:*0、指定入口和出口,新结点为入口,新节点成为激活结点*1、在激活结点处判断4个方向,如果某一方向未遍历,则将激活节点设置为老节点用“1”表示,将该方向的节点激活*2、在新激活的活结点处,进行步骤 1 ,如果四个方向没有一个未遍历的节点,则将该节点视为死结点,设置为“d”,且回退一步,将回退后的节点激活,重复步骤1*3、重复上面的步骤1,直至找到出口***//**思路:迷宫使用2维数组来保存使用栈来保存路径,栈结点使用结构体描述,保存坐标以及方向如何进行上下左右判断:通过对活结点进行运算,向上运动(x-1,y不变),向下运动(x+1,y不变),向左运动(x不变,y-1),向右运动(x不变,y+1)如何设置为 老节点'1', 死结点(d) 未遍历节点(' '),墙壁 :)**/#include<stdio.h>#include<stdlib.h>//使用栈来保存路typedef struct node{int x;int y;char direction;struct node* next;}Road;Road* top;//op stackvoid initStack(Road **top,int x,int y);void Push(Road **top,Road e);Road Pop(Road **top);void Destory(Road **top);//寻找路径void findRoad(char a[][10],int line,int row,int x,int y,int ex,int ey);//如果某一方向上的road为未遍历的,则节点压入栈,并将该方向上的节点激活Road findPush(char a[][10],int line, int x,int y);//打印Maze mapvoid printMaze(char a[][10],int line,int row);
maze.c

#include "maze.h"void initStack(Road **top,int x,int y){(*top)=(Road*)malloc(sizeof(Road));(*top)->x=x;(*top)->y=y;(*top)->next=NULL;}void Push(Road **top,Road e){Road *temp;temp=(Road*)malloc(sizeof(Road));temp->x=e.x;temp->y=e.y;temp->direction=e.direction;temp->next=(*top);(*top)=temp;}Road Pop(Road **top){Road *temp,e;temp=(*top);(*top)=(*top)->next;e.x=temp->x;e.y=temp->y;e.direction=temp->direction;free(temp);return e;}void Destory(Road **top){Road *temp;temp=*top;while((*top)->next!=NULL){(*top)=(*top)->next;free(temp);temp=*top;}free(*top);*top=NULL;}void printMaze(char a[][10],int line,int row){int i,j;for(i=0;i<line;i++){for(j=0;j<row;j++){printf("%c",a[i][j]);}printf("\n");}}Road findPush(char a[][10],int line, int x,int y){Road e;e.x=x;e.y=y;e.next=NULL;//如果向上为路,if(a[x-1][y]==' '){a[x][y]='1';//将原来的节点设置为老节点 1;e.direction=24;//设置方向Push(&top,e);//将老节点压栈e.x=x-1;//将该方向上的节点激活return e;}//如果向下为路if(a[x+1][y]==' '){a[x][y]='1';e.direction=25;Push(&top,e);e.x=x+1;return e;}//如果向左为路if(a[x][y-1]==' '){a[x][y]='1';e.direction=27;Push(&top,e);e.y=y-1;return e;}//如果向右为路if(a[x][y+1]==' '){a[x][y]='1';e.direction=26;Push(&top,e);e.y=y+1;return e;}//如果没有路else{a[x][y]='d';//将当前节点设置为死结点e=Pop(&top);//弹出老节点return e;}}void findRoad(char a[][10],int line,int row,int x,int y,int ex,int ey){Road e,*temp;//initialize e;e.x=x,e.y=y,e.direction=0,e.next=NULL;while((e.x!=ex)||(e.y!=ey)){//在这一步,注意终止条件。e=findPush(a,10,x,y);x=e.x;y=e.y;}Push(&top,e);//对迷宫图标出路径temp=top;while(temp->next!=NULL){a[temp->x][temp->y]=temp->direction;temp=temp->next;}Destory(&top);}

#include "maze.h"void main(){char a[10][10]={1 ,1 ,32,1, 1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,32,32,32,1 ,1 ,1 ,1 ,1 ,1 ,32,32,32,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,32,1 ,1 ,1 ,32,1 ,1 ,1 ,1 ,1 ,32,1 ,1 ,1 ,32,1 ,1 ,1 ,1 ,1 ,32,32,1 ,1 ,32,1 ,1 ,1 ,1 ,1 ,32,1 ,1 ,1 ,32,1 ,1 ,1 ,1 ,1 ,32,32,32,32,32,1 ,1 ,1 ,1 ,1 ,1 ,1 ,32 ,1 ,32,1 ,1 ,1 ,1 ,1 ,1 ,1 ,32 ,1 ,32,1 ,1 };initStack(&top,0,2);findRoad(a,10,10,0,2,9,7);//多维数组做参的情况printMaze(a,10,10);}


经验:

1、多维数组做参,

2、在函数中传入的形参名为a,形参a其实就是一个指针变量,并没有真正的开辟一个数组空间

3、找到出口的条件

4、关于变量的申明:比如Road e,以及Road *temp的问题