zoukankan      html  css  js  c++  java
  • poj1681 Painter's Problem(gauss+dfs判定)

    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

    分析:
    高斯消元解异或方程
    但是这道题需要考虑无解和自由元的情况。

    如果一个方程左边的未知数的系数全是0,但是最后的结果不为0,那么一定无解

    如果一个方程左边的未知数的系数全是0,但是最后的结果为0,
    因为第i个方程是用来就第i个未知数的,所以就说第i个未知数是自由元(与该位置的取值无关)
    对这些自由元我们进行爆搜,将爆搜出的结果代入每个方程验证,看是否可行。

    注意

    这里说的自由元,是指不能确定状态
    没有明确的说一定是0或一定是1

    判断可行的方法就是模拟

    for (int i=1;i<=n;i++)
    {
        int k=0;
        for (int j=1;j<=n;j++)
            k^=a[i][j]*p[j];   //*p[j]  当前状态
        if (k!=b[i]) ff=0;
    }

    tip

    a[i]数组里记录的是与i有关的未知量的系数
    第n+1位记录的是答案
    板子一定要打对

    //这里写代码片
    #include<cstdio>
    #include<cstring>
    #include<iostream>
    
    using namespace std;
    
    const int INF=1e9;
    int nn,n,tot;
    int a[300][300],cnt,b[300],ans[300];
    char s[20];
    int p[300];
    
    int get(int x,int y){return (x-1)*nn+y;}
    
    void dfs(int t)
    {
        if (t>n)
        {
            bool ff=1;
            for (int i=1;i<=n;i++)
            {
                int k=0;
                for (int j=1;j<=n;j++)
                    k^=a[i][j]*p[j];   //方程系数 
                if (k!=b[i]) ff=0;
            }
            if (ff)
            {
                int siz=0;
                for (int i=1;i<=n;i++)
                    if (p[i]) siz++;
                tot=min(tot,siz);
            }
            return;
        }
        if (ans[t]!=-1) p[t]=ans[t],dfs(t+1);   //确定的状态 
        else
        {
            p[t]=0; dfs(t+1);  
            p[t]=1; dfs(t+1);
        }
    }
    
    int gauss()
    {
        cnt=0; tot=0;
        int now=1,to;
        for (int i=1;i<=n;i++)
        {
            for (to=now;to<=n;to++)
                if (a[to][i]) break;
            if (to>n)
            {
                now++;
                continue;
            }
            if (to!=now)
                for (int j=1;j<=n+1;j++)
                    swap(a[to][j],a[now][j]);
            for (int j=1;j<=n;j++)
                if (j!=now&&a[j][i])
                    for (int k=1;k<=n+1;k++)
                        a[j][k]^=a[now][k];
            now++;
        }
    
        for (int i=1;i<=n;i++) b[i]=a[i][n+1];
        for (int i=n;i>=1;i--)
        {
            if (a[i][i]==0&&a[i][n+1]!=0) return 0;  //无解 
            if (a[i][i]==0&&a[i][n+1]==0)
            {
                ans[i]=-1;
                cnt++;   //自由元个数 
                continue;
            }
            ans[i]=a[i][n+1];
            if (ans[i]) tot++;   //确定需要涂色
            for (int j=i-1;j>=1;j--)
                if (a[j][i])
                    a[j][n+1]^=ans[i]; 
        }
    
        if (cnt)
        {
            tot=INF;
            dfs(1);
        }
    
        return 1;
    }
    
    int main()
    {
        int T;
        scanf("%d",&T);
        while (T--)
        {
            memset(a,0,sizeof(a));
            scanf("%d",&nn);
            n=nn*nn;
            for (int i=1;i<=nn;i++)
            {
                scanf("%s",&s);
                for (int j=0;j<nn;j++)
                    if (s[j]=='y') a[get(i,j+1)][n+1]=0;
                    else a[get(i,j+1)][n+1]=1;
            }
            for (int i=1;i<=nn;i++)
                for (int j=1;j<=nn;j++)
                {
                    int bh=get(i,j);
                    a[bh][bh]=1;
                    if (i>1) a[bh][get(i-1,j)]=1;
                    if (j>1) a[bh][get(i,j-1)]=1;
                    if (i<nn) a[bh][get(i+1,j)]=1;
                    if (j<nn) a[bh][get(i,j+1)]=1;
                }
            if (gauss()) printf("%d
    ",tot);
            else printf("inf
    ");
        }
        return 0;
    }
  • 相关阅读:
    oracle操作小常识
    Report Machine
    ReportMachine
    如何创建Asp.net MVC ViewModel
    Page Scroll Menu (页面中锚点菜单)
    EF selection expression 与 Linq备忘
    chrome 浏览器 开发者工具 性能检测 参数解释
    IE 兼容性写法
    Asp.net MVC Bundle 的使用与扩展
    ASP.NET MVC 性能优化总结
  • 原文地址:https://www.cnblogs.com/wutongtong3117/p/7673076.html
Copyright © 2011-2022 走看看