1 #include <iostream> 2 #include <cstdlib> 3 #include <vector> 4 #include <string> 5 #include <cstring> 6 #include <ctime> 7 #include <cstdio> 8 #include <algorithm> 9 #include <numeric> 10 //#include "Student.h" 11 //#include "SortFunction.h" 12 using namespace std; 13 int global=3; 14 //==================================================================== 15 #define swap(x,y) (x)=(x)+(y);(y)=(x)-(y);(x)=(x)-(y); 16 17 //==================================================================== 18 19 20 21 const int N=8; 22 char a[N+N+2]; 23 int A[N],B[N];//存A,B的索引 24 int ha=0; 25 int hb=0; 26 //B往左移 27 int byi(int t) 28 { 29 if(t==2) 30 { 31 if(a[0]=='#') 32 { 33 swap(a[0],a[t]); 34 return 0; 35 } 36 else if (a[1]=='#') 37 { 38 swap(a[1],a[t]); 39 return 1; 40 } 41 return -1; 42 } 43 else if(t==1) 44 { 45 if(a[0]=='#') 46 { 47 swap(a[0],a[t]); 48 return 0; 49 } 50 else 51 return -1; 52 } 53 if(t-3>=0&&a[t-2]=='#'&&a[t-3]!='B') 54 { 55 swap(a[t],a[t-2]);return t-2; 56 } 57 else if(t-2>=0&&a[t-1]=='#'&&a[t-2]!='B') 58 { 59 swap(a[t],a[t-1]);return t-1; 60 } 61 else if(t==B[hb] && a[t-2]=='#') 62 { 63 swap(a[t],a[t-2]);return t-2; 64 } 65 else if(t==B[hb] && a[t-1]=='#') 66 { 67 swap(a[t],a[t-1]);return t-1; 68 } 69 return -1; 70 71 } 72 73 //A往右移 74 int ayi(int t) 75 { 76 int i=0; 77 if(t==2*N-2) 78 { 79 if(a[t+2]=='#') 80 { 81 swap(a[t],a[t+2]); 82 return t+2; 83 } 84 else if(a[t+1]=='#') 85 { 86 swap(a[t],a[t+1]);return t+1; 87 } 88 return -1; 89 } 90 else if(i==2*N-1) 91 { 92 if(a[t+1]=='#') 93 { 94 swap(a[t],a[t+1]); 95 return t+1; 96 } 97 else 98 return -1; 99 } 100 if(t+3<2*N+2&&a[t+2]=='#'&&a[t+3]!='A') 101 { 102 swap(a[t],a[t+2]); 103 return t+2; 104 } 105 else if(t+2<2*N+2&&a[t+1]=='#'&&a[t+2]!='A') 106 { 107 swap(a[t],a[t+1]); 108 return t+1; 109 } 110 else if(t==A[ha] && a[t+2]=='#') 111 { 112 swap(a[t],a[t+2]);return t+2; 113 } 114 else if(t==A[ha] && a[t+1]=='#') 115 { 116 swap(a[t],a[t+1]);return t+1; 117 } 118 return -1; 119 } 120 121 void tiaoqi() 122 { 123 //初始化 124 int i=0; 125 for(; i<N; ++i) 126 { 127 a[i]='A'; 128 a[i+N+1]='B'; 129 A[i]=N-1-i; 130 B[i]=i+N+1; 131 } 132 a[i]='#'; 133 a[i+N+1]='\0'; 134 cout << a<<endl; 135 136 int t=0,t1=0; 137 while(ha!=N || hb != N) 138 { 139 t=ha; 140 while( t<N && (t1=ayi(A[t])) >=0 ) 141 { 142 143 A[t]=t1; 144 if(A[ha]==N+N-ha) 145 ++ha; 146 ++t; 147 // cout<<a<<endl; 148 } 149 cout<<a<<endl; 150 t=hb; 151 while(t<N && (t1=byi(B[t])) >=0) 152 { 153 154 B[t]=t1; 155 if(B[hb]==hb) 156 ++hb; 157 ++t; 158 // cout<<a<<endl; 159 } 160 cout<<a<<endl; 161 } 162 } 163 int main() 164 { 165 tiaoqi(); 166 system("pause"); 167 return 0; 168 }
转载于:https://www.cnblogs.com/iclod/archive/2012/10/23/2736229.html