Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 5378   Accepted: 2601

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

  0

15

题解:

   构造矩阵高斯消元后可以得到一组解,但是题目中要求的是求出最小染色次数。所以要对其中不确定的方案进行枚举。

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstdlib>
  4. #include<cmath>
  5. #include<algorithm>
  6. #include<cstring>
  7. #include<queue>
  8. #include<vector>
  9. using namespace std;
  10. int T,N,ANS;
  11. int a[][];
  12. bool gauss(){
  13. int now=;
  14. for(int i=;i<=N*N;i++){
  15. int to=now;
  16. while(to<=N*N&&a[to][i]==) to++;
  17. if(to>N*N) continue;
  18. if(to!=now){
  19. for(int j=;j<=N*N+;j++) swap(a[to][j],a[now][j]);
  20. }
  21. for(int j=;j<=N*N;j++){
  22. if(j!=now&&a[j][i]){
  23. for(int k=;k<=N*N+;k++){
  24. a[j][k]^=a[i][k];
  25. }
  26. }
  27. }
  28. now++;
  29. }
  30. for(int i=now;i<=N*N;i++)
  31. if(a[i][N*N+]!=) return false;
  32. return true;
  33. }
  34.  
  35. int v[],cnt;
  36. void dfs(int x){
  37. if(cnt>=ANS) return ;//已经比目前的答案大了,没有必要再搜
  38. if(x==){
  39. ANS=min(cnt,ANS);
  40. return ;
  41. }
  42. if(a[x][x]!=){
  43. int num=a[x][N*N+];//num表示第x块砖染色不染色
  44. for(int i=x+;i<=N*N;i++){
  45. if(a[x][i]!=) num=num^v[i];//已经枚举过的x+1~N*N中某块砖如果可以对x产生影响且已染色,就让num改变一次
  46. }
  47. v[x]=num;
  48. if(num==) cnt++;
  49. dfs(x-);
  50. if(num==) cnt--;
  51. }
  52. else{//枚举按或不按两种情况
  53. v[x]=; dfs(x-);
  54. v[x]=; cnt++; dfs(x-); cnt--;
  55. }
  56. }
  57.  
  58. int main(){
  59. scanf("%d",&T);
  60. while(T--){
  61. memset(a,,sizeof(a));
  62. scanf("%d",&N);
  63. for(int i=;i<=N*N;i++){
  64. a[i][i]=;
  65. if(i%N!=) a[i][i-]=;
  66. if(i%N!=) a[i][i+]=;
  67. if(i>=N+) a[i][i-N]=;
  68. if(i<=N*(N-)) a[i][i+N]=;
  69. }
  70. for(int i=;i<=N;i++){
  71. char s[];
  72. scanf("%s",s+);
  73. for(int j=;j<=N;j++){
  74. if(s[j]=='w') a[(i-)*N+j][N*N+]=;
  75. }
  76. }
  77. if(gauss()==false){
  78. puts("inf");
  79. continue;
  80. }
  81. ANS=<<;
  82. dfs(N*N);
  83. printf("%d\n",ANS);
  84. }
  85. return ;
  86. }

Painter's Problem的更多相关文章

  1. poj 1681 Painter's Problem

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

  2. Painter's Problem poj1681 高斯消元法

    Painter's Problem Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4420   Accepted: 2143 ...

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

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

  4. [POJ1681]Painter's Problem(高斯消元,异或方程组,状压枚举)

    题目链接:http://poj.org/problem?id=1681 题意:还是翻格子的题,但是这里有可能出现自由变元,这时候枚举一下就行..(其实这题直接状压枚举就行) /* ━━━━━┒ギリギリ ...

  5. OpenJudge 2813 画家问题 / Poj 1681 Painter's Problem

    1.链接地址: http://bailian.openjudge.cn/practice/2813 http://poj.org/problem?id=1681 2.题目: 总时间限制: 1000ms ...

  6. Painter's Problem (高斯消元)

    There is a square wall which is made of n*n small square bricks. Some bricks are white while some br ...

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

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

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

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

  9. POJ 1681 Painter's Problem (高斯消元 枚举自由变元求最小的步数)

    题目链接 题意: 一个n*n 的木板 ,每个格子 都 可以 染成 白色和黄色,( 一旦我们对也个格子染色 ,他的上下左右 都将改变颜色): 给定一个初始状态 , 求将 所有的 格子 染成黄色 最少需要 ...

随机推荐

  1. 操作数组可以通过Array这个类来操作(不需要考虑数组的类型!!!)

    这段代码通过Array这个类,把值取出来,存到collection里,不需要考虑数组的类型

  2. Ajax 常用资源

    regular online:http://regex.larsolavtorvik.com/ json online:http://json.cn/ Prototype:http://prototy ...

  3. JUnit4 测试示例

    1. JUnit4 测试示例 // Calculator.java public class Calculator{ public int add(int a, int b){ return a + ...

  4. Python数据分析(一):工具的简单使用

    1.Numpy 安装:pip install numpy [root@kvm work]# cat numpy_test.py #!/usr/bin/env python #coding:utf-8 ...

  5. nginx 哈希表结构图

  6. Mysql索引长度和区分度

    首先  索引长度和区分度是相互矛盾的, 索引长度太短,那么区分度就很低,吧索引长度加长,区分度就高,但是索引也是要占内存的,所以我们需要找到一个平衡点: 那么这个平衡点怎么来定? 比如用户表有个字段 ...

  7. maven仓库配置

    apache官方提供的maven库下载速度比较慢,所以可以配置成aliyun的maven库,这样在构建项目的时候速度会提升很多,具体方法如下: vim /usr/local/maven/conf/se ...

  8. HDU1160:FatMouse's Speed(最长上升子序列,不错的题)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1160 学的东西还是不深入啊,明明会最长上升子序列,可是还是没有A出这题,反而做的一点思路没有,题意就不多说 ...

  9. 用tophat和cufflinks分析RNAseq数据[转载]

    转自:http://blog.sciencenet.cn/home.php?mod=space&uid=635619&do=blog&id=884213 //今天看到一篇非常好 ...

  10. matplotlib显示栅格图片

    参考自Matplotlib Python 画图教程 (莫烦Python)(13)_演讲•公开课_科技_bilibili_哔哩哔哩 https://www.bilibili.com/video/av16 ...