p125.ans1
======================
1 2 3
1 4 5
1 6 7

=================
p125.cpp
======================
#include <bits/stdc++.h>

using namespace std;

template<typename T1, typename T2>
ostream& operator<<(ostream& out, const pair<T1, T2>& x) {
    return out << x.first << ' ' << x.second;
}

template<typename T1, typename T2>
istream& operator>>(istream& in, pair<T1, T2>& x) {
    return in >> x.first >> x.second;
}

template<typename T>
istream& operator>>(istream& in, vector<T>& a) {
    for(auto& x: a) {
        in >> x;
    }
    return in;
};

template<typename T>
ostream& operator<<(ostream& out, const vector<T>& a) {
    for(auto x: a) {
        out << x << ' ';
    }
    return out;
};

int n;
vector<vector<int>> a, b, b2;

void read() {
    cin >> n;
    b.resize(n, vector<int>(n));
    a.resize(n, vector<int>(n));
    cin >> b;
    b2 = b;
}

bool change(int i, int j, int delta) {
    bool fail = false;
    if(i > 0) {
        b2[i][j] += delta * (a[i - 1][j] > a[i][j]);
        b2[i - 1][j] += delta * (a[i - 1][j] < a[i][j]);
        if(b2[i][j] < 0 || b2[i - 1][j] < 0) {
            fail = true;
        }
    }

    if(j > 0) {
        b2[i][j] += delta * (a[i][j - 1] > a[i][j]);
        b2[i][j - 1] += delta * (a[i][j - 1] < a[i][j]);
        if(b2[i][j] < 0 || b2[i][j - 1] < 0) {
            fail = true;
        }
    }
    return fail;
}

void backtrack(int pos) {
    if(pos == n * n) {
        for(int j = 0; j < n; j++) {
            if(b2[n - 1][j] != 0) {
                return;
            }
        }

        for(int i = 0; i < n; i++) {
            for(int j = 0; j < n; j++) {
                cout << a[i][j] << " \n"[j == n - 1];
            }
        }
        exit(0);
    }

    int i = pos / n, j = pos % n;
    for(int val = 0; val <= 9; val++) {
        a[i][j] = val;
        if(!change(i, j, -1) && (i == 0 || b2[i - 1][j] == 0)) {
            backtrack(pos + 1);
        }
        change(i, j, 1);
    }
}

void solve() {
    /*
     * Reconstruct the army matrix A by backtracking over cells in row-major
     * order, trying every digit 0..9 per cell.
     *
     * b2 is a working copy of the target neighbour-counts B that is decremented
     * as constraints get satisfied: change(i, j, delta) adjusts the counts of
     * the current cell and its already-placed up/left neighbours according to
     * which side holds the larger army. After filling a cell we require its
     * upper neighbour's residual count to be exactly zero (it can no longer
     * gain neighbours), pruning impossible branches. A full assignment whose
     * last row also has all-zero residuals is printed and the program exits.
     */

    backtrack(0);
    cout << "NO SOLUTION" << '\n';
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int T = 1;
    // cin >> T;
    for(int test = 1; test <= T; test++) {
        read();
        // cout << "Case #" << test << ": ";
        solve();
    }

    return 0;
}

=================
p125.in1
======================
3
1 2 1
1 2 1
1 1 0

=================
statement.txt
======================
125. Shtirlits

time limit per test: 0.25 sec.
memory limit per test: 4096 KB



There is a checkered field of size N x N cells (1 Ј N Ј 3). Each cell designates the territory of a state (i.e. N2 states). Each state has an army. Let A [i, j] be the number of soldiers in the state which is located on i-th line and on j-th column of the checkered field (1£i£N, 1£j£N, 0 £  A[i, j] £  9). For each state the number of neighbors, B [i, j], that have a larger army, is known. The states are neighbors if they have a common border (i.e. 0 £  B[i, j]  £  4). Shtirlits knows matrix B. He has to determine the number of armies for all states (i.e. to find matrix A) using this information for placing forces before the war. If there are more than one solution you may output any of them.



Input

The first line contains a natural number N. Following N lines contain the description of matrix B - N numbers in each line delimited by spaces.


Output

If a solution exists, the output file should contain N lines, which describe matrix A. Each line will contain N numbers delimited by spaces. If there is no solution, the file should contain NO SOLUTION.


Sample Input

3
1 2 1
1 2 1
1 1 0
Sample Output

1 2 3
1 4 5
1 6 7
Author	: Victor G. Samoilov, Herman "Smash" Narkaytis
Resource	: 5th Southern Subregional Contest. Saratov 2002
Date	: 2002-10-10

=================
