<Problem>
https://oj.vnoi.info/problem/aladdin
#include <iostream>
using namespace std;
int n;
int c[200][200];
int blackBoard[300][300];
bool found = false;
void showArray(int arr[][300]) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cout << arr[i][j] << " ";
}
cout << endl;
}
}
bool inMatrix(int x, int y) {
if ((x >= 1 && x <= n)
&& (y >= 1 && y <= n)) {
return true;
}
else {
return false;
}
}
bool check(int x, int y, int rangeMin, int rangeMax) {
if (!inMatrix(x, y)) return true;
if (x == n || y == n) return true;
int sumSubRange = blackBoard[x][y] + blackBoard[x][y + 1]
+ blackBoard[x + 1][y] + blackBoard[x + 1][y + 1];
int l = c[x][y] - sumSubRange;
return (l >= rangeMin && l <= rangeMax);
}
void Alad(int p, int q, int i, int j) {
//cout << p << " " << q << " " << i << " " << j << endl;
//showArray(blackBoard);
//cout << endl;
if (found) return;
if (p > n) {
//cout << "END" << endl;
showArray(blackBoard);
found = true;
return;
}
for (int u = 0; u <= 1; u++) {
blackBoard[i][j] = u;
if (!check(i - 1, j - 1, 0, 0)) continue;
if (!check(i - 1, j, 0, 1)) continue;
if (!check(i, j - 1, 0, 2)) continue;
if (inMatrix(i + 1, j - 1)) {
Alad(p, q, i + 1, j - 1);
//blackBoard[i][j] = 0;
}
else {
if (q < n) Alad(p, q + 1, p, q + 1);
else Alad(p + 1, q, p + 1, q);
//blackBoard[i][j] = 0;
}
//blackBoard[i][j] = 0;
}
blackBoard[i][j] = 0;
}
int main() {
cin >> n;
for (int i = 1; i <= n - 1; i++) {
for (int j = 1; j <= n - 1; j++) {
cin >> c[i][j];
}
}
Alad(1, 1, 1, 1);
if (!found) cout << "No solution" << endl;
return 0;
}