|
| 1 | +/* |
| 2 | +ID: nathan.18 |
| 3 | +TASK: twofive |
| 4 | +LANG: C++ |
| 5 | +*/ |
| 6 | + |
| 7 | +#include <iostream> |
| 8 | +#include <string> |
| 9 | +#include <utility> |
| 10 | +#include <sstream> |
| 11 | +#include <algorithm> |
| 12 | +#include <stack> |
| 13 | +#include <vector> |
| 14 | +#include <queue> |
| 15 | +#include <map> |
| 16 | +#include <set> |
| 17 | +#include <bitset> |
| 18 | +#include <cmath> |
| 19 | +#include <cstring> |
| 20 | +#include <iomanip> |
| 21 | +#include <fstream> |
| 22 | +#include <cassert> |
| 23 | +#include <unordered_set> |
| 24 | +#include <ctime> |
| 25 | +#include <list> |
| 26 | + |
| 27 | +using namespace std; |
| 28 | + |
| 29 | +template<class T> using min_heap = priority_queue<T, vector<T>, greater<T>>; |
| 30 | + |
| 31 | +#define FOR(i, a, b) for (int i=a; i<(b); i++) |
| 32 | +#define F0R(i, a) for (int i=0; i<(a); i++) |
| 33 | +#define F0R1(i, a) for (int i=1; i<=(a); i++) |
| 34 | +#define FORd(i, a, b) for (int i = (b)-1; i >= a; i--) |
| 35 | +#define F0Rd(i, a) for (int i = (a)-1; i >= 0; i--) |
| 36 | +#define F0Rd1(i, a) for (int i=a; i>0; i--) |
| 37 | +#define SORT(vec) sort(vec.begin(), vec.end()) |
| 38 | +#define MIN(a, b) a = min(a, b) |
| 39 | +#define MAX(a, b) a = max(a, b) |
| 40 | + |
| 41 | +#define INF 1000000010 |
| 42 | +#define LL_INF 4500000000000000000 |
| 43 | +#define LSOne(S) (S & (-S)) |
| 44 | +#define EPS 1e-9 |
| 45 | +#define pA first |
| 46 | +#define pB second |
| 47 | +#define mp make_pair |
| 48 | +#define pb push_back |
| 49 | +#define PI acos(-1.0) |
| 50 | +#define ll long long |
| 51 | +#define MOD (int)(2e+9+11) |
| 52 | +#define SET(vec, val, size) for (int i = 0; i < size; i++) vec[i] = val; |
| 53 | +#define SET2D(arr, val, dim1, dim2) F0R(i, dim1) F0R(j, dim2) arr[i][j] = val; |
| 54 | +#define SET3D(arr, val, dim1, dim2, dim3) F0R(i, dim1) F0R(j, dim2) F0R(k, dim3) arr[i][j][k] = val; |
| 55 | +#define SET4D(arr, val, dim1, dim2, dim3, dim4) F0R(i, dim1) F0R(j, dim2) F0R(k, dim3) F0R(l, dim4) arr[i][j][k][l] = val; |
| 56 | +#define READGRID(arr, dim) F0R(i, dim) F0R(j, dim) cin >> arr[i][j]; |
| 57 | +#define all(x) (x).begin(), (x).end() |
| 58 | + |
| 59 | +typedef pair<int, int> ii; |
| 60 | +typedef pair<int, ii> iii; |
| 61 | +typedef pair<ll, ll> pll; |
| 62 | +typedef vector<int> vi; |
| 63 | +typedef vector<ii> vii; |
| 64 | +typedef vector<iii> viii; |
| 65 | +typedef vector<ll> vl; |
| 66 | + |
| 67 | +void setupIO(const string &PROB) { |
| 68 | + ios::sync_with_stdio(false); |
| 69 | + cin.tie(nullptr); |
| 70 | + ifstream infile(PROB + ".in"); |
| 71 | + if (infile.good()) { |
| 72 | + freopen((PROB + ".in").c_str(), "r", stdin); |
| 73 | + freopen((PROB + ".out").c_str(), "w", stdout); |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +/* ============================ */ |
| 78 | + |
| 79 | +int grid[5][5]; |
| 80 | +int memo[6][6][6][6][6]; |
| 81 | +bool used[26]; |
| 82 | + |
| 83 | +int run(int a, int b, int c, int d, int e, int val) { |
| 84 | + if (e == 5) return 1; |
| 85 | + while (val <= 25 && used[val]) val++; |
| 86 | + if (val > 25) return 0; |
| 87 | + |
| 88 | + if (memo[a][b][c][d][e] != -1) return memo[a][b][c][d][e]; |
| 89 | + |
| 90 | + int ans = 0; |
| 91 | + if (a != 5 && (a == 0 || grid[0][a-1] < val)) ans += run(a+1, b, c, d, e, val+1); |
| 92 | + if (b != 5 && b < a && grid[0][b] < val && (b == 0 || grid[1][b - 1] < val)) ans += run(a, b+1, c, d, e, val+1); |
| 93 | + if (c != 5 && c < b && grid[1][c] < val && (c == 0 || grid[2][c - 1] < val)) ans += run(a, b, c+1, d, e, val+1); |
| 94 | + if (d != 5 && d < c && grid[2][d] < val && (d == 0 || grid[3][d - 1] < val)) ans += run(a, b, c, d+1, e, val+1); |
| 95 | + if (e != 5 && e < d && grid[3][e] < val && (e == 0 || grid[4][e - 1] < val)) ans += run(a, b, c, d, e+1, val+1); |
| 96 | + |
| 97 | + /* cout << a << " " << b << " " << c << " " << d << " " << e << " " << val << " " << ans << endl; */ |
| 98 | + |
| 99 | + return memo[a][b][c][d][e] = ans; |
| 100 | +} |
| 101 | + |
| 102 | + |
| 103 | +int main() { |
| 104 | + setupIO("twofive"); |
| 105 | + |
| 106 | + /* SET2D(grid, -1, 5, 5); */ |
| 107 | + /* SET(used, false, 26); */ |
| 108 | + /* F0R(i, 6) F0R(j, 6) F0R(k, 6) F0R(l, 6) F0R(m, 6) memo[i][j][k][l][m] = -1; */ |
| 109 | + /* run(0, 0, 0, 0, 0, 0); */ |
| 110 | + |
| 111 | + /* return 0; */ |
| 112 | + |
| 113 | + char c; cin >> c; |
| 114 | + if (c == 'N') { |
| 115 | + int m; cin >> m; |
| 116 | + SET2D(grid, -1, 5, 5); |
| 117 | + SET(used, false, 26); |
| 118 | + F0R(i, 5) { |
| 119 | + F0R(j, 5) { |
| 120 | + int sum = 0; |
| 121 | + F0R(k, 25) { |
| 122 | + if (used[k]) continue; |
| 123 | + if (j > 0 && grid[i][j - 1] >= k) continue; |
| 124 | + if (i > 0 && grid[i - 1][j] >= k) continue; |
| 125 | + grid[i][j] = k; |
| 126 | + used[k] = true; |
| 127 | + |
| 128 | + F0R(i, 6) F0R(j, 6) F0R(k, 6) F0R(l, 6) F0R(m, 6) memo[i][j][k][l][m] = -1; |
| 129 | + |
| 130 | + int val; |
| 131 | + if (i == 0) val = run(j+1, 0, 0, 0, 0, 0); |
| 132 | + if (i == 1) val = run(5, j+1, 0, 0, 0, 0); |
| 133 | + if (i == 2) val = run(5, 5, j+1, 0, 0, 0); |
| 134 | + if (i == 3) val = run(5, 5, 5, j+1, 0, 0); |
| 135 | + if (i == 4) val = run(5, 5, 5, 5, j+1, 0); |
| 136 | + /* cout << val << endl; */ |
| 137 | + sum += val; |
| 138 | + if (sum >= m) { |
| 139 | + sum -= val; |
| 140 | + m -= sum; |
| 141 | + break; |
| 142 | + } |
| 143 | + used[k] = false; |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + string ans = ""; |
| 149 | + F0R(i, 5) F0R(j, 5) ans += ('A' + grid[i][j]); |
| 150 | + cout << ans << endl; |
| 151 | + } else { |
| 152 | + char realGrid[5][5]; |
| 153 | + F0R(i, 5) F0R(j, 5) cin >> realGrid[i][j]; |
| 154 | + SET2D(grid, -1, 5, 5); |
| 155 | + SET(used, false, 26); |
| 156 | + |
| 157 | + int ans = 0; |
| 158 | + F0R(i, 5) { |
| 159 | + F0R(j, 5) { |
| 160 | + F0R(k, 25) { |
| 161 | + if (used[k]) continue; |
| 162 | + if (j > 0 && grid[i][j - 1] >= k) continue; |
| 163 | + if (i > 0 && grid[i - 1][j] >= k) continue; |
| 164 | + grid[i][j] = k; |
| 165 | + used[k] = true; |
| 166 | + if (k == realGrid[i][j] - 'A') break; |
| 167 | + |
| 168 | + F0R(i, 6) F0R(j, 6) F0R(k, 6) F0R(l, 6) F0R(m, 6) memo[i][j][k][l][m] = -1; |
| 169 | + |
| 170 | + int val; |
| 171 | + if (i == 0) val = run(j+1, 0, 0, 0, 0, 0); |
| 172 | + if (i == 1) val = run(5, j+1, 0, 0, 0, 0); |
| 173 | + if (i == 2) val = run(5, 5, j+1, 0, 0, 0); |
| 174 | + if (i == 3) val = run(5, 5, 5, j+1, 0, 0); |
| 175 | + if (i == 4) val = run(5, 5, 5, 5, j+1, 0); |
| 176 | + |
| 177 | + ans += val; |
| 178 | + |
| 179 | + used[k] = false; |
| 180 | + } |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + ans++; |
| 185 | + cout << ans << endl; |
| 186 | + } |
| 187 | + |
| 188 | + return 0; |
| 189 | +} |
0 commit comments