POJ 1681---Painter's Problem(高斯消元)
2016-06-16 18:25
330 查看
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
Sample Output
Source
POJ Monthly--2004.06.27 张嘉龄
代码如下:
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 张嘉龄
代码如下:
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring> #include <cmath> using namespace std; const int maxn = 240; int equ,var; int a[maxn][maxn]; int x[maxn]; // 解集. int pos[maxn]; ///int free_num; void init_a()///对称阵; { memset(a,0,sizeof(a)); for(int i=0;i<var*var;i++) { a[i][i]=1; int t=i%var; if(t>0) a[i][i-1]=1; if(t<var-1) a[i][i+1]=1; t=i/var; if(t>=1) a[i][i-var]=1; if(t<var-1) a[i][i+var]=1; } } int Gauss() { int i, j, k; int max_r; // 当前这列绝对值最大的行. int t=0;///记录自由元的个数; int col = 0; /// 当前处理的列. for (k = 0; k < equ*equ && col < var*var; k++, col++) { max_r = k; for (i = k + 1; i < equ*equ; i++) { if (a[i][col] > a[max_r][col]) { max_r=i; break; } } if (max_r != k) { for (j = k; j < var*var + 1; j++) swap(a[k][j], a[max_r][j]); } if (a[k][col] == 0) { /// 说明该col列第k行以下全是0了,则处理当前行的下一列. ///并且应当记录这个自由元; k--; pos[t++]=col; continue; } for (i = k + 1; i < equ*equ; i++) { if (a[i][col] != 0) { for (j = col; j < var*var + 1; j++) { a[i][j]^=a[k][j]; } } } } for (i = k; i < equ*equ; i++) { // 对于无穷解来说,如果要判断哪些是自由变元,那么初等行变换中的交换就会影响,则要记录交换. if (a[i][col] != 0) return -1; } return var*var - k; } int solve(int s) { int ans=9999999; int state=(1<<s); for(int i=0;i<state;i++) { int cnt=0; memset(x,0,sizeof(x)); for(int j=0;j<s;j++) { if(i&(1<<j)) x[pos[j]]=1,cnt++; } for(int j=var*var-s-1;j>=0;j--) { int f=1; int ss; int tmp=a[j][var*var]; for(int k=j;k<equ*equ;k++) { if(a[j][k]&&f) { ss=k; f=0; } if(a[j][k]) tmp^=x[k]; } x[ss]=tmp; cnt+=x[ss]; } ans=min(ans,cnt); } return ans; } int main() { int T; scanf("%d",&T); while(T--) { scanf("%d",&equ); var=equ; init_a(); for(int i=0;i<var*var;i++) { char x; cin>>x; if(x=='y') a[i][var*var]=0; else a[i][var*var]=1; } int v=Gauss(); if(v==-1) printf("inf\n"); else cout<<solve(v)<<endl; } return 0; }
相关文章推荐
- NSSearchPathForDirectoriesInDomains
- 【MongoDB】2016-06-16T11:00:48.634+0800 warning: Failed to connect to 127.0.0.1:27017, reason: errno:10061 由于目标计算机积极拒绝,无法连接。
- Vivado Implementation - ERROR: [Chipscope 16-119] Implementing debug core dbg_hub failed
- APK Tool的问题:Exception in thread "main" brut.androlib.AndrolibException: Could not decode arsc file
- 【BZOJ2434-[Noi2011]】阿狸的打字机(AC自动机(fail树)+离线+树状数组)
- DDD(Domain Driver Designer) 领域驱动设计简介
- POJ 1681 Painter's Problem(高斯消元)
- 关于部分手机通话录音报start failed的问题
- 【leetcode】172. Factorial Trailing Zeroes
- WSASocket failed 10013 处理
- 打开金蝶AIS文件方法
- Redis redis.clients.jedis.exceptions.JedisDataException: ERR Operation against a key holding the wr
- 转载http://jingyan.baidu.com/article/bad08e1e87138209c8512118.html
- AIDL进程间通讯的一些注意问题
- CDISC SDTM DS domain学习笔记
- oracle查看允许的最大连接数和当前连接数等信息(转自:http://blog.csdn.net/haiross/article/details/41944493)
- 钥匙串KeyChain的使用
- “机器学习”相关资料分享
- tail和head命令
- INS-35172 Target database memory(945MB) excessds the systems available shared memory({0}MB)