Backtracking algorithm for solving maze problem (C language)

Backtracking is also known as heuristics, which first question to abandon restrictions on size of candidate solutions and issues one by one and test enumerate certain order. When they find the current candidate solution can not be the solution, on the choice of the next candidate solution; if the current candidate when the lifting of the scale of the problem does not meet requirements, meet all other requirements, to continue to expand the scale of the current candidate solutions, and continue to test if the current candidate solution meets all the requirements, including the scale of the problem, that. is a solution to the problem of candidate solutions. in backtracking, the current candidate solutions to give up, the next process to find a candidate solution is called backtracking expand the size of the current candidate solutions, and continue to test the process of becoming forward tentatively.

In order to ensure that the program can be terminated, adjustment, must ensure that the number has been given up fill sequence is not re-testing, which requires a certain order according to the number of filling model generating sequence is set to the candidates sequentially a solution being tested, one by one in this order, and generates a test candidate.

For the maze, I would like to use in the difficulties of backtracking on how the solution space to sort, to ensure that was given up to fill the number of sequences are not tested again. In the two-dimensional maze, starting from the starting point, each point by neighbors domain calculation, according to the right, the order of the left, in the search for the next goal, a road is entered, i.e. no way back, before the point a direction from the filter, can constitute an ordered model. i.e. maze following table

{  1,1,1,1,1,1,1,1,1,1,
    0,0,0,1,0,0,0,1,0,1,
    1,1,0,1,0,0,0,1,0,1,
    1,0,0,0,0,1,1,0,0,1,
    1,0,1,1,1,0,0,0,0,1,
    1,0,0,0,1,0,0,0,0,0,
    1,0,1,0,0,0,1,0,0,1,
    1,0,1,1,1,0,1,1,0,1,
    1,1,0,0,0,0,0,0,0,1,
    1,1,1,1,1,1,1,1,1,1}

Starting from the starting point, sequential find a selected point column constitute an ordered sequence to the next point, if all four directions searched no way to go back to back, the direction of the front and set point plus 1, and so on .... ...

 1  2  3  4  5  6  7  8  9  10
 x  1  1  1  2  3  3  3  2  ...
 Y  0  1  2  2  2  3  4  4  ...
 c  1  1  3  3  1  1  2  1  ...

#include <stdio.h>
#include <stdlib.h>
#define N1 10
#define N2 10
typedef struct Node
{
int x; x-coordinate memory //
int y; // save Y coordinate
int c; // save the point direction may be located at the point, the right represents 1, 2 up, 3 to the left, right 4
} linkstack;

Top linkstack [100];
// labyrinth matrix
int Maze [N1] [N2] = {1,1,1,1,1,1,1,1,1,1,
    0,0,0,1,0, 0,0,1,0,1,
    1,1,0,1,0,0,0,1,0,1,
    1,0,0,0,0,1,1,0,0,1,
    1,0,1,1,1,0,0,0,0,1,
    1,0,0,0,1,0,0,0,0,0,
    1,0,1,0,0, 0,1,0,0,1,
    1,0,1,1,1,0,1,1,0,1,
    1,1,0,0,0,0,0,0,0,1,
    1,1,1,1,1,1,1,1,1,1};

int i,j,k,m=0;

main ()
{
// Initialization top [], the number of all direction to the left
for (I = 0; I <N1 * N2; I ++)
{
Top [I] .c =. 1;
}
the printf ( "The Maze IS: / n ");

// print the original labyrinth matrix
for (I = 0; I <N1; I ++)
{
 for (J = 0; J <N2; J ++)
 the printf (Maze [I] [J] "*":? "");
 The printf ( "/ n-");
}
I = 0; Top [I] .x =. 1; Top [I] .y = 0;
Maze [. 1] [0] = 2;
/ * backtracking * /
do
{
 IF ( top [i] .c <5) // heuristic may further forwardly
 {
  IF (Top [I] .x. 5 && == Top [I] == .y. 9) has found a combination //
  {
   // print path
   the printf ( "of The Way% D IS: / n-", m ++);
   for (J = 0; J <= I; J ++)
   {
    the printf ( "(% D,% D) ->", Top [J]. X, Top [J] .y);
   }
   the printf ( "/ n-");

   // print the selected path maze
   for (J = 0; J <N1; J ++)
   {
    for (K = 0; K <N2; K ++)
    {
     IF (Maze [J] [K] == 0) the printf ( " ");
     the else IF (Maze [J] [K] == 2) the printf (" O ");
     the else the printf (" * ");
    }
    the printf (" / n-");
   }

   maze[top[i].x][top[i].y]=0;
   top[i].c = 1;
   i--;
   top[i].c += 1;
   continue;
  }
  switch (top[i].c)   //向前试探
  {
   case 1:
    {
     if(maze[top[i].x][top[i].y+1]==0)
     {
      i++;
      top[i].x=top[i-1].x;
      top[i].y=top[i-1].y+1;
      maze[top[i].x][top[i].y]=2;
     }
     else
     {
      top[i].c += 1;
     }
     break;
    }
   case 2:
    {
     if(maze[top[i].x-1][top[i].y]==0)
     {
      i++;
      top[i].x=top[i-1].x-1;
      top[i].y=top[i-1].y;
      maze[top[i].x][top[i].y]=2;
     }
     else
     {
      top[i].c += 1;
     }
     break;
    }
   case 3:
    {
     if(maze[top[i].x][top[i].y-1]==0)
     {
      i++;
      top[i].x=top[i-1].x;
      top[i].y=top[i-1].y-1;
      maze[top[i].x][top[i].y]=2;
     }
     else
     {
      top[i].c += 1;
     }
     break;
    }
   case 4:
    {
     if(maze[top[i].x+1][top[i].y]==0)
     {
      i++;
      top[i].x=top[i-1].x+1;
      top[i].y=top[i-1].y;
      maze[top[i].x][top[i].y]=2;
     }
     else
     {
      top[i].c += 1;
     }
     break;
    }
  }
 }
 else   //回溯
 {
  if(i==0) return;   //已找完所有解
  maze[top[i].x][top[i].y]=0;
  top[i].c = 1;
  i--;
  top[i].c += 1;
 }
}while(1);
}

Released four original articles · won praise 1 · views 5545

Guess you like

Origin blog.csdn.net/robinhzp/article/details/316642