Cod sursa(job #542410)

Utilizator andunhillMacarescu Sebastian andunhill Data 26 februarie 2011 13:02:36
Problema Pixels Scor 0
Compilator cpp Status done
Runda Romanian Master in Mathematics and Sciences 2011, Ziua 2 Marime 1.62 kb
#include<fstream>
using namespace std;
ifstream f("pixels.in");
ofstream g("pixels.out");
struct dif 
{	int sus,dr,jos,st;
};
int N;
int A[101][101];
int B[101][101];
dif a[101][101];
char mat[101][101];
void read(int N,int a[101][101])
{	int i,j;
	for(i=1;i<=N;i++)
		for(j=1;j<=N;j++)
			f>>a[i][j];
}
int maxx(int a,int b,int c,int d)
{	int m;
	m=max(a,max(b,max(c,d)));
	return m;
}
int main()
{	int i,j,alb,neg,aa,bb;
	int sa,sn,sd; sa=sn=sd=0; int ma,mn;
	f>>N;
	read(N,A);
	read(N,B);
	for(i=1;i<=N;i++)
		for(j=1;j<=N;j++)
			f>>a[i][j].sus>>a[i][j].dr>>a[i][j].jos>>a[i][j].st;
	for(i=1;i<=N;i++)
		for(j=1;j<=N;j++)
		{	alb=maxx(A[i][j]+B[i][j+1]+B[i+1][j]-a[i][j].dr-a[i][j].jos,
					A[i][j]+B[i][j+1]+A[i+1][j]-a[i][j].dr,
					A[i][j]+A[i][j+1]+B[i+1][j]-a[i][j].jos,
					A[i][j]+A[i][j+1]+A[i+1][j]);
			neg=maxx(B[i][j]+A[i][j+1]+A[i+1][j]-a[i][j].dr-a[i][j].jos,
					B[i][j]+A[i][j+1]+B[i+1][j]-a[i][j].dr,
					B[i][j]+B[i][j+1]+A[i+1][j]-a[i][j].jos,
					B[i][j]+B[i][j+1]+B[i+1][j]);
			aa=A[i][j]+(mat[i][j-1]=='A'?A[i][j-1]:B[i][j-1]-a[i][j].st) + (mat[i-1][j]=='A'?A[i-1][j]:B[i-1][j]-a[i][j].sus);
			bb=B[i][j]+(mat[i][j-1]=='B'?B[i][j-1]:A[i][j-1]-a[i][j].st) + (mat[i-1][j]=='B'?B[i-1][j]:A[i-1][j]-a[i-1][j].sus);
			ma=max(aa,alb);
			mn=max(bb,neg);
			if(ma<=mn)
				mat[i][j]='N';
			else mat[i][j]='A';
			
		}
	for(i=1;i<=N;i++)
		for(j=1;j<=N;j++)
		{	if(mat[i][j]=='A')
				sa+=A[i][j];
			else sn+=B[i][j];
			if(mat[i][j]!=mat[i-1][j]) sd+=a[i][j].sus;
			if(mat[i][j]!=mat[i][j+1]) sd+=a[i][j].dr;
		}
	g<<sa+sn-sd;
	f.close();
	g.close();
	return 0;
}