/*
*Copyright (c)2017, School of computer and control engineering, Yantai University
*All rights reservrd.
*Author: Li Xinhao
*Completion time: November 22, 2017
*Version number: v1.0
*Problem Description: a program is designed to solve the maze problem with the idea of depth first traversal algorithm.
(1) establish the graph data structure corresponding to the maze and its adjacency table representation.
(2) using the idea of depth first traversal, the algorithm is designed to output all maze paths from the entry (1,1) point to the exit (M,N).
[model building]
Each lattice in the maze is regarded as a vertex. If the adjacent lattice can reach it, there is edge connection between the corresponding vertices.
For example, the following maze
The code is as follows:
#include <stdio.h> #include <malloc.h> #define MaxSize 100 #define M 4 #define N 4 //The following defines the adjacency table type typedef struct ANode //Node structure type of edge { int i,j; //End position of the edge (i,j) struct ANode *nextarc; //Pointer to next edge } ArcNode; typedef struct Vnode //Types of adjacent header nodes { ArcNode *firstarc; //Point to first edge } VNode; typedef struct { VNode adjlist[M+2][N+2]; //Adjacency header node array } ALGraph; //Adjacency table types of Graphs typedef struct { int i; //Line number of the current block int j; //Column number of the current block } Box; typedef struct { Box data[MaxSize]; int length; //path length } PathType; //Define path type int visited[M+2][N+2]= {0}; int count=0; void CreateList(ALGraph *&G,int mg[][N+2]) //Establish adjacency table G corresponding to maze array { int i,j,i1,j1,di; ArcNode *p; G=(ALGraph *)malloc(sizeof(ALGraph)); for (i=0; i<M+2; i++) //Set the initial value of the pointer field of all header nodes in the adjacency table for (j=0; j<N+2; j++) G->adjlist[i][j].firstarc=NULL; for (i=1; i<=M; i++) //Check each element in mg for (j=1; j<=N; j++) if (mg[i][j]==0) { di=0; while (di<4) { switch(di) { case 0: i1=i-1; j1=j; break; case 1: i1=i; j1=j+1; break; case 2: i1=i+1; j1=j; break; case 3: i1=i, j1=j-1; break; } if (mg[i1][j1]==0) //(I 1, j 1) is a walking block { p=(ArcNode *)malloc(sizeof(ArcNode)); //Create a node * p p->i=i1; p->j=j1; p->nextarc=G->adjlist[i][j].firstarc; //After chaining * p node to linked list G->adjlist[i][j].firstarc=p; } di++; } } } //Output adjacency table G void DispAdj(ALGraph *G) { int i,j; ArcNode *p; for (i=0; i<M+2; i++) for (j=0; j<N+2; j++) { printf(" [%d,%d]: ",i,j); p=G->adjlist[i][j].firstarc; while (p!=NULL) { printf("(%d,%d) ",p->i,p->j); p=p->nextarc; } printf("\n"); } } void FindPath(ALGraph *G,int xi,int yi,int xe,int ye,PathType path) { ArcNode *p; visited[xi][yi]=1; //Set visited flag path.data[path.length].i=xi; path.data[path.length].j=yi; path.length++; if (xi==xe && yi==ye) { printf(" Maze path%d: ",++count); for (int k=0; k<path.length; k++) printf("(%d,%d) ",path.data[k].i,path.data[k].j); printf("\n"); } p=G->adjlist[xi][yi].firstarc; //p points to the first edge vertex of vertex v while (p!=NULL) { if (visited[p->i][p->j]==0) //If the (P - > I, P - > J) block is not accessed, access it recursively FindPath(G,p->i,p->j,xe,ye,path); p=p->nextarc; //p points to the next edge vertex of vertex v } visited[xi][yi]=0; } int main() { ALGraph *G; int mg[M+2][N+2]= //Labyrinth array { {1,1,1,1,1,1}, {1,0,0,0,1,1}, {1,0,1,0,0,1}, {1,0,0,0,1,1}, {1,1,0,0,0,1}, {1,1,1,1,1,1} }; CreateList(G,mg); printf("Adjacency table corresponding to maze:\n"); DispAdj(G); //Output adjacency table PathType path; path.length=0; printf("All maze paths:\n"); FindPath(G,1,1,M,N,path); return 0; }
The test results are as follows: