BFS- eight digital state map search

  Placed on the board of a 3x3 numbered 1 to 8 eight blocks, each accounting for a grid, in addition to a space. And a space adjacent digital box can be moved to the boxes. Task 1: Specifies initial target chess game chess and calculates the minimum number of movement steps; Task 2: the sequence number of a digital mobile.

  The blank as 0, a total of nine numbers.

  Sample input:

  1 2 3 0 8 4 7 6 5 

  1 0 3 8 2 3 7 6 5 

  Sample output:

  2

  1. FIG state as a chess game, a total of 9! Status = 362880. Start from an initial chess game, each moving to the next state, reaches the target stop chess game.

  2. Cantor Expand

    Cantor expansion is a special kind of hash function. Which is a function of the input arrangement, it is calculated from small to large order rank in the whole arrangement.

    eg: Analyzing 2143 {1,2,3,4} is the full array of precedence.

      (1) arranged first of all smaller than 2. Ratio of only 1, arranged behind the three small number 2 has 2 * 1 * 3 = 3! One, written as 1 * 3! 6 =

      (2) as the first 2, second of all permutations of less than 1. No, written 0 * 2! = 0

      (3) for the first two 21, the third of all permutations of less than 4. Only 3 a number, written as 1 * 1! = 1

      (3) 214 as the top three, the fourth all permutations of less than 3. No, written 0 * 0! = 0

      Sum: 1 * 3! * 2 + 0! * 1 + 1! + 0 * 0! 7 =

    Therefore calculated ranking is X = a [n] * (n-1)! + A [n-1] * (n-2)! + ... + a [1] * 0!

  

. 1 #include <bits / STDC ++ H.>
 2 #include <Queue>
 . 3  the using  namespace STD;
 . 4  
. 5  const  int len = 362,880 ; // status = 362880 kinds 9! 
. 6  int visited [len] = { 0 }; / / flag state has been used to weight 
. 7  int start [ . 9 ]; // start state 
. 8  int goal [ . 9 ]; // target state 
. 9  int Factory [] = { . 1 , . 1 , 2 , . 6, 24 , 120 , 720 , 5040 , 40320 , 362800 }; // factorial 0 to 9 
10  int the dir [ . 4 ] [ 2 ] = {{- . 1 , 0 }, { 0 , - . 1 }, { . 1 , 0 }, { 0 , . 1 }};
 . 11  
12 is  struct Node {
 13 is      int state [ . 9 ]; // chess game state down a one-dimensional storage 
14      int DIS; //Recording the number of steps starting state moves from the current state 
15  };
 16   
. 17  BOOL Cantor ( int STR [], int n-) {
 18 is      int Result = 0 ;
 . 19      for ( int I = 0 ; I <n-; I ++ ) {
 20 is          int CNT = 0 ;
 21 is          for ( int J = I + . 1 ; J <n-; J ++ ) {
 22 is              IF (STR [I]> STR [J])
 23 is                  CNT ++ ;
 24          }
 25         Factory = CNT + + Result [Ni- . 1 ];
 26 is      }
 27      IF (! visited [Result]) {
 28          visited [Result] = . 1 ;
 29          return  . 1 ;
 30      }
 31 is      return  0 ;
 32  }
 33 is  
34 is  int the BFS () {
 35      Node head, Next;
 36      the memcpy (head.state, start, the sizeof (head.state)); // copy start state and inserted into the queue 
37 [      head.dis = 0 ; 
 38 is      Cantor (head.state,. 9 );
 39      Queue <Node> Q;
 40      q.push (head);
 41 is      
42 is      the while (! Q.empty ()) {
 43 is          head = q.front ();
 44 is          q.pop ();
 45          int Z;
 46 is          for (Z = 0 ; Z < . 9 ; Z ++ )
 47              IF (head.state [Z] == 0 ) // find 0 
48                  BREAK ;
 49          int X = Z% . 3 ; // the conversion of one-dimensional position 0 a two-dimensional horizontal and vertical coordinates 
50          int Y = Z /. 3 ;
 51 is          for ( int I = 0 ; I < . 9 ; I ++ ) {
 52 is              int newx = X + the dir [I] [ 0 ];
 53 is              int newy, = Y + the dir [I] [ . 1 ];
 54 is              int Newz = newx + . 3 * newy,; // to 0 after moving back into a one-dimensional coordinate 
55              IF (newx> = 0 && newx < . 3 && newy,> = 0 && newy, < . 3 ) { // avoid cross-border 
56 is                  the memcpy (& Next, & head, sizeof ( structNode));
 57 is                  the swap (next.state [Z], next.state [Newz]); // After copying the original state, changing the position of 0 
58                  next.dis ++ ;
 59                  IF (memcmp (next.state, Goal , the sizeof (next.state)) == 0 )
 60                      return next.dis;
 61 is                  IF (Cantor (next.state, . 9 )) // check the weight 
62 is                      q.push (Next);
 63 is              } 
 64          }
 65      }
 66      return - . 1 ;
 67  }
 68 
69 int main(){
70     for(int i=0; i<9; i++)
71         scanf("%d", start+i);
72     for(int i=0; i<9; i++)
73         scanf("%d", goal+i);
74     
75     int num = BFS();
76     if(num != -1)
77         printf("%d\n",num);
78     else
79         printf("Impossible\n"); 
80 }

  (1) for storing a state diagram and structure of the number of steps

  (2) for moving an array of

  (3) for marking array deduplication

  (4) in advance to reckon factorial stored in the array

  (5) CANTON function sentenced to heavy

  (6) BFS function: queue <node> q; node head, next;

  Interconversion of a digital block and the one-dimensional coordinate two-dimensional coordinates (7) in the state of FIG.

  (8) to check the legality of coordinates   

Eight digital multiple solutions: https://www.cnblogs.com/zufezzt/p/5659276.html

Guess you like

Origin www.cnblogs.com/0424lrn/p/12229971.html