Cod sursa(job #999893)

Utilizator AlexandruValeanuAlexandru Valeanu AlexandruValeanu Data 21 septembrie 2013 17:18:15
Problema Plantatie Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.94 kb
#include <iostream>
#include <fstream>

using namespace std;

const int Nmax = 502;

int D[Nmax][Nmax][Nmax];

int N, M;

void precalc()
{
    for ( int i = 1; i <= N; ++i )
            for ( int j = 1; j <= N; ++j )
                    for ( int k = 2; k <= N; ++k )
                    {
                        int max1 = max( D[i][j][k - 1], D[i][j + 1][k - 1] );
                        int max2 = max( D[i + 1][j][k - 1], D[i + 1][j + 1][k - 1] );

                        D[i][j][k] = max( max1, max2 );
                    }
}

int main()
{
    ifstream f("plantatie.in");
    ofstream g("plantatie.out");

    f >> N >> M;

    for ( int i = 1; i <= N; ++i )
            for ( int j = 1; j <= N; ++j )
                    f >> D[i][j][1];

    precalc();

    for ( int i = 1, a, b, c; i <= M; ++i )
    {
        f >> a >> b >> c;

        g << D[a + c - 1][b + c - 1][1] << "\n";
    }

    return 0;
}