POJ1681:Painter's Problem(高斯消元 & 格子转换 & 二进制)

本文介绍了一种使用高斯消元法解决Painter's Problem的方法,旨在求解将所有砖块变为黄色所需的最少操作次数。通过构建线性方程组并求解,实现了对特定地图上砖块颜色的高效转换。

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

Painter's Problem
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 7113 Accepted: 3388

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

Source

题意:给N*N的地图,有两种颜色,每次改变一个格子的颜色,其上下左右的格子也会变成相反的颜色,求令所有格子变成黄色的最少改变次数。

思路:高斯消元,即解线性方程组,这个需要一点线性代数的基础。怎样构造方程组呢?定义a[i][j]=1表示改变第j个格子,第i个格子将会变色,=0即表示不变这样子构造n*n-1 x n*n的增广矩阵,解出它们各自的解,因为题目要求次数最少,那么自由变元需要枚举下取值了。

# include <iostream>
# include <cstdio>
# include <cstring>
# include <algorithm>
using namespace std;
typedef long long LL;
int T, t, n, a[500][500], x[500], fre[503];
char s[18][18];
//高斯消元,计算线性方程组,先进行初等行变换化成阶梯矩阵,再从最后一行把值逐个回代。函数返回-1无解,正数为有自由元(本题没体现).
//-2为有浮点数解(本题没体现),0为有唯一解(本题没体现)。
int gauss(int n, int m)
{
    int r=0, c=0, cnt=0, res=0x7fffffff;
    while(r<n && c<m)
    {
        int id = r;//找到当前列首元素最大的行,初始化为第r行。
        for(int i = r+1; i<n; ++i)
            if(a[i][c] > a[id][c])
                id = i;
        if(id != r)//如果不是第r行最大,就交换两行。
            for(int i=r; i<=m; ++i)
                swap(a[r][i], a[id][i]);
        if(a[r][c] != 0)//如果当前首元素非0,那么需要将其他元素该位置清0(阶梯矩阵的定义)
        {
            for(int i=r+1; i<n; ++i)
                if(a[i][c] != 0)
                    for(int j=c; j<=m; ++j)
                        a[i][j] ^= a[r][j];//本题是异或运算,这样算也相当于加减运算中的极大无关线性组。
            ++r;//进入下一行。
        }
        else fre[cnt++] = c;//否则他就是自由元,记下来。
        ++c;
    }
    for(int i=r; i<n; ++i)//无解,即出现000...00X的行。
        if(a[i][m] != 0) return -1;
    for(int i=0; i<(1<<n-r); ++i)//自由元的不同赋值可能有不同的解,枚举它们的取值找最小解。
    {
        int tot = 0, sta = i;
        for(int j=0; j<n-r; ++j)
        {
            x[fre[j]] = sta&1;
            if(sta&1) ++tot;
            sta >>= 1;
        }
        for(int j=r-1; j>=0; --j)
        {
            int tmp = a[j][m];
            for(int k=j+1; k<m; ++k)
                if(a[j][k]) tmp ^= x[k];
            x[j] = tmp;//本题此处 不一定特指第j个解就是x[j],严格来说是第j行的第一个非0项所在的列为x[j]。
            if(x[j]) ++tot;
        }
        res = min(res, tot);
    }
    return res;
}
int main()
{
    scanf("%d",&T);
    while(T--)
    {
        memset(a, 0, sizeof(a));
        scanf("%d",&n);
        for(int i=0; i<n; ++i) scanf("%s",s[i]);
        for(int i=0; i<n; ++i)
            for(int j=0; j<n; ++j)
            {
                int t = i*n+j;
                a[t][n*n] = (s[i][j]=='w');
                a[t][t] = 1;
                if(i>0) a[(i-1)*n+j][t] = 1;
                if(j>0) a[t-1][t] = 1;
                if(i<n-1) a[(i+1)*n+j][t] = 1;
                if(j<n-1) a[t+1][t] = 1;
            }
        int tmp = gauss(n*n, n*n);
        if(tmp == -1) puts("inf");
        else printf("%d\n",tmp);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值