POJ 1681- Painter's Problem

来源:互联网 发布:常熟淘宝美工招聘 编辑:程序博客网 时间:2024/06/02 04:17

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

23yyyyyyyyy5wwwwwwwwwwwwwwwwwwwwwwwww

Sample Output

015题目大意,给出一些方格,需要把所有的方格刷成黄色,粉刷每个方格时不仅时当前方格变色,同时使其上下左右的方格颜色都反转,是不是想到了位运算?思路分析:可以看出只要第一行的状态确定了,那么第二行应该怎么刷就确定了,因为要保证第一行全部都要刷成黄色,第二行确定了,那么第三行也就确定了,一直到最后          一行,判断一下最后一行是不是全部为黄色即可,用一个数组B[15]来表示第一行第i个位置是否要粉刷,0为否,1为是,枚举所有的可能,然后求出需要最小的粉          刷次数,如果找不到粉刷次数,则输出 infAC 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;}


原创粉丝点击