One-dimensional checkers (BFS)

Is a one-dimensional checkers on a 1 × (2N + 1) to play the game board. A total of N pieces, where N is the number of black, the N is white. Before the start of the game, N a stone is placed on a white, N black stones are placed on the other side, the intermediate empty lattice.

There are two methods is permitted to move in this game: you can put a piece moves to a space adjacent to it; you can put a piece skipping one (only one) with its different colored pieces reach spaces.

For the case where N = 3, the state of the board as follows:

 1 WWW BBB
 2 WW WBBB
 3 WWBW BB
 4 WWBWB B
 5 WWB BWB
 6 W BWBWB
 7 WBWBWB
 8 BW WBWB
 9 BWBW WB
10 BWBWBW
11 BWBWB W
12 BWB BWW
13 B BWBWW
14 BB WBWW
15 BBBW WW
16 BBB WWW

Corresponding to the position where the space (from the left) is: 356,421,357,642,354.

Input Format

Enter only an integer representing the value for N (1≤N≤10) a.

Output Format

Sequentially outputs a position where the board spaces, separated by spaces between each integer, the number of each row 5 (end of each line without spaces, last line number less than 5; minimum set if multiple mobile steps solution, a first output the minimum number of solution)

Sample input

4 

Sample Output

4 6 7 5 3
2 4 6 8 9
7 5 3 1 2
4 6 8 7 5
3 4 6 5

 

Binary to indicate the status of the board

 

  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <iostream>
  4 #include <string>
  5 #include <math.h>
  6 #include <algorithm>
  7 #include <vector>
  8 #include <stack>
  9 #include <queue>
 10 #include <set>
 11 #include <map>
 12 #include <sstream>
 13 const int INF=0x3f3f3f3f;
 14 typedef long long LL;
 15 using namespace std;
 16 
 17 int n;
 18 int P[(1<<20)*21+5];
 19 bool vis[1<<20][21];
 20 struct node
 21 {
 22     int num;
 23     int pos;
 24 };
 25 
 26 int main()
 27 {
 28     #ifdef DEBUG
 29     freopen("sample.txt","r",stdin);
 30     #endif
 31     
 32     scanf("%d",&n); 
 33     vector<node> a;
 34     queue<int> qe;
 35     int first=(1<<n)-1;
 36     int last=first<<n;
 37     printf("%d %d\n",first,last); 
 38     qe.push(a.size());
 39     vis[first][n]=true;
 40     a.push_back({first,n});
 41     while(!qe.empty())
 42     {
 43         int id=qe.front();
 44         qe.pop();
 45         int num=a[id].num;
 46         int pos=a[id].pos;
 47         if(num==last&&pos==n)
 48         {
 49             vector<int> ans;
 50             for(int i=id;i;i=P[i])
 51             {
 52                 ans.push_back ( 2 * Na [I] + .POS . 1 );
 53 is              }
 54 is              Reverse (ans.begin (), ans.end ());
 55              for ( int I = 0 ; I <ans.size (); I ++ )
 56 is                  the printf ( " % C% D " , ANS [I], I% . 5 == . 4 ? ' \ n- ' : '  ' );
 57 is              BREAK ;
 58          }
 59          // spaces left blank piece is moved to the position 
60          IF (POS < 2* N-)
 61 is          {
 62 is              int TN = NUM;
 63 is              int TP = + POS . 1 ;
 64              IF (! VIS [TN] [TP])
 65              {
 66                  ) qe.push (a.size ();
 67                  VIS [TN] [ TP] = to true ;
 68                  P [a.size ()] = ID;
 69                  a.push_back ({TN, TP});
 70              }
 71 is          }
 72          // pawn moves to the right of the space position of the space 
73 is          IF (POS> 0 )
74          {
 75              int TN = NUM;
 76              int TP = POS- . 1 ;
 77              IF (! VIS [TN] [TP])
 78              {
 79                  qe.push (a.size ());
 80                  VIS [TN] [TP] = to true ;
 81                  P [a.size ()] = ID;
 82                  a.push_back ({TN, TP});
 83              }
 84          } 
 85          // spaces left pieces jump location spaces 
86          IF (POS <= 2 * N- 2 && ((+ POS NUM >>1&1)^(num>>pos&1)))
 87         {
 88             int tn=num^(3<<pos);
 89             int tp=pos+2;
 90             if(!vis[tn][tp])
 91             {
 92                 qe.push(a.size());
 93                 vis[tn][tp]=true;
 94                 P[a.size()]=id;
 95                 a.push_back({tn,tp});
 96             }
 97         }
 98         //Blank spaces left position jump pieces 
99          IF (POS> = 2 && ((NUM POS- >> . 1 & . 1 ) ^ (NUM POS- >> 2 & . 1 )))
 100          {
 101              int TN NUM = ^ ( . 3 POS- << 2 );
 102              int TP = POS- 2 ;
 103              IF (! VIS [TN] [TP])
 104              {
 105                  ) qe.push (a.size ();
 106                  VIS [TN] [TP] = to true ;
 107                  P [a.size ()] = ID;
 108                 a.push_back({tn,tp});
109             }
110         }
111     }
112     return 0;
113 }

 

-

Guess you like

Origin www.cnblogs.com/jiamian/p/12189981.html