CodeForces - 961C Chessboard (模拟暴力)Magnus decided to play a classic chess game. Though what he saw...

链接:http://codeforces.com/problemset/problem/961/C

Magnus decided to play a classic chess game. Though what he saw in his locker shocked him! His favourite chessboard got broken into 4 pieces, each of size n by nn is always odd. And what's even worse, some squares were of wrong color. j-th square of the i-th row of k-th piece of the board has color ak, i, j1 being black and 0 being white.

Now Magnus wants to change color of some squares in such a way that he recolors minimum number of squares and obtained pieces form a valid chessboard. Every square has its color different to each of the neightbouring by side squares in a valid board. Its size should be 2n by 2n. You are allowed to move pieces but not allowed to rotate or flip them.

Input

The first line contains odd integer n (1 ≤ n ≤ 100) — the size of all pieces of the board.

Then 4 segments follow, each describes one piece of the board. Each consists of n lines of n characters; j-th one of i-th line is equal to 1 if the square is black initially and 0 otherwise. Segments are separated by an empty line.

Output

Print one number — minimum number of squares Magnus should recolor to be able to obtain a valid chessboard.

Examples
Input
1
0

0

1

0
Output
1
Input
3
101
010
101

101
000
101

010
101
011

010
101
010
Output
2

题意:完整的棋盘被分成四部分。而且颜色也有些变化了(1是黑色,0是白色,原来1,0,是相邻的,相同的数不会在一起),让你用最小的代价重新合成这个棋盘(就是改变最少的颜色)。如果要组成完整的棋盘,必须有两个开头为0的,和两个开头为一的(n是奇数)。

#include<stdio.h>
#define inf 0x3f3f3f3f
#include<algorithm>
#include<string.h>
using namespace std;
int n;
char s1[200][200];
char s2[200][200];
char s3[200][200];
char s4[200][200];
char mp1[200][200];  //开头为0;
char mp2[200][200];  //开头为1;
int num[200][200];
int cmp(char a[200][200],char b[200][200])
{
    int num=0;
    for(int i=0; i<n; i++)
        for(int j=0; j<n; j++)
        {
            if(a[i][j]!=b[i][j])num++;
        }
    return num;
}
int main()
{
    while(~scanf("%d",&n))
    {
        for(int i=0; i<n; i++)
            scanf("%s",&s1[i]);
        for(int i=0; i<n; i++)
            scanf("%s",&s2[i]);
        for(int i=0; i<n; i++)
            scanf("%s",&s3[i]);
        for(int i=0; i<n; i++)
            scanf("%s",&s4[i]);
        int ans=inf;
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<n; j++)
            {
                if((i+j)%2==0)
                {
                     mp1[i][j]='0';
                     mp2[i][j]='1';
                }
                else
                {
                    mp1[i][j]='1';
                    mp2[i][j]='0';
                }
            }
        }
        int t1=cmp(s1,mp1);
        int t2=cmp(s1,mp2);
        num[0][0]=t1;
        num[0][1]=t2;
        t1=cmp(s2,mp1);
        t2=cmp(s2,mp2);
        num[1][0]=t1;
        num[1][1]=t2;
        t1=cmp(s3,mp1);
        t2=cmp(s3,mp2);
        num[2][0]=t1;
        num[2][1]=t2;
        t1=cmp(s4,mp1);
        t2=cmp(s4,mp2);
        num[3][0]=t1;
        num[3][1]=t2;

        for(int i=0;i<2;i++)
        for(int j=0;j<2;j++)
            for(int k=0;k<2;k++)
            for(int z=0;z<2;z++)
            {
                int a=0,b=0;
                if(i==0)a++;
                else b++;
                if(j==0)a++;
                else b++;
                if(k==0)a++;
                else b++;
                if(z==0)a++;
                else b++;
                if(a==2&&b==2)
                {
                    ans=min(ans,num[0][i]+num[1][j]+num[2][k]+num[3][z]);
                }
            }
        printf("%d\n",ans);
    }
    return 0;
}


转载于:https://www.cnblogs.com/zitian246/p/9123592.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用\[1\]中提到了一种树形动态规划的方法来解决CodeForces - 982C问题。在这个问题中,subtree指的是子连通块,而不是子树。为了使cnt_white - cnt_black尽可能大,可以使用两次树形动态规划来求解。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。如果ans\[u\]为正,则减去dp\[v\]就是树链所代表的子树的权值。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] 问题: CodeForces - 982C 树形DP是什么问题?如何解决? 回答: CodeForces - 982C是一个树形动态规划问题。在这个问题中,需要求解子连通块的最大权值和,使得cnt_white - cnt_black尽可能大。解决这个问题的方法是使用两次树形动态规划。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] #### 引用[.reference_title] - *1* *2* [CodeForces - 1324F Maximum White Subtree(树形dp)](https://blog.csdn.net/qq_45458915/article/details/104831678)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值