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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
| class Rectangle{ public: typedef pair<int, int> Point; Rectangle(int sx, int sy) : dx({1, 0}), dy({0, 1}), vec({Point{0, 0}, Point{0, sy - 1}, Point{sx - 1, 0}, Point{sx - 1, sy - 1}}){} void mirror(int dr = 1){ if(dr == 1){ swap(vec[0], vec[1]); swap(vec[2], vec[3]); }else{ swap(vec[0], vec[2]); swap(vec[1], vec[3]); } update(); } void transpose(int dr = 1){ if(dr == 1){ swap(vec[1], vec[2]); }else{ swap(vec[0], vec[3]); } update(); } void rotate(int dr = 1){ transpose(); if(dr == 1){ mirror(1); }else{ mirror(0); } update(); } Point mapping(Point p){ return {vec[0].first + dx.first * p.first + dy.first * p.second, vec[0].second + dx.second * p.first + dy.second * p.second}; } Point dx, dy; array<Point, 4> vec; private: void update(){ int xlim = abs(vec[2].first - vec[0].first) + abs(vec[2].second - vec[0].second); int ylim = abs(vec[1].first - vec[0].first) + abs(vec[1].second - vec[0].second); assert(xlim > 0 and ylim > 0); dx = Point{(vec[2].first - vec[0].first) / xlim, (vec[2].second - vec[0].second) / xlim}; dy = Point{(vec[1].first - vec[0].first) / ylim, (vec[1].second - vec[0].second) / ylim}; }
}; class Solution { public: bool composeCube(vector<vector<string>>& shapes) { int sz = shapes[0].size(); int cnt = sz * sz * 2 + (sz - 1) * (sz - 2) * 4; for(auto& vec: shapes) { for(auto& row: vec){ for(auto& c: row){ if(c == '1') --cnt; } } } if(cnt != 0) return false; typedef Rectangle::Point Point; Rectangle rec(sz, sz); vector<Rectangle> recd; for(int i = 0; i < 4; ++i){ recd.push_back(rec); rec.rotate(0); } vector<Rectangle> recs(6, Rectangle{sz, sz}); auto&& isCon = [&](int x, int y, int sx, int sy){ for(int i = 0; i < sz; ++i){ auto px = recd[sx].mapping({0, i}); auto py = recd[sy].mapping({0, sz - 1 - i}); auto ppx = recs[x].mapping(px); auto ppy = recs[y].mapping(py); if(i == 0 or i == sz - 1){ if(shapes[x][ppx.first][ppx.second] == shapes[y][ppy.first][ppy.second] and shapes[y][ppy.first][ppy.second] == '1'){ return false; } }else if(shapes[x][ppx.first][ppx.second] == shapes[y][ppy.first][ppy.second]){ return false; } } return true; }; auto&& show = [&](int i, int x, int y){ return recs[i].mapping({x, y}); }; vector<int> idx(6); vector<vector<int>> dr = {{-1, 0, -1, -1}, {1, 2, 3, 4}, {5, -1, -1, -1}}; auto&& print = [&](int q){ for(int i = 0; i < 3; ++i){ for(int j = 0; j < sz; ++j){ for(int k = 0; k < 4; ++k){ for(int l = 0; l < sz; ++l){ if(dr[i][k] < 0) cout << " "; else if(dr[i][k] > q) cout << "?"; else{ auto rt = show(idx[dr[i][k]], j, l); cout << shapes[idx[dr[i][k]]][rt.first][rt.second]; } } cout << " "; } cout << endl; } cout << endl; } }; auto&& check = [&](int sz){
bool ok = true; for(int i = 0; i < 4 and i + 1 <= sz and ok; ++i){ if(!isCon(idx[0], idx[i + 1], 3 - i, 0)){ ok = false; } } for(int i = 0; i < 4 and i + 1 <= sz and (i + 1) % 4 + 1 <= sz and ok; ++i){ if(!isCon(idx[i + 1], idx[(i + 1) % 4 + 1], 1, 3)) ok = false; } if(sz == 5){ for(int i = 0; i < 4 and ok; ++i){ if(!isCon(idx[5], idx[i + 1], i, 2)) ok = false; } }
return ok; }; idx[0] = 0; vector<int> vis(6, 0); vis[0] = 1; auto&& dfs = [&](auto&& self, int cur) -> bool{ if(cur >= 6) { return true; } for(int i = 1; i < 6; ++i){ if(vis[i] == 0){ idx[cur] = i; vis[i] = 1; for(int m = 0; m < 2; ++m){ for(int r = 0; r < 4; ++r){ if(check(cur)) { if(self(self, cur + 1)) return true; } recs[i].rotate(); } recs[i].mirror(); } vis[i] = 0; } } return false; }; if(dfs(dfs, 1)) return true; recs[0].mirror(); if(dfs(dfs, 1)) return true; return false; } };
|