The premise of the subject: The starting point (8,8), the end point (
#include <stdio.h>#include<stdlib.h>#defineOK 1#defineERROR 0#defineTRUE 1#defineFALSE 0#defineOVERFLOW-1#defineStack_init_size 100#defineStackincrement 20#defineN 10typedefintStatus;typedefCharMazetype[n][n +1];intDx[] = {0,1,0, -1};intDy[] = {1,0, -1,0};typedefstruct { intX//rows in the Maze intY//columns in the Maze} Postype;//coordinatestypedefstruct{Postype seat; //the current coordinate position intDi//the direction of the downward coordinate position} Selemtype;//the element type of the stacktypedefstruct{Selemtype*Base; Selemtype*top; intstacksize;} Sqstack; Status Initstack (Sqstack&s)//initialization of the stack{s.Base= (Selemtype *)malloc(Stack_init_size *sizeof(Selemtype)); if(!s.Base) exit (OVERFLOW); S.top= S.Base; S.stacksize=stack_init_size; returnOK;} Status Push (Sqstack&s, Selemtype e) { if(S.top-s.Base>= s.stacksize) {//full stack, additional storage spaceS.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; returnOK;} Status Pop (Sqstack&s, Selemtype &e) { if(S.top = = S.)Base)returnERROR; E= * --S.top; returnOK;}intStackempty (Sqstack s) {returnS.Base==S.top;} Status Pass (Mazetype Maze, Postype CurPos)//determines whether the current node is passed{ returnMAZE[CURPOS.X][CURPOS.Y] = =' '|| MAZE[CURPOS.X][CURPOS.Y] = ='S'|| MAZE[CURPOS.X][CURPOS.Y] = ='E';}voidFootprint (Mazetype &maze, Postype CurPos)//Leave footprints{Maze[curpos.x][curpos.y]='*';//Walk through and get through}postype Nextpos (postype pos,intd) {pos.x+=Dx[d]; Pos.y+=Dy[d]; returnPos;}voidMarkprint (Mazetype &maze, Postype CurPos)//leave a mark that cannot be passed{Maze[curpos.x][curpos.y]='!';//Walk through but can't get through}//a path for solving Maze maze, from entry start to exit endStatus Mazepath (Mazetype &Maze, Postype start, Postype end) {Sqstack s; Selemtype e; Initstack (s); Postype CurPos=start; Do { if(Pass (Maze, CurPos)) {//if the current position can be passed, that is, a channel block that has never been reachedFootprint (Maze, curpos);//Leave footprints if(Curpos.x==end.x && curpos.y==end.y)returnTRUE; E.seat=CurPos; E.di=0; Push (S, e); CurPos= Nextpos (CurPos,0);//Get Next node: The east neighbor of the current position } Else{//The current position cannot be passed if(!Stackempty (s)) {Pop (S, e); while(E.di = =3&&!Stackempty (s)) {Markprint (maze, e.seat); Pop (S, e); //leave the mark that cannot pass, and return one step } if(E.di <3) {E.di++; Push (S, e); //explore in a different directionCurPos = Nextpos (E.seat, E.di);//sets the current position to be an adjacent block in that direction}//if}//if}//Else} while(!Stackempty (s)); returnFALSE;}intMain () {inti; Postype Start={1,1}, end={8,8}; Mazetype Maze; for(i =0; I <Ten; i++) gets (Maze[i]); Mazepath (Maze, start, end); for(i =0; I <Ten; i++) puts (maze[i]); return 0;}
Maze problem (i)-Using stack backtracking