POJ1671--Painer'sProblem

本文探讨了Painter's Problem的解决方法,这是一个关于将墙面砖从白色涂成黄色的算法问题。通过特定的刷子操作,每次能改变自身及其周围四个方向砖块的颜色。文章提供了两种实现方式并讨论了如何找到最少的涂色次数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Painter’s Problem
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 5261 Accepted: 2549
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

几经波折,最终终于通过了,总是因为变量初始化的位置不对导致我改bug用了很长时间。。。。
下面是我拙劣的代码

#include<iostream>
#include<cstring>
using namespace std;
int n;
int wall[16][17];
int paint[16][17];
int mark=0;
int count=0;
bool solution()
{
    count=0;
    for(int j=1;j<=n;j++)
    {
        if(((wall[n][j]+paint[n][j]+paint[n-1][j]+paint[n][j-1]+paint[n][j+1])%2)==0)
            return false;
    }
    for(int i=1;i<=n;i++)
    for(int j=1;j<=n;j++)
    {
        if(paint[i][j]) count++;
    }
    return true;
}
int main()
{
    int t;
    int mmax;
    int mmin=300;
    char xy[16][17];
    cin>>t;
    while(t--)
    {

        memset(wall,0,sizeof(wall));
        memset(paint,0,sizeof(paint));
        cin>>n;

        //数据输入 
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
            {
                cin>>xy[i][j];
                if(xy[i][j]=='y')
                    wall[i][j]=1;
            }


        mmax=1<<n;
        mark=0;
        mmin=300; 
        for(int k=1;k<=mmax;k++)
        {

            for(int i=2;i<=n;i++)
            for(int j=1;j<=n;j++)
            {
                if(((wall[i-1][j]+paint[i-1][j]+paint[i-1][j-1]+paint[i-1][j+1]+paint[i-2][j])%2)==0)
                    paint[i][j]=1;
                else
                    paint[i][j]=0;
            }
            if(solution())
            {
                mark=1;
            /// cout<<mmin<<endl;
                if(mmin>count)
                    mmin=count;
                //  cout<<count<<endl;
            }       
            //枚举第一行 
            int c=1;
            paint[1][1]++;
            while(paint[1][c]>1)
            {
                paint[1][c]=0;
                c++;
                paint[1][c]++;
            }

        }
        if(mark)
            cout<<mmin<<endl;
        else
            cout<<"inf"<<endl;  
    }
    return 0;
}

这是大神的代码

#include <iostream>
using namespace std;

bool wall[18][18];
bool paint[18][18];
int count=0;
int mmin=300;

bool allYellow(int n)
{
    int i,j;
    count=0;
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=n;j++)
        {
            if(wall[i][j]^paint[i][j]^paint[i][j-1]^paint[i][j+1]^paint[i-1][j]^paint[i+1][j])return false;
            if(paint[i][j])count++;
        }
    }
    return true;
}

int main()
{
    freopen("in.txt","r",stdin);
    int t,n;
    cin>>t;
    int i,j,k,p;
    char wy[16];
    int mmax,tmp;
    bool possible=false;

    for(i=0;i<t;i++)
    {
        cin>>n; 
        memset(wall,0,sizeof(wall));

        for(j=1;j<=n;j++)
        {
            cin>>wy;
            for(k=0;k<n;k++)
            {
                if(wy[k]=='w')wall[j][k+1]=true;
            }
        }

        mmax=1<<n;
        mmin=300;
        possible=false;

        for(j=0;j<mmax;j++)         //第一行的状态 0000 - 1111 枚举
        {
            memset(paint,0,sizeof(paint));
            tmp=j;
            for(k=1;k<=n;k++)      //第一行的状态
            {
                paint[1][k]=tmp&1;
                tmp=tmp>>1;       //右移一位
            }

            for(k=2;k<=n;k++)     
            {
                for(p=1;p<=n;p++)       //根据wall[k-1][p]的paint后状态 决定paint[k][p]值
                {
                    if(wall[k-1][p]^paint[k-1][p]^paint[k-1][p-1]^paint[k-1][p+1]^paint[k-2][p])paint[k][p]=true;
                }
            }

            if(allYellow(n))
            {
                possible=true;
                if(mmin>count)mmin=count;
            }

        }
        if(possible)cout<<mmin<<endl;
        else cout<<"inf"<<endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值