1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | #include <iostream> #include <Windows.h> using namespace std; #define _(i,j) ((i)*((i)+1)/2+(j)+1) #define PAUSE system("pause") #define CLS system("cls") const int N=5; const int M=(N+1)*N/2; bool box[N][N]={0}; int balls,count; void func(); void show(); void play(int& balls); struct Ways {int F; int T;}; Ways way[M],save[M]; int main(void) { int I,J; I = 0; J = 0; // for(i=0;i<N;i++) for(j=0;j<=i;j++) { count = 0; for(int i=0;i<N;i++) for(int j=0;j<=i;j++) box[i][j]=true; box[I][J] = false; balls = M-1; func(); printf("%d處留白,共有%d種不同方法\n",_(I,J),count); PAUSE; for(int i=0;i<N;i++) for(int j=0;j<=i;j++) box[i][j]=true; box[I][J] = false; balls = M-1; play(balls); } PAUSE; return 0; } void func() { if(balls>1) { for(int i=0;i<N;i++) for(int j=0;j<=i;j++) { if (box[i][j]) { if (i-2 >= j) { if ((box[i-1][j])&&(!box[i-2][j])) { box[i][j]=false; box[i-1][j]=false; box[i-2][j]=true; --balls; way[balls].F = _(i,j); way[balls].T = _(i-2,j); func(); box[i][j]=true; box[i-1][j]=true; box[i-2][j]=false; ++balls; } } if (i+2 < N) { if ((box[i+1][j])&&(!box[i+2][j])) { box[i][j]=false; box[i+1][j]=false; box[i+2][j]=true; --balls; way[balls].F = _(i,j); way[balls].T = _(i+2,j); func(); box[i][j]=true; box[i+1][j]=true; box[i+2][j]=false; ++balls; } if ((box[i+1][j+1])&&(!box[i+2][j+2])) { box[i][j]=false; box[i+1][j+1]=false; box[i+2][j+2]=true; --balls; way[balls].F = _(i,j); way[balls].T = _(i+2,j+2); func(); box[i][j]=true; box[i+1][j+1]=true; box[i+2][j+2]=false; ++balls; } } if (j+2 <= i) { if ((box[i][j+1])&&(!box[i][j+2])) { box[i][j]=false; box[i][j+1]=false; box[i][j+2]=true; --balls; way[balls].F = _(i,j); way[balls].T = _(i,j+2); func(); box[i][j]=true; box[i][j+1]=true; box[i][j+2]=false; ++balls; } } if (j-2 >= 0) { if ((box[i][j-1])&&(!box[i][j-2])) { box[i][j]=false; box[i][j-1]=false; box[i][j-2]=true; --balls; way[balls].F = _(i,j); way[balls].T = _(i,j-2); func(); box[i][j]=true; box[i][j-1]=true; box[i][j-2]=false; ++balls; } if ((box[i-1][j-1])&&(!box[i-2][j-2])) { box[i][j]=false; box[i-1][j-1]=false; box[i-2][j-2]=true; --balls; way[balls].F = _(i,j); way[balls].T = _(i-2,j-2); func(); box[i][j]=true; box[i-1][j-1]=true; box[i-2][j-2]=false; ++balls; } //--天呀 } //--這是 } //--世界 } //--奇觀 return; } count++; for (int i=13;i>=1;i--) save[i] = way[i]; return; } void show() { printf(" balls:%d\n",balls); for (int i=0;i<N;i++) { for (int j=0;j<=N-i;j++) printf(" "); for (int j=0;j<=i;j++) printf((box[i][j] ? "●" : "○")); printf("\n"); } } void play(int& balls) { int Fi=0,Fj=0,Ti=0,Tj=0,Bi=0,Bj=0; CLS; show(); Sleep(1000); if (balls > 1) { for (int i=N-1;i>0;i--) if (_(i,0) <= save[(balls-1)].F) { Fi = i; Fj = save[(balls-1)].F-_(i,0);break; } for (int i=N-1;i>0;i--) if (_(i,0) <= save[(balls-1)].T) { Ti = i; Tj = save[(balls-1)].T-_(i,0);break; } Bi = (Fi+Ti)/2; Bj = (Fj+Tj)/2; for (int i=0;i<2;i++) { box[Fi][Fj]=false; box[Bi][Bj]=true; box[Ti][Tj]=false; CLS; show(); Sleep(200); box[Fi][Fj]=true ; box[Bi][Bj]=true; box[Ti][Tj]=false; CLS; show(); Sleep(200); } box[Fi][Fj]=false; box[Bi][Bj]=false; box[Ti][Tj]=true; play(--balls); } return; } |
Direct link: https://paste.plurk.com/show/247320