See Data structure Write code (13) Application of Stack (IV.) Maze Solver

Source: Internet
Author: User

This is an example of solving a maze problem with a poor lifting method, but it is somewhat inadequate in the shortest path of efficiency and reconciliation .

These two questions remain in the next idle moment to answer.


Please note that the code is insufficient

The following code:


Maze.cpp: Defines the entry point of the console application. #include "stdafx.h" #include <stdlib.h>struct postion{int x;int y;}; typedef int DIRECTION;STRUCT elementtype{postion pos;direction Direction;}; Enum E_state{e_state_error = 0,E_STATE_OK = 1,};//Linked list node nodestruct lstacknode{elementtype Data;lstacknode * next;};/ /link Stack struct linkstack{lstacknode * bottom;lstacknode * top;int Len;}; Lstacknode * Makenode (ElementType data) {Lstacknode * Pnode = (Lstacknode *) malloc (sizeof (Lstacknode)); if (pnode! = NULL) { Pnode->data = Data;pnode->next = NULL;} return pnode;} E_state stackinit (Linkstack * lstack) {//Assign head node Lstacknode * Pnode = (Lstacknode *) malloc (sizeof (Lstacknode)); if (Pnode = = N ULL) {return e_state_error;} Pnode->next = null;//The top of the stack and the bottom of the stack are empty when they point to the same node. Lstack->bottom = Lstack->top = Pnode;lstack->len = 0;return E_State_Ok ;} void Stackclear (Linkstack * stack) {Lstacknode * next = Stack->bottom->next;while (next! = NULL) {Lstacknode * Freenod E = next;/* and careless ... Free (freenode); next = Next->next;*/next = next->next;free (freenode);} Stack->top = stack->bottom;//forgot//Error Stack->bottom->next = Null;stack->len = 0;} void Stackdestory (Linkstack * stack) {stackclear (stack); free (stack->top); stack->top = Stack->bottom = NULL;} E_state stackgettop (linkstack stack,elementtype * topdata) {//List of the stack top points to the top element if (stack.top! = stack.bottom) {*topdata = stack . Top->data;return E_state_ok;} Else{return E_state_error;}} int Stacklen (Linkstack stack) {return stack.len;} BOOL Stackempty (Linkstack stack) {return stack.top = = Stack.bottom? True:false;} E_state Stackpush (Linkstack * stack,elementtype data) {Lstacknode * node = makenode (data); if (node! = NULL) {stack->top- >next = Node;stack->top = node;stack->len++;} Else{return E_state_error;}} E_state Stackpop (linkstack * stack,elementtype * data) {if (stack->top! = Stack->bottom) {//First point to first element. Lstacknode * Next = Stack->bottom;*data = stack->top->data;//finds the precursor of the top element of the stack while (next->next! = stack->top) {next = next- >next;} FreE (stack->top); next->next = Null;stack->top = next;//forgot to add Stack->len--;return e_state_ok;} Else{return E_state_error;}} int mazedata[10][10] = {{0,0,0,0,0,0,0,0,0,0},{0,1,1,0,1,1,1,0,1,0},{0,1,1,0,1,1,1,0,1,0},{0,1,1,1,1,0,0,1,1,0},{ 0,1,0,0,0,1,1,1,1,0},{0,1,1,1,0,1,1,1,1,0},{0,1,0,1,1,1,0,1,1,0},{0,1,0,0,0,1,0,0,1,0},{0,0,1,1,1,1,1,1,1,0},{ Whether a node of the 0,0,0,0,0,0,0,0,0,0},};//pos coordinate can be passed bool Ispass (postion POS) {int pass = mazedata[pos.x][pos.y];//node is not a barrier, and Is the node that did not pass return pass = = 1? True:false;} Gets the next coordinate postion getnextpos (postion pos,direction di) {int addarray[][2] = {{0,1},{1,0},{0,-1},{-1,0}}; Postion nextpos;nextpos.x = pos.x + addarray[di][0];nextpos.y = pos.y + Addarray[di][1];return NextPos;} void Footpos (postion pos,int curstep) {Mazedata[pos.x][pos.y] = curstep;} void Delpos (postion pos) {Mazedata[pos.x][pos.y] =-1;} int Mazepath (postion startpos,postion endpos) {linkstack stack; ElementType data;int curstep = 2;stackinit (&stack);d o{if (Ispass (startpos)) {Footpos (Startpos,curstEP); curstep++;d ata.pos.x = startpos.x;data.pos.y = Startpos.y;data.direction = 0;stackpush (&stack,data); if ( Startpos.x = = Endpos.x && Startpos.y = = endpos.y) {stackdestory (&stack); return 1;} Else{startpos = Getnextpos (startpos,0);}} Else{if (!stackempty (Stack)) {Stackpop (&stack,&data); Curstep--;while (data.direction = = 3 &&!) Stackempty (Stack)) {//error forgot to delete: Delpos (Data.pos); Stackpop (&stack,&data); Curstep--;//startpos.x = Data.pos.x;//startpos.y = Data.pos.y;} if (Data.direction < 3) {data.direction++;//change direction to explore Stackpush (&stack,data); curstep++;startpos = Getnextpos ( data.pos,data.direction);}}} while (!stackempty (stack)); stackdestory (&stack); return 0;} Output solution void Printmaze () {int I,j;int x = 10,y = 10;for (i=0;i<x;i++) {for (j=0;j<y;j++) printf ("%3d", Mazedata[i][j]); printf ("\ n");} printf ("\ n");} int _tmain (int argc, _tchar* argv[]) {printf ("Initial maze as follows: \ n"); Printmaze (); Postion startpos; Postion endpos;printf ("Please enter the starting position coordinates x, Y (from 0 rows, 0 columns):"); scanf ("%d%d", &startpos.x,&startpos.y);p rintf ("Please enter the terminating position coordinates x, Y (from 0 rows, 0 columns):"); scanf ("%d%d", &endpos.x,&endpos.y); int result = Mazepath (Startpos,endpos), if (result = = 1) {printf ("Maze solution is as follows: (start address 2) \ n"); Printmaze ();} Else{printf ("Maze from (%d,%d) to (%d,%d) no solution \ n", startpos.x,startpos.y,endpos.x,endpos.y); Printmaze ();} return 0;}

The initial maze of 0 represents the wall, and 1 represents the pathway.




See Data structure Write code (13) Application of Stack (IV.) Maze Solver

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.