fork(1) download
  1. #include <bits/stdc++.h>
  2. #define x first
  3. #define y second
  4. #define n 16
  5.  
  6. using namespace std;
  7.  
  8. bool h[18]['Z'], c[18]['Z'], o[6][6]['Z'];
  9. char a[18][18];
  10.  
  11. int k;
  12. typedef pair<int, int> ii;
  13. ii bd;
  14. bool ok;
  15.  
  16. void back(int x, int y) {
  17. if (ok) return;
  18. if (x == 16) { ok = true; return; }
  19. if (a[x][y] != '-') {
  20. if (y + 1 == n) back(x + 1, 0); else back(x, y + 1);
  21. return;
  22. }
  23. for (char i = 'A'; i <= 'P'; ++ i) {
  24. if (ok || h[x][i] || c[y][i] || o[x % 4][y % 4][i]) continue;
  25. a[x][y] = i;
  26. h[x][i] = true;
  27. c[y][i] = true;
  28. o[x % 4][y % 4][i] = true;
  29. if (y + 1 == n) back(x + 1, 0); else back(x, y + 1);
  30. a[x][y] = '-';
  31. h[x][i] = false;
  32. c[y][i] = false;
  33. o[x % 4][y % 4][i] = false;
  34. }
  35. }
  36.  
  37. int main() {
  38. // freopen("file.inp", "r", stdin);
  39. cin >> k;
  40. while (k > 0) {
  41. -- k;
  42. memset(h, false, sizeof(h));
  43. memset(c, false, sizeof(c));
  44. memset(o, false, sizeof(o));
  45. ok = false;
  46. for (int i = 0; i < n; ++ i)
  47. for (int j = 0; j < n; ++ j) {
  48. cin >> a[i][j];
  49. if (a[i][j] == '-') continue;
  50. h[i][a[i][j]] = true;
  51. c[j][a[i][j]] = true;
  52. o[i % 4][j % 4][a[i][j]] = true;
  53. }
  54. back(0, 0);
  55. for (int i = 0; i < n; ++ i) {
  56. for (int j = 0; j < n; ++ j) cout << a[i][j];
  57. cout << endl;
  58. }
  59. }
  60. return(0);
  61. }
Success #stdin #stdout 0s 15248KB
stdin
Standard input is empty
stdout
Standard output is empty