[ABC 099] D-Good Grid

D - Good Grid


Time limit : 2sec / Memory limit : 256MB

Score : 400 points

Problem Statement

There is a grid with N rows and N columns of squares. Let (i,j) be the square at the i-th row from the top and the j-th column from the left.

These squares have to be painted in one of the C colors from Color 1 to Color C. Initially, (i,j) is painted in Color ci,j.

We say the grid is a good grid when the following condition is met for all i,j,x,y satisfying 1≤i,j,x,yN:

  • If (i+j)%3=(x+y)%3, the color of (i,j) and the color of (x,y) are the same.
  • If (i+j)%3≠(x+y)%3, the color of (i,j) and the color of (x,y) are different.

Here, X%Y represents X modulo Y.

We will repaint zero or more squares so that the grid will be a good grid.

For a square, the wrongness when the color of the square is X before repainting and Y after repainting, is DX,Y.

Find the minimum possible sum of the wrongness of all the squares.

Constraints

  • 1≤N≤500
  • 3≤C≤30
  • 1≤Di,j≤1000(ij),Di,j=0(i=j)
  • 1≤ci,jC
  • All values in input are integers.

[题目解释]

  给你一个n*n的矩阵,D(i,j)为矩阵i行j列的初始颜色,给你一个c*c的矩阵C(i,j)表示i颜色变为j颜色所花费的代价,当然i,j≤c且i=j时代价为0,先输入n和c,再一次输入C矩阵和D矩阵,在D矩阵中如果满足横坐标i加纵坐标j的和mod 3同余的每一个方块颜色相同,余数不同的每一个方块颜色不同就是一个好矩阵,问我们如何花费最小代价使这个矩阵变为一个好矩阵.

[题目解析]

  由于每一个格子的横坐标和纵坐标之和mod 3的值只有3种可能,0,1,2我们可以在记一个数组t[i][j]表示余数为i的方格,颜色为j的数目为t[i][j]个,在输入矩阵D的时候,计算这个方格横坐标和纵坐标 mod 3的值将该颜色的个数+1即可.因为c的最大值为30,所以我们可以依次枚举每一种余数所对应的方块统一变成一种颜色所对应的代价,将其加起来如果小于已知最优答案,更新答案即可.

[代码]

/*
    Name: Good Grid 
    Author: FZSZ-LinHua
    Date: 2018 06 10
    Exec time: 22ms
    Memory usage: 384KB
    Score: 400
    Algorithm: Brute-force 
*/
# include "iostream"
# include "cstdio"

using namespace std;

const int maxm=1000+10;
const int maxn=30+10; 
int n,c,d[maxn][maxn],t[4][maxn],x,y,z,ans;
bool f1,f2,f3;

int main(){
    scanf("%d%d",&n,&c);
    ans=0xfffffff;    //先把已知答案记为一个极大值 
    register int i,j,k,l; 
    for(i=1;i<=c;i++){    //输入C矩阵(i-j)为颜色i变为颜色j的代价为(i,j) 
        for(j=1;j<=c;j++){
            scanf("%d",&d[i][j]);
        }
    }
    for(i=1;i<=n;i++){    //输入矩阵D 
        for(j=1;j<=n;j++){
            scanf("%d",&x);
            t[(i+j)%3][x]++;    //计算横坐标加纵坐标mod 3的数值,颜色为x的个数+1 
        }
    }
    for(i=1;i<=c;i++){    //枚举余数为0统一变成颜色i其代价为x 
        x=0; 
        for(j=1;j<=c;j++){
            x+=t[0][j]*d[j][i];
        }
        for(j=1;j<=c;j++){    //枚举余数为1统一变成颜色j,其代价为y 
            if(j==i) continue;    //不同余数的颜色不能相同 
            y=0;
            for(k=1;k<=c;k++){
                if(t[1][k]>=1 && y>=1){
                    f2=true;
                }
                y+=t[1][k]*d[k][j];
            }
            for(k=1;k<=c;k++){    //枚举余数为2统一变成颜色k,其代价为z 
                if(k==j || k==i) continue;    //不同余数的颜色不同 
                z=0;
                for(l=1;l<=c;l++){
                    z+=t[2][l]*d[l][k];
                }
                if(ans>x+y+z){    //如果当前转换的代价比已知最优答案小就更新答案 
                    ans=x+y+z;
                }
            }
        }
    } 
    printf("%d",ans);
    return 0; 
} 

 

D - Good Grid


Time limit : 2sec / Memory limit : 256MB

Score : 400 points

Problem Statement

There is a grid with N rows and N columns of squares. Let (i,j) be the square at the i-th row from the top and the j-th column from the left.

These squares have to be painted in one of the C colors from Color 1 to Color C. Initially, (i,j) is painted in Color ci,j.

We say the grid is a good grid when the following condition is met for all i,j,x,y satisfying 1≤i,j,x,yN:

  • If (i+j)%3=(x+y)%3, the color of (i,j) and the color of (x,y) are the same.
  • If (i+j)%3≠(x+y)%3, the color of (i,j) and the color of (x,y) are different.

Here, X%Y represents X modulo Y.

We will repaint zero or more squares so that the grid will be a good grid.

For a square, the wrongness when the color of the square is X before repainting and Y after repainting, is DX,Y.

Find the minimum possible sum of the wrongness of all the squares.

Constraints

  • 1≤N≤500
  • 3≤C≤30
  • 1≤Di,j≤1000(ij),Di,j=0(i=j)
  • 1≤ci,jC
  • All values in input are integers.

Input

posted @ 2018-06-11 11:09  FJ-Frank  阅读(387)  评论(0编辑  收藏  举报