The way of data structure and algorithm learning: Maze problem--backtracking thinking find all paths

Source: Internet
Author: User

Today, my little friend and I said that the maze problem before the question, I changed the change, I feel that before the idea of writing something is not clear, but also more chaotic, re-wrote an article ... Don't worry, ha.


First, the Maze problem description:

Given a maze, as well as the start and end points, the algorithm is designed to find a path that can reach the end point. Once resolved, find the shortest path and all paths.


Second, the solution:

1, find a path to reach is not difficult, as long as set the direction, and then each point to find a can go direction has been to the feasible direction to go.


2, find the shortest path. To find the shortest path, you can try the breadth-first algorithm--bfs. BFS to find a point to another point in the shortest path is still very convenient (no weight), the main idea is that each point can be calculated to the state, the first to reach the end of the state is the shortest path length. It may be abstract to say so, so take a look at the following picture:


3. Find all paths. The easiest way to find all the paths is to find a path, then go back to the previous point and see if there is anywhere else to go and then move down in this direction. So we can find all the paths backwards.


Third, the code:

Find out how all paths are implemented:


#include <stdio.h> #include <stdlib.h> #define WALL 1#define ROAD 0#define end-1#define TRUE 1#define FALSE 0#d Efine visited 1#define unvisited 0#define BARRIER-1//Four weeks is the dead end of the wall # define MAXSIZE 11//The maximum number of midpoint of each row or column in a maze # define Dir_size 4//direction #define STACK_SIZE 128typedef int status;//The point typedef struct Point{int X;int y in the maze; Status status; Status visited;} point;typedef struct stack{point point[stack_size];int top;} Stack, *stackptr;void stack_init (stackptr s); void Stack_push (Stackptr s, point point); void Stack_pop (Stackptr s);// Search all paths. Multi-entry multiple exits add a starting point array, the starting point through the loop as the bottom of the stack, traversing the path to different endpoints can be void Maze_search (Stackptr s, point maze[maxsize][maxsize],point *dir); int Main () {stackptr stack; Point Dir[dir_size]; Point up, down, right, left; Point Maze[maxsize][maxsize];int I, j;//initialize maze for (i = 0; i < MAXSIZE; i++) {for (j = 0; J < MAXSIZE; J + +) Maze[i][j].s Tatus = ROAD;} for (i = 0; i < MAXSIZE; i++) for (j = 0; J < MAXSIZE; J + +) {maze[i][j].x = I;maze[i][j].y = j;maze[i][j].visited = UN visited;} for (i = 0; i < MAXSIZE; i++) {maze[0][i].status = Wall;maze[10][i].status = wall;maze[0][i].visited = visited;maze[10][i].visited = visited;} for (i = 0; i < MAXSIZE; i++) {maze[i][0].status = Wall;maze[i][10].status = wall;maze[i][0].visited = visited;maze[i][1 0].visited = visited;} Maze[1][2].status = Wall;maze[1][3].status = Wall;maze[1][5].status = Wall;maze[1][6].status = WALL;maze[1][9].status = Wall;maze[2][3].status = Wall;maze[2][4].status = Wall;maze[2][7].status = Wall;maze[2][8].status = WALL;maze[2][9]. Status = Wall;maze[3][3].status = Wall;maze[3][5].status = Wall;maze[3][6].status = Wall;maze[3][8].status = WALL;maze[4 ][2].status = Wall;maze[4][4].status = Wall;maze[4][7].status = Wall;maze[4][9].status = WALL;maze[5][6].status = WALL; Maze[5][3].status = Wall;maze[5][5].status = Wall;maze[5][8].status = Wall;maze[6][1].status = WALL;maze[6][4].status = Wall;maze[6][6].status = Wall;maze[6][7].status = Wall;maze[6][9].status = Wall;maze[7][4].status = WALL;maze[7][8]. Status = WALl;maze[7][9].status = Wall;maze[8][2].status = Wall;maze[8][6].status = Wall;maze[9][1].status = WALL;maze[9][2]. Status = wall;maze[1][1].visited = visited;maze[1][2].visited = visited;maze[1][3].visited = VISITED;maze[1][5]. visited = visited;maze[1][6].visited = visited;maze[1][9].visited = visited;maze[2][3].visited = VISITED;maze[2][4]. visited = visited;maze[2][7].visited = visited;maze[2][8].visited = visited;maze[2][9].visited = VISITED;maze[3][3]. visited = visited;maze[3][5].visited = visited;maze[3][6].visited = visited;maze[3][8].visited = VISITED;maze[4][2]. visited = visited;maze[4][4].visited = visited;maze[4][7].visited = visited;maze[4][9].visited = VISITED;maze[5][6]. visited = visited;maze[5][3].visited = visited;maze[5][5].visited = visited;maze[5][8].visited = VISITED;maze[6][1]. visited = visited;maze[6][4].visited = visited;maze[6][6].visited = visited;maze[6][7].visited = VISITED;maze[6][9]. visited = visited;maze[7][4].visited = visited;maze[7][8].visited = Visited;maze[7][9].visited = visited;maze[8][2].visited = visited;maze[8][6].visited = visited;maze[9][1].visited = VISITED;maze [9] [2].visited = Visited;maze[9][9].status = end;up.x = -1;up.y = 0;up.status = Road;left.x = 0;left.y = -1;left.status = ROA D;right.x = 0;right.y = 1;right.status = road;down.x = 1;DOWN.Y = 0;down.status = road;dir[0] = left;dir[1] = down;dir[2] = right;dir[3] = up;//output maze for (i = 0; i < MAXSIZE, i++) {for (j = 0; J < MAXSIZE; J + +) {if (Maze[i][j].status = = WALL) printf ("▇"); elseprintf ("");} printf ("\ n");} printf ("\ n");//Initialize stack stack = (stackptr) malloc (sizeof (stack)) to save the path; Stack_init (stack); Maze_search (Stack, Maze, dir); return 0;} void Stack_init (Stackptr s) {point temp;if (s = = NULL) return;temp.x = 1;temp.y = 1;temp.status = road;temp.visited = VISITE D;s->top = 0;s->point[s->top] = Temp;return;} void Stack_push (Stackptr s, point point) {if (S->top = = (stack_size-1)) {printf ("Stack full! "); return;} S->top + = 1;s->point[s->top] = point;} void Stack_pop (Stackptr s{Point Temp;if (s->top = =-1) {printf ("Stack is empty! "); return;} temp.x = 0;TEMP.Y = 0;temp.status = Road;s->point[s->top] = temp;s->top-= 1;} void Maze_search (Stackptr s, point maze[maxsize][maxsize],point *dir) {int i,j;if (s->point[s->top].status = = END) {for (i = 0; i < MAXSIZE, i++) {for (j = 0; J < MAXSIZE; J + +) {if (Maze[i][j].status = = WALL) printf ("▇"); else if (Maz E[i][j].status = = ROAD | | Maze[i][j].status = = END) && maze[i][j].visited = = visited && maze[i][j].visited! = BARRIER) printf ("※"); elseprintf ("");} printf ("\ n");} maze[9][9].visited = unvisited; Stack_pop (s);p rintf ("\ n"); return;} for (i = 0; i < dir_size; i++) {if (maze[s->point[s->top].x + dir[i].x][s->point[s->top].y + Dir[i].y].statu s! = wall&& maze[s->point[s->top].x + dir[i].x][s->point[s->top].y + dir[i].y].visited! = visited) { maze[s->point[s->top].x + dir[i].x][s->point[s->top].y + dir[i].y].visited = visited; Stack_push (S, maze[s->point[s->top].x + dir[i].x][s->point[s->top].y + dir[i].y]); Maze_search (S, Maze, dir);}} for (i = 0; i < dir_size; i++) {if (maze[s->point[s->top].x + dir[i].x][s->point[s->top].y + Dir[i].y].statu s! = wall&& maze[s->point[s->top].x + dir[i].x][s->point[s->top].y + dir[i].y].visited! = visited) { maze[s->point[s->top].x][s->point[s->top].y].visited = unvisited; Stack_pop (s); return;}} if (Maze[s->point[s->top].x][s->point[s->top].y-1].status = = ROAD && maze[s->point[s->top ].x][s->point[s->top].y-1].visited = = unvisited) maze[s->point[s->top].x][s->point[s->top].y]. visited = unvisited;maze[s->point[s->top].x][s->point[s->top].y].visited = BARRIER; Stack_pop (s); return;}


The way of data structure and algorithm learning: Maze problem--backtracking thinking find all paths

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.