POJ   1681---Painter's Problem(高斯消元)

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

  1. 2
  2. 3
  3. yyy
  4. yyy
  5. yyy
  6. 5
  7. wwwww
  8. wwwww
  9. wwwww
  10. wwwww
  11. wwwww

Sample Output

  1. 0
  2. 15

Source

 
代码如下:
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <cstdio>
  4. #include <cstring>
  5. #include <cmath>
  6. using namespace std;
  7. const int maxn = ;
  8.  
  9. int equ,var;
  10. int a[maxn][maxn];
  11. int x[maxn]; // 解集.
  12. int pos[maxn];
  13. ///int free_num;
  14.  
  15. void init_a()///对称阵;
  16. {
  17. memset(a,,sizeof(a));
  18. for(int i=;i<var*var;i++)
  19. {
  20. a[i][i]=;
  21. int t=i%var;
  22. if(t>) a[i][i-]=;
  23. if(t<var-) a[i][i+]=;
  24. t=i/var;
  25. if(t>=) a[i][i-var]=;
  26. if(t<var-) a[i][i+var]=;
  27. }
  28. }
  29.  
  30. int Gauss()
  31. {
  32. int i, j, k;
  33. int max_r; // 当前这列绝对值最大的行.
  34. int t=;///记录自由元的个数;
  35. int col = ; /// 当前处理的列.
  36.  
  37. for (k = ; k < equ*equ && col < var*var; k++, col++)
  38. {
  39. max_r = k;
  40. for (i = k + ; i < equ*equ; i++)
  41. {
  42. if (a[i][col] > a[max_r][col])
  43. {
  44. max_r=i;
  45. break;
  46. }
  47. }
  48. if (max_r != k)
  49. {
  50. for (j = k; j < var*var + ; j++) swap(a[k][j], a[max_r][j]);
  51. }
  52. if (a[k][col] == )
  53. {
  54. /// 说明该col列第k行以下全是0了,则处理当前行的下一列.
  55. ///并且应当记录这个自由元;
  56. k--;
  57. pos[t++]=col;
  58. continue;
  59. }
  60. for (i = k + ; i < equ*equ; i++)
  61. {
  62. if (a[i][col] != )
  63. {
  64. for (j = col; j < var*var + ; j++)
  65. {
  66. a[i][j]^=a[k][j];
  67. }
  68. }
  69. }
  70. }
  71. for (i = k; i < equ*equ; i++)
  72. {
  73. // 对于无穷解来说,如果要判断哪些是自由变元,那么初等行变换中的交换就会影响,则要记录交换.
  74. if (a[i][col] != ) return -;
  75. }
  76. return var*var - k;
  77. }
  78.  
  79. int solve(int s)
  80. {
  81. int ans=;
  82. int state=(<<s);
  83. for(int i=;i<state;i++)
  84. {
  85. int cnt=;
  86. memset(x,,sizeof(x));
  87. for(int j=;j<s;j++)
  88. {
  89. if(i&(<<j)) x[pos[j]]=,cnt++;
  90. }
  91. for(int j=var*var-s-;j>=;j--)
  92. {
  93. int f=;
  94. int ss;
  95. int tmp=a[j][var*var];
  96. for(int k=j;k<equ*equ;k++)
  97. {
  98. if(a[j][k]&&f)
  99. {
  100. ss=k;
  101. f=;
  102. }
  103. if(a[j][k]) tmp^=x[k];
  104. }
  105. x[ss]=tmp;
  106. cnt+=x[ss];
  107. }
  108. ans=min(ans,cnt);
  109. }
  110. return ans;
  111. }
  112.  
  113. int main()
  114. {
  115. int T;
  116. scanf("%d",&T);
  117. while(T--)
  118. {
  119. scanf("%d",&equ);
  120. var=equ;
  121. init_a();
  122. for(int i=;i<var*var;i++)
  123. {
  124. char x;
  125. cin>>x;
  126. if(x=='y')
  127. a[i][var*var]=;
  128. else a[i][var*var]=;
  129. }
  130. int v=Gauss();
  131. if(v==-) printf("inf\n");
  132. else cout<<solve(v)<<endl;
  133. }
  134. return ;
  135. }

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

  1. POJ 1681 Painter's Problem (高斯消元)

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

  2. POJ 1681 Painter's Problem [高斯消元XOR]

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

  3. POJ 1681 Painter's Problem 【高斯消元 二进制枚举】

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

  4. POJ 1681 Painter's Problem(高斯消元+枚举自由变元)

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

  5. poj 1681 Painter&#39;s Problem(高斯消元)

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

  6. poj 1681 Painter's Problem

    Painter's Problem 题意:给一个n*n(1 <= n <= 15)具有初始颜色(颜色只有yellow&white两种,即01矩阵)的square染色,每次对一个方格 ...

  7. POJ 1222【异或高斯消元|二进制状态枚举】

    题目链接:[http://poj.org/problem?id=1222] 题意:Light Out,给出一个5 * 6的0,1矩阵,0表示灯熄灭,反之为灯亮.输出一种方案,使得所有的等都被熄灭. 题 ...

  8. POJ 2947 Widget Factory(高斯消元)

    Description The widget factory produces several different kinds of widgets. Each widget is carefully ...

  9. POJ 1830 开关问题(高斯消元)题解

    思路:乍一看好像和线性代数没什么关系.我们用一个数组B表示第i个位置的灯变了没有,然后假设我用u[i] = 1表示动开关i,mp[i][j] = 1表示动了i之后j也会跟着动,那么第i个开关的最终状态 ...

  10. POJ 1830 开关问题(高斯消元求解的情况)

    开关问题 Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 8714   Accepted: 3424 Description ...

随机推荐

  1. Rxlifecycle(三):坑

    坑1 Observable.just("hello world!") .compose(this.<String>bindUntilEvent(ActivityEven ...

  2. HTML5+javascript实现图片加载进度动画效果

    在网上找资料的时候,看到网上有图片加载进度的效果,手痒就自己也写了一个. 图片加载完后,隐藏loading效果. 想看加载效果,请ctrel+F5强制刷新或者清理缓存. 效果预览:   0%   // ...

  3. RestTemplate 使用总结

    场景: 认证服务器需要有个 http client 把前端发来的请求转发到 backend service, 然后把 backend service 的结果再返回给前端,服务器本身只做认证功能. 遇到 ...

  4. asp.net添加验证码

    1.新建一个aspx页面生成验证码图像 using System; using System.Data; using System.Configuration; using System.Collec ...

  5. Android界面布局基本属性

    在 android 中我们常用的布局方式有这么几种:1.LinearLayout ( 线性布局 ) :(里面只可以有一个控件,并且不能设计这个控件的位置,控件会放到左上角)              ...

  6. python 字符串复制

    通过变量来进行赋值 fstr = 'strcpy'sstr = fstrfstr = 'strcpy2'print sstr

  7. ruby 中文字符to_json后乱码(unicode)

    今天遇到一个中文to_json问题 text = "第1章 青豆 不要被外表骗了" text.to_json => "\"\\u7b2c1\\u7ae0 ...

  8. Web离线存储的几种方式

    随着HTML5的正式定稿,我们也可以大量使用HTML离线网络应用程序的特性. #1.Application Cache Application Cache 可以很简单让我们的WebApp具有离线的能力 ...

  9. EF Attach时已存在的处理方式

    如果我们在先前的步骤中读取过数据,如 var list = db.Model.ToList(); 之后再,附加 var o = new Model { Id = 1 }; db.Model.Attac ...

  10. 主机访问虚拟机中linux上的web服务

    环境:主机windows xp 虚拟机centos 6.4 [root@localhost /]# iptables -I INPUT -p tcp --dport 80 -j ACCEPT[root ...