Pagini recente » Cod sursa (job #3222060) | Cod sursa (job #1448622) | Cod sursa (job #595373) | Cod sursa (job #235662) | Cod sursa (job #542526)
Cod sursa(job #542526)
#include<algorithm>
using namespace std;
#define DIM 105
#define DIMbuff 1234
int n,a[DIM][DIM],b[DIM][DIM],c[DIM][DIM][5],rez,poz;
char buff[DIMbuff];
bool bst[DIM][DIM];
inline void pars (int &nr)
{
nr=0;
while(buff[poz]<'0' || buff[poz]>'9')
if(++poz==DIMbuff)
fread(buff,1,DIMbuff,stdin),poz=0;
while(buff[poz]>='0' && buff[poz]<='9')
{
nr=nr*10+buff[poz]-'0';
if(++poz==DIMbuff)
fread(buff,1,DIMbuff,stdin),poz=0;
}
}
inline int ct (int x,int y,int nr)
{
if(nr==1)
{
if(bst[x][y]!=bst[x-1][y])
return c[x][y][1];
return 0;
}
else
{
if(bst[x][y]!=bst[x][y-1])
return c[x][y][4];
return 0;
}
}
void back (int k,int sol)
{
if(k==n*n+1)
{
rez=max(sol,rez);
}
else
{
int i=(k-1)/n+1;
int j=(k-1)%n+1;
bst[i][j]=0;
back(k+1,sol+a[i][j]-ct(i,j,1)-ct(i,j,4));
bst[i][j]=1;
back(k+1,sol+b[i][j]-ct(i,j,1)-ct(i,j,4));
}
}
int main ()
{
freopen("pixels.in","r",stdin);
freopen("pixels.out","w",stdout);
int i,j,nr1,nr2;
pars(n);
for(i=1;i<=n;++i)
for(j=1;j<=n;++j)
pars(a[i][j]);
for(i=1;i<=n;++i)
for(j=1;j<=n;++j)
pars(b[i][j]);
for(i=1;i<=n*n;++i)
{
nr1=(i-1)/n+1;
nr2=(i-1)%n+1;
pars(c[nr1][nr2][1]);
pars(c[nr1][nr2][2]);
pars(c[nr1][nr2][3]);
pars(c[nr1][nr2][4]);
}
back (1,0);
printf("%d\n",rez);
return 0;
}