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
| #include <iostream> #include <cstring> #include <cstdlib> #include <string> #include <cstdio>
#define FILE_IN(__fname) freopen(__fname, "r", stdin) #define FILE_OUT(__fname) freopen(__fname, "w", stdout) #define IMPROVE_IO() std::ios::sync_with_stdio(false) #define DDBUG(x,y) std::cerr << #x << " = " << x << y; #define MDBUG(comment) std::cerr << comment; #define getNum(x) ((ans[x])) #define giveNum(x,y) ((ans[x] = y))
using std::cin; using std::cout; using std::endl;
const int MAXN = 26 + 10;
int n, ans[MAXN], col[3 + 2][MAXN]; bool vis[MAXN]; std::string ol[3];
void Init() { for (int i = 0; i < 3; ++i) { for (int j = 0; j < n; ++j) { col[i + 1][j + 1] = ol[i][j] - 'A' + 1; } } }
namespace Checks { bool check1() { for (int i = 1; i <= n; ++i) if (ans[i] == -1) return true; return false; } bool check2() { int nextBit = 0; for (int i = n; i >= 1; --i) { int A = getNum(col[1][i]); int B = getNum(col[2][i]); int C = getNum(col[3][i]); if ((A + B + nextBit) % n != C) return true; nextBit = (bool) ((A + B + nextBit) >= n); } return false; } bool check3() { if (getNum(col[1][1]) + getNum(col[2][1]) >= n) return true; for (int i = n; i >= 1; --i) { int A = getNum(col[1][i]); int B = getNum(col[2][i]); int C = getNum(col[3][i]); if (A == -1 || B == -1 || C == -1) continue; if ((A + B) % n != C && (A + B + 1) % n != C) return true; } return false; } }
void DFS(int column = n, int line = 1, int nextBit = 0) { if (Checks::check3()) return; if (!Checks::check1()) { if (!Checks::check2()) { for (int i = 1; i <= n; ++i) cout << ans[i] << ' '; cout << endl; exit(0); } return; } if (getNum(col[line][column]) == -1) { for (int i = n - 1; i >= 0; --i) { if (vis[i]) continue; if (line == 3) { int A = getNum(col[1][column]); int B = getNum(col[2][column]); int C = A + B + nextBit; if (C % n != i) continue; vis[i] = true; giveNum(col[line][column], i); DFS(column - 1, 1, (bool) (C >= n)); vis[i] = false; giveNum(col[line][column], -1); } else { vis[i] = true; giveNum(col[line][column], i); DFS(column, line + 1, nextBit); vis[i] = false; giveNum(col[line][column], -1); } } } else { if (line != 3) DFS(column, line + 1, nextBit); else { int A = getNum(col[1][column]); int B = getNum(col[2][column]); int C = A + B + nextBit; DFS(column - 1, 1, (bool) (C >= n)); } } }
int main() { IMPROVE_IO(); cin >> n; cin >> ol[0]; cin >> ol[1]; cin >> ol[2]; Init(); memset(ans, -1, sizeof ans); DFS(); return 0; }
|