任意门:http://poj.org/problem?id=1681

Painter's Problem
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 7667   Accepted: 3624

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

题意概括:

一个二维矩阵, 输入每个格子的初始颜色,可以进行的操作是 粉刷一个格子则相邻的上下左右四个各自颜色都会取反(只有两种颜色);

问最后把全部各自涂成黄色的最小操作数;

解题思路:

根据题意,每个各自都是一个变元,根据各自之间的相邻关系构造增广矩阵;

高斯消元求出自由元个数 sum;

二进制枚举方案,找出最小的操作数;

AC code:

 #include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
const int MAXN = ; int equ, var;
int a[MAXN][MAXN];
char str[MAXN][MAXN];
int x[MAXN];
int free_x[MAXN];
int free_num;
int N; int Gauss()
{
int maxRow, col, k;
free_num = ;
for(k = , col = ; k < equ && col < var; k++, col++){
maxRow = k;
for(int i = k+; i <equ; i++){
if(abs(a[i][col]) > abs(a[maxRow][col]))
maxRow = i;
}
if(a[maxRow][col] == ){ //最大的都为0说明该列下面全是 0
k--;
free_x[free_num++] = col; //说明col是自由元
continue;
}
if(maxRow != k){ //交换行
for(int j = col; j < var+; j++)
swap(a[k][j], a[maxRow][j]);
}
for(int i = k+; i < equ; i++){ //消元
if(a[i][col] != ){
for(int j = col; j < var+; j++){
a[i][j] ^= a[k][j];
}
}
}
}
for(int i = k; i< equ; i++){
if(a[i][col] != ) return -; //无解
}
if(k < var) return var-k; //返回自由元个数 for(int i = var-; i >= ; i--){ //唯一解,回代
x[i] = a[i][var];
for(int j = i+; j < var; j++){
x[i] ^= (a[i][j] && x[j]);
}
}
return ;
} void init()
{
memset(a, , sizeof(a));
memset(x, , sizeof(x));
equ = N*N;
var = N*N;
for(int i = ; i < N; i++){ //构造增广矩阵
for(int j = -; j < N; j++){
int t = i*N+j;
a[t][t] = ;
if(i > ) a[(i-)*N+j][t] = ;
if(i < N-) a[(i+)*N+j][t] = ;
if(j > ) a[i*N+j-][t] = ;
if(j < N-) a[i*N+j+][t] = ;
}
}
} void solve()
{
int t = Gauss();
if(t == -){ //无解
printf("inf\n");
return;
}
else if(t == ){ //唯一解
int ans = ;
for(int i = ; i < N*N; i++){
ans += x[i];
}
printf("%d\n", ans);
return;
}
else{ //多解,需要枚举自由元
int ans = INF;
int tot = <<t;
int cnt = ;
for(int i = ; i < tot; i++){
cnt = ;
for(int j = ; j < t; j++){
if(i&(<<j)){
x[free_x[j]] = ;
cnt++;
}
else x[free_x[j]] = ;
} for(int j = var-t-; j >= ; j--){
int index;
for(index = j; index < var; index++){
if(a[j][index]) break;
}
x[index] = a[j][var]; for(int s = index+; s < var; s++)
if(a[j][s]) x[index] ^= x[s]; cnt+=x[index];
}
ans = min(ans, cnt);
}
printf("%d\n", ans);
}
} int main()
{
int T_case;
int tpx, tpy;
scanf("%d", &T_case);
while(T_case--){
scanf("%d", &N);
init();
for(int i = ; i < N; i++){
scanf("%s", str[i]);
for(int j = ; j < N; j++){
tpx = i*N+j, tpy = N*N;
if(str[i][j] == 'y') a[tpx][tpy] = ;
else a[tpx][tpy] = ;
}
}
solve();
}
return ;
}

最新文章

  1. js 弹出确认 取消对话框
  2. LoadRunner脚本参数化设置
  3. 记录java基础的学习过程
  4. 如何闪开安装VS2013必须要有安装IE10的限制
  5. MFC 最大化 的时候控件 按比例变大
  6. HttpRequestDeviceUtils
  7. why does txid_current() assign new transaction-id?
  8. C#扩展方法入门
  9. 【转】Hibernate映射机制之XXX.hbm.xml
  10. SAP HANA STRING_AGG
  11. 跑openstack命令错误【You must provide a username via either -...】
  12. 一个js的动画,以前以为只有flash可以实现
  13. Asp.Net WebAPI配置接口返回数据类型为Json格式
  14. 559. Maximum Depth of N-ary Tree
  15. Error: listen EADDRINUSE :::3000
  16. from组件补充
  17. liteos 从入门到放弃
  18. Java SubString截取字符串
  19. recyclerView插入(add)和删除(remove)item后,item错乱,重复,覆盖在原recyclerView上
  20. jndi 小案例

热门文章

  1. jenkins +gitlab 自动化代码秒级上线
  2. (转)创建DB2实例时出错,请大家帮忙解决
  3. 3d旋转卡牌
  4. nyoj 349&amp;Poj 1094 Sorting It All Out——————【拓扑应用】
  5. MVC中提交表单的4种方式
  6. js数据类型检测小结
  7. ul+js模拟select
  8. Js COOkie 读取
  9. 调用WCF错误-There was no endpoint listening
  10. MVC3.0与MVC2.0的区别