Painter's Problem (高斯消元)

时间:2022-01-31 23:10:26
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.
Painter's Problem (高斯消元)

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
// POJ 1681 为例题:

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std; const int maxn = ;
//有equ个方程, var个变元。增广矩阵列数为var+1:0到var;
int equ, var;
int a[maxn][maxn]; // 增广矩阵
int x[maxn]; //解集
int free_x[maxn]; // *元
int free_num; //*元个数 //返回-1无解, 为0 唯一解, 否则返回*变元个数;
int Gauss()
{
int max_r, col, k;
free_num = ;
for(k = , col = ; k < equ&&col < var; k++, col++)
{
max_r = k;
for(int i = k+; i < equ; i++)
{
if(abs(a[i][col]) > abs(a[max_r][col]))
max_r = i;
}
if(a[max_r][col] == )
{
k--;
free_x[free_num++] = col; // 因为只有0,1;当最大为0,则为*元
continue;
}
if(max_r != k) // 交换
{
for(int j = col; j < var+; j++)
{
swap(a[k][j], a[max_r][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 ;
} int n;
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 = 0x3f3f3f3f;
int tot = ( << t);
for(int i =; i < tot; i++)
{
int 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 idx;
for(idx = j; idx < var; idx++)
if(a[j][idx])
break;
x[idx] = a[j][var];
for(int l = idx+; l < var; l++)
if(a[j][l])
x[idx] ^= x[l];
cnt += x[idx];
}
ans = min(ans , cnt);
}
printf("%d\n", ans);
}
} char str[][];
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d", &n);
init();
for(int i = ; i < n; i++)
{
scanf("%s", str[i]);
for(int j = ; j < n; j++)
{
if(str[i][j] == 'y')
a[i*n+j][n*n] = ;
else a[i*n+j][n*n] = ;
}
}
solve();
}
return ;
}

Painter's Problem (高斯消元)的更多相关文章

  1. POJ 1681 Painter&&num;39&semi;s Problem &lpar;高斯消元&rpar;

    题目链接 题意:有一面墙每个格子有黄白两种颜色,刷墙每次刷一格会将上下左右中五个格子变色,求最少的刷方法使得所有的格子都变成yellow. 题解:通过打表我们可以得知4*4的一共有4个*变元,那么我 ...

  2. POJ 1681 Painter&&num;39&semi;s Problem &lbrack;高斯消元XOR&rsqb;

    同上题 需要判断无解 需要求最小按几次,正确做法是枚举*元的所有取值来遍历变量的所有取值取合法的最小值,然而听说数据太弱*元全0就可以就水过去吧.... #include <iostream ...

  3. poj 1681 Painter&amp&semi;&num;39&semi;s Problem&lpar;高斯消元&rpar;

    id=1681">http://poj.org/problem? id=1681 求最少经过的步数使得输入的矩阵全变为y. 思路:高斯消元求出*变元.然后枚举*变元,求出最优值. ...

  4. POJ 1681 Painter&&num;39&semi;s Problem 【高斯消元 二进制枚举】

    任意门:http://poj.org/problem?id=1681 Painter's Problem Time Limit: 1000MS   Memory Limit: 10000K Total ...

  5. POJ 1681 Painter&&num;39&semi;s Problem(高斯消元&plus;枚举*变元)

    http://poj.org/problem?id=1681 题意:有一块只有黄白颜色的n*n的板子,每次刷一块格子时,上下左右都会改变颜色,求最少刷几次可以使得全部变成黄色. 思路: 这道题目也就是 ...

  6. POJ - 1681: Painter&&num;39&semi;s Problem (开关问题-高斯消元)

    pro:开关问题,同上一题. 不过只要求输出最小的操作步数,无法完成输出“inf” sol:高斯消元的解对应的一组合法的最小操作步数. #include<bits/stdc++.h> #d ...

  7. POJ 1681---Painter&&num;39&semi;s Problem(高斯消元)

    POJ   1681---Painter's Problem(高斯消元) Description There is a square wall which is made of n*n small s ...

  8. Problem A&colon; Apple&lpar;高斯消元&rpar;

    可以发现具有非常多的方程, 然后高斯消元就能85分 然而我们发现这些方程组成了一些环, 我们仅仅设出一部分变量即可获得N个方程, 就可以A了 trick 合并方程 #include <cstdi ...

  9. HDU 4818 RP problem (高斯消元, 2013年长春区域赛F题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4818 深深地补一个坑~~~ 现场赛坑在这题了,TAT.... 今天把代码改了下,过掉了,TAT 很明显 ...

  10. 高斯消元 分析 &amp&semi;&amp&semi; 模板 (转载)

    转载自:http://hi.baidu.com/czyuan_acm/item/dce4e6f8a8c45f13d7ff8cda czyuan 先上模板: /* 用于求整数解得方程组. */ #inc ...

随机推荐

  1. 关于&lbrack;super dealloc&rsqb;

    销毁一个对象时,需要重写系统的dealloc方法来释放当前类所拥有的对象,在dealloc方法中需要先释放当前类中所有的对象,然后再调用[super dealloc]释放父类中所拥有的对象.如先调用[ ...

  2. ionic实现双击返回键退出功能

    实现这个功能需要四个步骤: 步骤一: 说明:因为需要和手机的硬件(返回按钮)打交道,而ionic本身是不具备该功能的,但是有一个东西可以:ng-cordova插件,这个插件是phoneGap为了能让i ...

  3. protobuf 文件级别优化

    package IM.BaseDefine;option java_package = "com.mogujie.tt.protobuf";option optimize_for ...

  4. vs2013 error c4996&colon; &&num;39&semi;fopen&&num;39&semi;&colon; This function or varia

    做opencv练习时,使用vs2013遇到如下错误: ​错误1error C4996: 'fopen': This function or variable may be unsafe. Consid ...

  5. poj 1195 Mobile phones&lpar;二维树状数组&rpar;

    树状数组支持两种操作: Add(x, d)操作:   让a[x]增加d. Query(L,R): 计算 a[L]+a[L+1]……a[R]. 当要频繁的对数组元素进行修改,同时又要频繁的查询数组内任一 ...

  6. 条件阻塞Condition的应用

    Condition的功能类似在传统线程技术中的Object.wait和Object.notity的功能.   例子:生产者与消费者 import java.util.Random; import ja ...

  7. winform调用WCF默认实例

    一:截图 二:调用代码 using System; using System.Collections.Generic; using System.ComponentModel; using Syste ...

  8. Qt5-控件-QMenu&comma;QMenuBar-菜单栏详解-菜单热键-菜单校验功能

    #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QMenu> #inclu ...

  9. iOS NSDictionary、NSData、JSON等 数据类型相互转换

    1.NSDictionary类型转换为NSData类型: NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys: @&qu ...

  10. node中的get请求和post请求的不同操作【node学习第五篇】

    获取get的请求内容 /** * Created by Administrator on 2016/8/5. */ var http = require("http"); var ...