Cod sursa(job #3135236)

Utilizator profinfo114Prof Info profinfo114 Data 2 iunie 2023 14:00:16
Problema Zone Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.87 kb
#include <fstream>
#include <algorithm>

using namespace std;

ifstream fin("zone.in");
ofstream fout("zone.out");

int n, v[515][515];
long long int sp[515][515], a[12];

long long int suma(int is, int js, int ij, int jj) {
    return sp[ij][jj] - sp[is - 1][jj] - sp[ij][js - 1] + sp[is - 1][js - 1];
}

inline bool valid(int i, int j, int ii, int jj) {
    long long int temp[12];
    temp[1] = suma(1, 1, i, j);
    temp[2] = suma(1, j + 1, i, jj);
    temp[3] = suma(1, jj + 1, i, n);
    temp[4] = suma(i + 1, 1, ii, j);
    temp[5] = suma(i + 1, j + 1, ii, jj);
    temp[6] = suma(i + 1, jj + 1, ii, n);
    temp[7] = suma(ii + 1, 1, n, j);
    temp[8] = suma(ii + 1, j + 1, n, jj);
    temp[9] = suma(ii + 1, jj + 1, n, n);
    sort(temp + 1, temp + 10);
    for(int i = 1; i <= 9; i++) {
        if(a[i] != temp[i]) {
            return false;
        }
    }
    return true;
}

int main() {
    fin >> n;
    for(int i = 1; i <= 9; i++) {
        fin >> a[i];
    }
    sort(a + 1, a + 10);
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= n; j++) {
            fin >> v[i][j];
            sp[i][j] = sp[i - 1][j] + sp[i][j - 1] - sp[i - 1][j - 1] + 1LL * v[i][j];
        }
    }
    for(int i = 1; i < n - 1; i++) {
        for(int j = 1; j < n - 1; j++) {
            if(binary_search(a + 1, a + 10, sp[i][j])) {
                for(int k = i + 1; k < n; k++) {
                    if(!binary_search(a + 1, a + 10, suma(i + 1, 1, k, j))) {
                        continue;
                    }
                    for(int l = j + 1; l < n; l++) {
                        if(valid(i, j, k, l)) {
                            fout << i << " " << k << " " << j << " " << l;
                            return 0;
                        }
                    }
                }
            }
        }
    }
    return 0;
}