一維跳棋是一種在1×(2N+1) 的棋盤上玩的游戲。一共有N個棋子,其中N 個是黑的,N 個是白的。游戲開始前,N 個白棋子被放在一頭,N 個黑棋子被放在另一頭,中間的格子空着。
在這個游戲里有兩種移動方法是允許的:你可以把一個棋子移到與它相鄰的空格;你可以把一個棋子跳過一個(僅一個)與它不同色的棋子到達空格。
對於N=3 的情況,棋盤狀態依次為:
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
對應的空格所在的位置(從左數)為:3 5 6 4 2 1 3 5 7 6 4 2 3 5 4。
輸入格式
輸入僅一個整數,表示針對N(1≤N≤10) 的取值。
輸出格式
依次輸出空格所在棋盤的位置,每個整數間用空格分隔,每行5 個數(每行結尾無空格,最后一行可以不滿5 個數;如果有多組移動步數最小的解,輸出第一個數最小的解)
樣例輸入
4
樣例輸出
4 6 7 5 3 2 4 6 8 9 7 5 3 1 2 4 6 8 7 5 3 4 6 5
用二進制來表示棋盤的狀態
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*n-a[i].pos+1); 53 } 54 reverse(ans.begin(),ans.end()); 55 for(int i=0;i<ans.size();i++) 56 printf("%d%c",ans[i],i%5==4?'\n':' '); 57 break; 58 } 59 //空格左邊的棋子移動到空格位置 60 if(pos<2*n) 61 { 62 int tn=num; 63 int tp=pos+1; 64 if(!vis[tn][tp]) 65 { 66 qe.push(a.size()); 67 vis[tn][tp]=true; 68 P[a.size()]=id; 69 a.push_back({tn,tp}); 70 } 71 } 72 //空格右邊的棋子移動到空格位置 73 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]=true; 81 P[a.size()]=id; 82 a.push_back({tn,tp}); 83 } 84 } 85 //空格左邊的棋子跳到空格位置 86 if(pos<=2*n-2&&((num>>pos+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 //空格左邊的棋子跳到空格位置 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]=true; 107 P[a.size()]=id; 108 a.push_back({tn,tp}); 109 } 110 } 111 } 112 return 0; 113 }
-