Advanced algorithms - wide search

The following are the one pass and code OJ title

· Example:

Subject description:

  In a 4 * 4 has eight on the board of a Black and eight white stones, if and only if two cells have a common edge, the two chess on the grid are adjacent. Rules chess move is to swap adjacent two pieces.

Given an initial and a final board chessboard, find a shortest sequence of moves to make the final changes to the initial checkerboard chessboard.

Code:

. 1 #include <the iostream>
 2 #include <Queue>
 . 3  #define the FOR (A, B, C) for (int A = (B); A <(C); A ++)
 . 4  the using  namespace STD;
 . 5  
. 6  const  int MAXN = 16 ;
 . 7  struct the Node {    // binary sequence and number of steps the board structure memory 
. 8      int NUM, D;
 . 9  };
 10  int a, B;
 . 11  int VIS [ 100000 ];
 12 is  
13 is  void the BFS () {
 14 Queue <the Node> Q;
 15      q.push ((the Node) {A,0 });
 16      the while (! ) Q.empty () 
 . 17      {
 18 is          the Node U = q.front (); q.pop ();
 . 19          int tmp = u.num;
 20 is          IF (tmp == B) {COUT UD <<; return ;}
 21 is          for ( int I = 15 ; I> = 0 ; i--)    // board corresponding binary sequence, each position enumerated descending order 
22 is          {
 23 is              int X = ( 15 -i) / . 4 , Y = ( 15 -i)% . 4 , W = . 1 << I, Z;   //Calculating a position on the board of the x and y coordinate values 
24              IF (y < . 3 && (& tmp ( . 1 << I))! = (& Tmp ( . 1 << I- . 1 )))    // exchange to the right, the binary sequence i and i-1 exchange 
25              {
 26 is                  Z = . 1 << I- . 1 ;
 27                  IF (! VIS [tmp ^ Z ^ W]) {
 28                      VIS [tmp ^ Z ^ W] = . 1 ;
 29                      q.push ( (the Node) {tmp ^ W ^ Z, U.D. + . 1 });
 30                  }
 31 is              }
 32              IF (X < . 3 && (& tmp (. 1 ! << I)) = (& tmp ( . 1 << I- . 4 )))   // exchange downwardly, i binary sequences, and i-4 exchanger 
33 is              {
 34 is                  Z = . 1 << I- . 4 ;
 35                  IF ( ! VIS [tmp ^ Z ^ W]) {
 36                      VIS [tmp ^ Z ^ W] = . 1 ;
 37 [                      q.push ((the Node) {tmp ^ W ^ Z, U.D. + . 1 });
 38 is                  }
 39              }
 40          }
 41 is      }
 42 is  }
 43 is  
44 is  int main () 
 45  {
46     char c;
47     for(int i=15;i>=0;i--) {
48         cin>>c;
49         if(c!='0')  A += 1<<i;
50     }
51     for(int i=15;i>=0;i--) {
52         cin>>c;
53         if(c!='0')  B += 1<<i;
54     }
55     if(A==B) cout<<0;
56     else BFS();
57 }

· Example Two:

Subject description:

From the original title: HAOI 2008

  In a 4 × 4 block is placed a plurality of identical toys, toys one wants to reposition his mind an ideal state, can be moved in the predetermined four directions moving toy, and mobile location can not have toys, you will move to the initial state of the toy target state with the least number of moves.

Code;

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int dx[]={0,0,1,-1};
 4 const int dy[]={1,-1,0,0};
 5 
 6 char s[10];
 7 int mp[66000],state[5][5],st[5][5],ed[5][5],e;
 8 
 9 int get_hash(int a[5][5]){
10     int tot=0,p=1;
11     for(int i=1;i<=4;i++)
12         for(int j=1;j<=4;j++)
13             tot+=a[i][j]*p,p<<=1;
14     return tot;
15 }
16 
17 void get_state(int x){
18     for(int i=1;i<=4;i++)
19         for(int j=1;j<=4;j++)
20             state[i][j]=x&1,x>>=1;
21 }
22 
23 void read_and_parse(){
24     for(int i=1;i<=4;i++){
25         scanf("%s",s+1);
26         for(int j=1;j<=4;j++)st[i][j]=s[j]-'0';
27     }
28     for(int i=1;i<=4;i++){
29         scanf("%s",s+1);
30         for(int j=1;j<=4;j++)ed[i][j]=s[j]-'0';
31     }
32     memset(mp,-1,sizeof(mp));
33     e=get_hash(ed);
34 }
35 
36 void solve(){
37     queue<int> q;
38     q.push(get_hash(st)),mp[get_hash(st)]=0;
39     while(q.size()){
40         int u=q.front();q.pop();
41         if(u==e)break;
42         get_state(u);
43         for(int i=1;i<=4;i++)
44             for(int j=1;j<=4;j++)
45                 if(state[i][j])
46                     for(int k=0;k<4;k++){
47                         int x=i+dx[k],y=j+dy[k];
48                         if(x<1||y<1||x>4||y>4)continue;
49                         swap(state[i][j],state[x][y]);
50                         int v=get_hash(state);
51                         if(mp[v]==-1){
52                             q.push(v);
53                             mp[v]=mp[u]+1;
54                         }
55                         swap(state[i][j],state[x][y]);
56                     }
57     }
58     printf("%d\n",mp[e]);
59 }
60 
61 int main(){
62     read_and_parse();
63     solve();
64     return 0;
65 }

 

Guess you like

Origin www.cnblogs.com/juruohqk/p/10991669.html