Cod sursa(job #18332)

Utilizator raula_sanChis Raoul raula_san Data 18 februarie 2007 11:34:24
Problema Plantatie Scor 60
Compilator cpp Status done
Runda preONI 2007, Runda 2, Clasa a 10-a Marime 1.11 kb
#include <cstdio>

#define dim 1 << 9

int N; long M, A[dim][dim], B[dim][dim];

void Read();
long Solve( int, int, int );

int main()
{
	Read();
	return 0;
}

void Read()
{
	freopen("plantatie.in", "r", stdin);
	freopen("plantatie.out", "w", stdout);

	scanf("%d %ld", &N, &M);

	int i, j, l; long k, Max;

	for(i=1; i<=N; ++i)
		for(j=1; j<=N; ++j)
		{
			scanf("%ld", &A[i][j]);
			
            Max = B[i-1][j] > B[i][j-1] ? B[i-1][j] : B[i][j-1];
			Max = A[i][j] > Max ? A[i][j] : Max;
			
            B[i][j] = Max;
		}

	for(k=1; k<=M; ++k)
	{
		scanf("%d %d %d", &i, &j, &l);
		
		if( B[i+l-1][j+l-1] != B[i-1][j+l-1] && B[i+l-1][j+l-1] != B[i+l-1][j-1] )
		{
			Max = B[i-1][j+l-1] > B[i+l-1][j-1] ? B[i-1][j+l-1] : B[i+l-1][j-1];
			Max = B[i+l-1][j+l-1] > Max ? B[i+l-1][j+l-1] : Max;
			
			printf("%ld\n", Max);
		}
		else
			printf("%ld\n", Solve(i,j,l));
	}

	fclose(stdin); fclose(stdout);
}

long Solve( int a, int b, int l )
{
	int i, j; long Max = -1;

	for(i=a; i<=a+l-1; ++i)
		for(j=b; j<=b+l-1; ++j)
			if( A[i][j] > Max ) Max = A[i][j];

	return Max;
}