Cod sursa(job #1112686)

Utilizator paunmatei7FMI Paun Matei paunmatei7 Data 19 februarie 2014 22:45:55
Problema Zone Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.1 kb
#include <cstdio>
#include <algorithm>

#define NMAX 517
#define LL long long

using namespace std;

LL Sol[17], a[NMAX][NMAX], Sum[17];
LL n;

inline bool verif(LL a){
    for(LL i = 1; i <= 9; ++i)
        if(Sol[i] == a)
            return 1;
    return 0;
}

int main(){
    freopen("zone.in", "r", stdin);
    freopen("zone.out", "w", stdout);
    scanf("%lld", &n);
    for(LL i = 1; i <= 9; ++i)
        scanf("%lld", &Sol[i]);
    sort(Sol + 1, Sol + 10);
    for(LL i = 1; i <= n; ++i)
        for(LL j = 1; j <= n; ++j){
            scanf("%lld", &a[i][j]);
            a[i][j] = a[i][j - 1] + a[i - 1][j] + a[i][j] - a[i - 1][j - 1];
        }
    for(LL l1 = 1; l1 <= n; ++l1)
        for(LL c1 = 1; c1 <= n; ++c1)
            if(verif(a[l1][c1]) == 1)
                for(LL l2 = 1; l2 <= n; ++l2)
                    if(verif(a[l2][c1] - a[l1][c1]) == 1)
                        for(LL c2 = 1; c2 <= n; ++c2){
                            Sum[1] = a[l1][c1];
                            Sum[2] = a[l1][c2] - Sum[1];
                            Sum[3] = a[l1][n] - Sum[2] - Sum[1];
                            Sum[4] = a[l2][c1] - Sum[1];
                            Sum[5] = a[l2][c2] - Sum[4] - Sum[2] - Sum[1];
                            Sum[6] = a[l2][n] - Sum[5] - Sum[3] - Sum[2] - Sum[1] - Sum[4];
                            Sum[7] = a[n][c1] - Sum[4] - Sum[1];
                            Sum[8] = a[n][c2] - Sum[7] - Sum[5] - Sum[4] - Sum[1] - Sum[2];
                            Sum[9] = a[n][n] - Sum[8] - Sum[3] - Sum[7] - Sum[6] - Sum[5] - Sum[4] - Sum[1] - Sum[2];
                            sort(Sum + 1, Sum + 10);
                            bool ok = 0;
                            for(LL i = 1; i <= 9; ++i)
                                if(Sum[i] != Sol[i])
                                    ok = 1;
                            if(ok == 0){
                                printf("%lld %lld %lld %lld\n", l1, l2, c1, c2);
                                return 0;
                            }
                        }
    printf("-1");
    return 0;
}