POJ 1681 画家问题

该博客介绍了POJ 1681编程题目,即画家Bob试图将一个n*n的方格墙全部涂成黄色的问题。由于刷子的特性,一次涂色会改变周围五个砖块的颜色。博主分享了如何找到最小的涂色次数,以解决此问题,指出这是一道枚举题,并提到了利用二进制表示和图数组在解决过程中的关键作用。给出的样例输入和输出展示了不同情况下的结果。
摘要由CSDN通过智能技术生成

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

想了好久才发现本题是枚举题。
技巧在于利用二进制表示黄与白,并且用graph数组与进行操作的paint数组来判定是否要上色。
利用第一行的二进制状态进行枚举

#include <iostream>
#include <cmath>
#include <climits>
#include <string>
#include <cctype>
#include <vector>
#include <cmath>
#include <cstring>
#include <array>
using namespace std;
int graph[18][18]={0},paint[18][18]={0};
int N;
int total=0;
int min1;
bool guess();
bool enumerate();

int main(){
    int i,j;
    cin>>N;
    char c;
    for(i=1;i<=N;i++){
        for(j=1;j<=N;j++){
            cin>>c;
            if(c=='y') graph[i][j]=0;    //0 denotes yellow
            else graph[i][j]=1;      //1 denotes white
        }
    }
    if(enumerate()==false)
        cout<<"inf";
    else{


        cout<<min1;
    }
}
bool guess(){
    int i,j;
    for(i=1;i<N;i++){
        for(j=1;j<=N;j++){
            paint[i+1][j]=(graph[i][j]+paint[i][j]+paint[i-1][j]+paint[i][j-1]+paint[i][j+1])%2;
        }
    }
    for(j=1;j<=N;j++){
        if(graph[N][j]!=(paint[N][j-1]+paint[N][j]+paint[N][j+1]+paint[N-1][j])%2)
            return(false);
    }
    return(true);
}
bool enumerate(){
    int j,flag=0,i;
    i=1;
    min1=10000;
    while(i<=(int)pow(2,N)){
        if(guess()==true){
            total=0;
            flag=1;
            for(int i=1;i<=N;i++){
                for(int j=1;j<=N;j++){
                    if(paint[i][j]==1)
                        total++;
                }
            }
            if(total<min1) min1=total;
        }
        paint[1][1]++;
        j=1;
        while(paint[1][j]>1){
            paint[1][j]=0;
            j++;
            paint[1][j]+=1;
        }                //处理进位

        i++;
    }
    if(!flag) return(false);
    return(true);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值