您的位置:首页 > 大数据 > 人工智能

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

2017-10-01 00:38 501 查看
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

POJ Monthly--2004.06.27 张嘉龄
题意:给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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: