POJ1681高消+搜索

0
0
0
1. 云栖社区>
2. 博客>
3. 正文

## POJ1681高消+搜索

prime7 2012-09-13 20:21:00 浏览830

Painter's Problem
 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 3484 Accepted: 1718

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

```#include <iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int maxn=230;
int a[maxn][maxn+1],x[maxn];//a 是系数矩阵和增广矩阵，x 是最后存放的解
// a[][maxn]中存放的是方程右面的值（bi）
int equ,var;//equ 是系数阵的行数，var 是系数矩阵的列数（变量的个数）
int free_num,ans=100000000;
void Debug(void)
{
int i, j;
for (i = 0; i < equ; i++)
{
for (j = 0; j < var + 1; j++)
{
cout << a[i][j] << " ";
}
cout << endl;
}
cout << endl;
}
//调试输出，看消元后的矩阵值，提交时，就不用了
inline int gcd(int a, int b) //最大公约数
{
int t;
while (b != 0)
{
t = b;
b = a % b;
a = t;
}
return a;
}

inline int lcm(int a, int b) //最小公倍数
{
return a * b / gcd(a, b);
}

int dfs(int p) //枚举自由解，只能取0-1，枚举完就回带，找到最小的
{
if (p<=free_num-1) //深入到了主对角线元素非0 的行了
{
//下面就是回带的代码啊
for(int i = free_num-1; i >= 0; --i)
{
int tmp = a[i][var] % 2;
for(int j = i+1; j < var; ++j) //x[i]取决于x[i+1]--x[var]啊，所以后面的解对前面的解有影
//响。
if(a[i][j] != 0)
tmp = ( tmp - (a[i][j]*x[j])%2 + 2 ) % 2;
x[i] = (tmp/a[i][i]) % 2; //上面的正常解
} //回带完成了
//计算解元素为1 的个数；
int sum=0;
for(int i=0; i<var; i++) sum+=x[i];
if (ans>sum) ans=sum;
return 0;
}
x[p]=0;
dfs(p-1);
x[p]=1;
dfs(p-1);
}
void swap(int &a,int &b)
{
int temp=a;    //交换 2 个数
a=b;
b=temp;
}
int Gauss()
{
int k,col = 0;
//当前处理的列
for(k = 0; k < equ && col < var; ++k,++col)
{
int max_r = k;
for(int i = k+1; i < equ; ++i)
if(a[i][col] > a[max_r][col])
max_r = i;
if(max_r != k)
{
for(int i = k; i < var + 1; ++i)
swap(a[k][i],a[max_r][i]);
}
if(a[k][col] == 0)
{
k--;
continue;
}
for(int i = k+1; i < equ; ++i)
{
if(a[i][col] != 0)
{
int LCM = lcm(a[i][col],a[k][col]);
int ta = LCM/a[i][col], tb = LCM/a[k][col];
if(a[i][col]*a[k][col] < 0)
tb = -tb;
for(int j = col; j < var + 1; ++j)
a[i][j] = ( (a[i][j]*ta)%2 - (a[k][j]*tb)%2 + 2 ) % 2;
// 0 和 1 两种状态
}
}
}
//a[i][j]只有

//上述代码是消元的过程，行消元完成
//解下来 2 行，判断是否无解
//注意 K 的值，k 代表系数矩阵值都为 0 的那些行的第 1 行
for(int i = k; i < equ; ++i)
if(a[i][col] != 0)
return -1;
//Debug();

//唯一解或者无穷解,k<=var
//var-k==0 唯一解；var-k>0 无穷多解，自由解的个数=var-k
//能执行到这，说明肯定有解了，无非是 1 个和无穷解的问题。
//下面这几行很重要，保证秩内每行主元非 0，且按对角线顺序排列，就是检查列
for(int i = 0; i <equ; ++i)//每一行主元素化为非零
if(!a[i][i])
{
int j;
for(j = i+1; j<var; ++j)
if(a[i][j])
break;
if(j == var)
break;
for(int k = 0; k < equ; ++k)
swap(a[k][i],a[k][j]);
}
// ----处理保证对角线主元非 0 且顺序，检查列完成

free_num=k;
if (var-k>0)
{
dfs(var-1);
return ans;
//无穷多解，先枚举解，然后用下面的回带代码进行回带；
//这里省略了下面的回带的代码；不管唯一解和无穷解都可以回带，只不过无穷解
//回带时，默认为最后几个自由变元=0 而已。
}
if(var-k<0)
return -1;
// 无解返回 -1
if (var-k==0)//唯一解时
{
//下面是回带求解代码，当无穷多解时，最后几行为 0 的解默认为 0；
for(int i = k-1; i >= 0; --i) //从消完元矩阵的主对角线非 0 的最后 1 行，开始往
//回带
{
int tmp = a[i][var] % 2;

for(int j = i+1; j < var; ++j) //x[i]取决于 x[i+1]--x[var]啊，所以后面的解对前面的解有影响。
if(a[i][j] != 0)
tmp = ( tmp - (a[i][j]*x[j])%2 + 2 ) % 2;
//if (a[i][i]==0) x[i]=tmp;//最后的空行时，即无穷解得
//else
x[i] = (tmp/a[i][i]) % 2; //上面的正常解
}
int sum=0;
for(int i=0; i<var; i++)
sum+=x[i];
return sum;

//回带结束了
}
}

int main()
{
int t,n;
char map[17][17];
cin>>t;
getchar();
while(t--)
{
memset(a,0,sizeof(a));
memset(x,0,sizeof(x));
ans=100000000;
cin>>n;
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
{
int k=i*n+j;
a[k][k]=1;
if(i>0) a[k][k-n]=1;
if(i<n-1) a[k][k+n]=1;
if(j>0)  a[k][k-1]=1;
if(j<n-1) a[k][k+1]=1;
cin>>map[i][j];
if(map[i][j]=='w')
a[k][n*n]=1;

}
equ=var=n*n;
int j1=Gauss();
// Debug();
if(j1==-1)
cout<<"inf"<<endl;
else
cout<<j1<<endl;

}
return 0;
}
```

prime7
+ 关注