#include #include #include using namespace std ; const int MAXN = 100000 ; int st[MAXN][3] ; int n ; vector wc ; // columns we work with int gcolor, gcolumn ; // destination column void move(int src, int dst) { int si = 2 ; int di = 0 ; while (st[src][si] == 0) si-- ; while (st[dst][di] != 0) di++ ; swap(st[dst][di], st[src][si]) ; } int enc() { int r = 0 ; for (int i=0; i<6; i++) for (int j=0; j<3; j++) r = r * 3 + st[i][j] ; return r ; } int enc2() { int r = 0 ; for (int i=0; i<(int)wc.size(); i++) for (int j=0; j<3; j++) { int t = st[wc[i]][j] ; if (t > 0) t = (t == gcolor ? 1 : 2) ; r = r * 3 + t ; } return r ; } void dec(int v) { for (int i=5; i>=0; i--) for (int j=2; j>=0; j--) { st[i][j] = v % 3 ; v /= 3 ; } } map dist ; void recur(int at, int togo) { if (togo == 0) { dist[enc()] = 0 ; return ; } if (at >= 6) return ; for (int i=0; i<=3 && i<=togo; i++) { recur(at+1, togo-i) ; if (i < 3) st[at][2-i] = 0 ; } for (int i=0; i<3; i++) st[at][i] = 2 ; } int main() { for (int i=0; i<6; i++) for (int j=0; j<3; j++) st[i][j] = min(i+1, 2) ; recur(1, 3) ; for (int d=0; ; d++) { vector cs ; for (auto x: dist) if (x.second == d) cs.push_back(x.first) ; if (cs.size() == 0) break ; for (auto prev: cs) { dec(prev) ; for (int i=0; i<6; i++) { if (st[i][0] != 0) { int fi = 0 ; while (fi < 2 && st[i][fi+1] != 0) fi++ ; int ok = 0 ; for (int k=0; k<=fi; k++) if (st[i][k] == 1) { ok = 1 ; break ; } for (int j=0; j<6; j++) { if (i != j && st[j][2] == 0) { int tj = 2 ; while (tj > 0 && st[j][tj-1] == 0) tj-- ; if (i == 0 && fi == 0) // never move first one continue ; swap(st[i][fi], st[j][tj]) ; int dst = enc() ; if (dist.find(dst) == dist.end()) dist[dst] = d+1 ; swap(st[i][fi], st[j][tj]) ; } } } } } } cin >> n ; n++ ; vector seq(n) ; vector> sol ; for (int i=0; i> st[i][j] ; for (int i=0; i<3; i++) st[n][i] = n ; // dummy column int osollen = 0 ; while (1) { for (int i=0; i