Pagini recente » Cod sursa (job #392112) | Cod sursa (job #384538) | Cod sursa (job #1754868) | Cod sursa (job #3265784) | Cod sursa (job #542433)
Cod sursa(job #542433)
#include <cstdio>
//consts
const int NMAX=128;
const int KMAX=4;
const int vx[4]={0,0,1,-1}, vy[4]={1,-1,0,0};
//global
int n,nn,sol;
struct {int white,black,cost1,cost2,cost3,cost4; bool b;} v[NMAX][NMAX];
//functions
inline void print_matrix(){
int i,j;
for(i=0;i<n;++i){
for(j=0;j<n;++j)
printf("%d ",v[i][j].b);
printf("\n");
}
printf("\n");
}
inline bool inbound(int x){
if (x<0)
return false;
if (x>=n)
return false;
return true;
}
inline void count(){
int i,j,local=0;
for(i=0;i<n;++i)
for(j=0;j<n;++j){
if(v[i][j].b)
local+=v[i][j].black;
else
local+=v[i][j].white;
}
for(i=0;i<n;++i)
for(j=0;j<n;++j){
if (inbound(i+vx[0]) && inbound(j+vy[0]) && v[i][j].b!=v[i+vx[0]][j+vy[0]].b){
local-=v[i][j].cost1;
}
if (inbound(i+vx[1]) && inbound(j+vy[1]) && v[i][j].b!=v[i+vx[1]][j+vy[1]].b){
local-=v[i][j].cost2;
}
if (inbound(i+vx[2]) && inbound(j+vy[2]) && v[i][j].b!=v[i+vx[2]][j+vy[2]].b){
local-=v[i][j].cost3;
}
if (inbound(i+vx[3]) && inbound(j+vy[3]) && v[i][j].b!=v[i+vx[3]][j+vy[3]].b){
local-=v[i][j].cost4;
}
}
if (local>sol) sol=local;
return;
}
inline void back (int a, int b){
if(a==n && b==n){
count();
return;
}
if (b==n){
back(a+1,0);
return;
}
if (a==n)
return;
v[a][b].b=true;
count();
back(a,b+1);
v[a][b].b=false;
count();
back(a,b+1);
return;
}
int main(){
freopen("pixels.in","r",stdin);
//freopen("pixels.out","w",stdout);
//vars
int i,j;//cycle
//read
scanf("%d",&n); nn=n*n;
for(i=0;i<n;++i)
for(j=0;j<n;++j)
scanf("%d%d",&v[i][j].white,&v[i][j].black);
for(i=0;i<n;++i)
for(j=0;j<n;++j)
scanf("%d%d%d%d",&v[i][j].cost1,&v[i][j].cost2,&v[i][j].cost3,&v[i][j].cost4);
back(0,0);
printf("%d\n",sol);
return 0;
}