• POJ 1681 Painter's Problem


    Painter's Problem

    Description

    There is a square wall which is made of n*n small square bricks. Some bricks are white while some bricks are yellow. Bob is a painter and he wants to paint all the bricks yellow. But there is something wrong with Bob's brush. Once he uses this brush to paint brick (i, j), the bricks at (i, j), (i-1, j), (i+1, j), (i, j-1) and (i, j+1) all change their color. Your task is to find the minimum number of bricks Bob should paint in order to make all the bricks yellow.

    Input

    The first line contains a single integer t (1 <= t <= 20) that indicates the number of test cases. Then follow the t cases. Each test case begins with a line contains an integer n (1 <= n <= 15), representing the size of wall. The next n lines represent the original wall. Each line contains n characters. The j-th character of the i-th line figures out the color of brick at position (i, j). We use a 'w' to express a white brick while a 'y' to express a yellow brick.

    Output

    For each case, output a line contains the minimum number of bricks Bob should paint. If Bob can't paint all the bricks yellow, print 'inf'.

    Sample Input

    2
    3
    yyy
    yyy
    yyy
    5
    wwwww
    wwwww
    wwwww
    wwwww
    wwwww
    

    Sample Output

    0
    15
    
    题目大意,给出一些方格,需要把所有的方格刷成黄色,粉刷每个方格时不仅时当前方格变色,同时使其上下左右的方格颜色都反转,是不是想到了位运算?
    
    思路分析:可以看出只要第一行的状态确定了,那么第二行应该怎么刷就确定了,因为要保证第一行全部都要刷成黄色,第二行确定了,那么第三行也就确定了,一直到最后
              一行,判断一下最后一行是不是全部为黄色即可,用一个数组B[15]来表示第一行第i个位置是否要粉刷,0为否,1为是,枚举所有的可能,然后求出需要最小的粉
              刷次数,如果找不到粉刷次数,则输出 inf
    
    AC code:
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<cstdlib>
    #include<algorithm>
    #include<cmath>
    
    using namespace std;
    
    int A[20],B[20];
    int a[20][20],b[20][20];
    void DecToBin(int n,int *a,int len)
    {
        int cnt=len;
        while(n)
        {
            a[cnt--]=n%2;
            n/=2;
        }
        while(cnt)
        {
            a[cnt--]=0;
        }
    }
    int POW(int b)
    {
        int res=1;
        for(int i=0;i<b;i++)
        {
            res*=2;
        }
        return res;
    }
    int main()
    {
        //freopen("in.txt","r",stdin);
        int N,m;
        cin>>N;
        int cnt;
        while(N--)
        {
            int ct=0;
            int Min=100000000;
            memset(a,0,sizeof a);
            cin>>m;
            char c;
            bool flag=0;
            for(int i=1;i<=m;i++)
            {
                for(int j=1;j<=m;j++)
                {
                    cin>>c;
    //                A[i]*=2;
                    if(c=='y')
    //                {
                        a[i][j]=1;
    //                    A[i]+=1;
    //                }
                    else
                    {
                        a[i][j]=0;
                        flag=1;
                    }
                }
            }
            if(!flag)
            {
                cout<<0<<endl;
                continue;
            }
            int temp=POW(m);
            //cout<<temp;
            for(int i=0;i<temp;i++)
            {
                //cout<<"i="<<i<<endl;
                cnt=0;
                memcpy(b,a,sizeof a);
                DecToBin(i,B,m);
    //            for(int j=1;j<=m;j++)
    //                cout<<B[j]<<" ";
    //            cout<<endl;
                for(int j=1;j<=m;j++)
                {
                    if(B[j])
                    {
                        cnt++;
                        b[1][j]^=B[j];
                        b[2][j]^=B[j];
                        b[1][j-1]^=B[j];
                        b[1][j+1]^=B[j];
                    }
                }
                for(int j=2;j<=m;j++)
                {
                    for(int k=1;k<=m;k++)
                    {
                        if(b[j-1][k]==0)
                        {
                            b[j][k]^=1;
                            b[j+1][k]^=1;
                            b[j-1][k]^=1;
                            b[j][k+1]^=1;
                            b[j][k-1]^=1;
                            cnt++;
                        }
                    }
                }
                bool flag=1;
                for(int j=1;j<=m;j++)
                {
                    //cout<<b[m][j];
                    if(b[m][j]!=1)
                    {
                        flag=0;
                        break;
                    }
                }
                //cout<<endl;
                if(flag)
                {
                    ct=1;
                    if(cnt<Min)
                        Min=cnt;
                }
    
            }
            if(!ct)
                cout<<"inf"<<endl;
            else
                cout<<Min<<endl;
        }
        return 0;
    }
    


  • 相关阅读:
    判断字符中是否包含汉字
    since I lived here; since I have lived here. 的区别? 从语法上看, 为啥会有这样的区别?
    have married; have been married; 到底是结婚了没?还是已经离婚了?
    C#项目依据 x86 x64 配置不同的引用
    现在完成时可以表示过去事件对现在的影响/效果. 过去完成时也可以起相同的作用!!!!
    使用现在完成时的常见错误(转发)
    去除win10下的缺省ctrl加空格功能
    appear + 表语 与 appear to be + "表语" 的区别; get hurt与 get to be hurt的区别
    ssm搭建的一个web应用的工作流程
    return和finally究竟谁先执行,还有return是怎么返回数据的
  • 原文地址:https://www.cnblogs.com/jiangu66/p/3003869.html
Copyright © 2020-2023  润新知