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 123 124 125 126 127 128 129
| func encode(a [][]byte) (res [4][2]int) { n := len(a) for i, b := range a[0] { res[0][0] |= int(b&1) << i res[0][1] |= int(b&1) << (n - 1 - i) res[2][0] |= int(a[n-1][i]&1) << i res[2][1] |= int(a[n-1][i]&1) << (n - 1 - i) } for i, r := range a { res[1][0] |= int(r[n-1]&1) << i res[1][1] |= int(r[n-1]&1) << (n - 1 - i) res[3][0] |= int(r[0]&1) << i res[3][1] |= int(r[0]&1) << (n - 1 - i) } return }
func rotate(a [][]byte) [][]byte { n, m := len(a), len(a[0]) b := make([][]byte, m) for i := range b { b[i] = make([]byte, n) } for i, r := range a { for j, v := range r { b[j][n-1-i] = v } } return b }
func composeCube(shapes [][]string) bool { n := len(shapes[0]) a := [6][8][4][2]int{} for i, shape := range shapes { t := make([][]byte, n) for j, s := range shape { t[j] = []byte(s) } for j := 0; j < 4; j++ { a[i][j] = encode(t) t = rotate(t) } for _, r := range t { for j := 0; j < n/2; j++ { r[j], r[n-1-j] = r[n-1-j], r[j] } } for j := 4; j < 8; j++ { a[i][j] = encode(t) t = rotate(t) } }
MASK := 1<<(n-1) - 2 ok := func(v, w int) bool { return v&w == 0 && (v|w)&MASK == MASK }
type pair struct{ who, rot int } fill := [6]pair{} vis := 0 var dfs func(int) bool dfs = func(p int) bool { if p == 6 { return true } for cur := 1; cur < 6; cur++ { if vis>>cur&1 > 0 { continue } vis ^= 1 << cur for rot := 0; rot < 8; rot++ { switch p { case 1: if !ok(a[cur][rot][0][0], a[0][0][2][0]) { continue } case 2: w, r := fill[p-1].who, fill[p-1].rot if !ok(a[cur][rot][0][0], a[0][0][1][1]) || !ok(a[cur][rot][3][0], a[w][r][1][0]) || a[0][0][2][1]&1 == 0 && a[cur][rot][0][0]&1 == 0 && a[w][r][0][1]&1 == 0 { continue } case 3: w, r := fill[p-1].who, fill[p-1].rot if !ok(a[cur][rot][0][0], a[0][0][0][1]) || !ok(a[cur][rot][3][0], a[w][r][1][0]) || a[0][0][1][0]&1 == 0 && a[cur][rot][0][0]&1 == 0 && a[w][r][0][1]&1 == 0 { continue } case 4: w, r := fill[p-1].who, fill[p-1].rot w1, r1 := fill[1].who, fill[1].rot if !ok(a[cur][rot][0][0], a[0][0][3][0]) || !ok(a[cur][rot][3][0], a[w][r][1][0]) || !ok(a[cur][rot][1][0], a[w1][r1][3][0]) || a[0][0][3][0]&1 == 0 && a[cur][rot][0][0]&1 == 0 && a[w][r][0][1]&1 == 0 || a[0][0][2][0]&1 == 0 && a[cur][rot][0][1]&1 == 0 && a[w1][r1][0][0]&1 == 0 { continue } default: w1, r1 := fill[1].who, fill[1].rot w2, r2 := fill[2].who, fill[2].rot w3, r3 := fill[3].who, fill[3].rot w4, r4 := fill[4].who, fill[4].rot if !ok(a[cur][rot][0][0], a[w1][r1][2][0]) || !ok(a[cur][rot][1][0], a[w2][r2][2][0]) || !ok(a[cur][rot][2][1], a[w3][r3][2][0]) || !ok(a[cur][rot][3][1], a[w4][r4][2][0]) || a[cur][rot][0][1]&1 == 0 && a[w1][r1][2][1]&1 == 0 && a[w2][r2][2][0]&1 == 0 || a[cur][rot][1][1]&1 == 0 && a[w2][r2][2][1]&1 == 0 && a[w3][r3][2][0]&1 == 0 || a[cur][rot][2][0]&1 == 0 && a[w3][r3][2][1]&1 == 0 && a[w4][r4][2][0]&1 == 0 || a[cur][rot][0][0]&1 == 0 && a[w4][r4][2][1]&1 == 0 && a[w1][r1][2][0]&1 == 0 { continue } } fill[p] = pair{cur, rot} if dfs(p + 1) { return true } } vis ^= 1 << cur } return false } return dfs(1) }
|