Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ///....DH....///
- #include <bits/stdc++.h>
- #define task "ALADDIN"
- #define fi(a) freopen(a, "r", stdin)
- #define fo(a) freopen(a, "w", stdout)
- #define pii pair<int, int>
- #define ft first
- #define sd second
- #define mp make_pair
- #define pb push_back
- #define pf push_front
- #define popb pop_back
- #define popf pop_front
- #define reset(a, x) memset(a, x, sizeof(a))
- #define For(i, s, n, m) for(int i = s; i <= n; i += m)
- #define Ford(i, s, n, m) for(int i = s; i >= n; i -= m)
- using namespace std;
- template <typename R, typename D> inline void Min(R &a, D b) {
- if(a>b)
- a=b;
- }
- template <typename D, typename R> inline void Max(D &a, R b) {
- if(a<b)
- a=b;
- }
- /** MOD **/ const long long mod = 1e9 + 7;
- /** size of array **/ const int maxn = 205;
- int a[maxn][maxn], b[maxn][maxn], n;
- bool ok;
- void backtrack(int x) {
- if(x > n) {
- for(int i = 1; i <= n; i++) {
- for(int j = 1; j <= n; j++)
- cout << a[i][j] << " ";
- cout << "\n";
- }
- ok = true;
- return ;
- }
- for(int u = 0; u <= 1; u++)
- for(int v = 0; v <= 1; v++) {
- bool ne = true;
- a[1][x] = u;
- a[x][1] = v;
- for(int i = 2; i < x; i++) {
- a[x][i] = b[x - 1][i - 1] - a[x - 1][i - 1] - a[x][i - 1] - a[x - 1][i];
- if(a[x][i] < 0 || a[x][i] > 1) {
- ne = false;
- a[x][i] = 0;
- break ;
- }
- }
- if(ne == true) {
- for(int i = 2; i <= x; i++) {
- a[i][x] = b[i - 1][x - 1] - a[i - 1][x - 1] - a[i][x - 1] - a[i - 1][x];
- if(a[i][x] < 0 || a[i][x] > 1) {
- ne = false;
- a[i][x] = 0;
- break ;
- }
- }
- }
- if(ne == true)
- backtrack(x + 1);
- if(ok == true)
- return ;
- }
- }
- main() {
- ios_base::sync_with_stdio(false);
- cin.tie(0), cout.tie(0);
- cin >> n;
- for(int i = 1; i < n; i++)
- for(int j = 1; j < n; j++)
- cin >> b[i][j];
- backtrack(2);
- if(ok == false) {
- memset(a, 0, sizeof(a));
- a[1][1] = 1;
- backtrack(2);
- }
- if(ok == false)
- cout << "No solution";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement