[Gauss]POJ1681 Painter's Problem
和POJ1222(分析)完全相同
题意也类似, 可以涂自己以及上下左右五个位置的颜色
问几次能全部涂色 不能输出inf
01方程组 用异或来求解就好了
int a[][]; // 增广矩阵
int x[]; // 解
int free_x[]; // 标记是否为自由未知量 int n;
void debug()
{
for(int i0=;i0<n*n;i0++)
{
for(int j0=;j0<n*n;j0++)
printf("%d ", a[i0][j0]);
printf("\n");
}
} int Gauss(int n, int m) // n个方程 m个未知数 即 n行m+1列
{
//转换为阶梯形式
int col=, k, num=;
for(k=;k<n && col<m;k++, col++)
{//枚举行
int max_r=k;
for(int i=k+;i<n;i++)//找到第col列元素绝对值最大的那行与第k行交换
if(abs(a[i][col])>abs(a[max_r][col]))
max_r=i;
if(max_r!=k)// 与第k行交换
for(int j=col;j<m+;j++)
swap(a[k][j], a[max_r][j]);
if(!a[k][col])// 说明该col列第k行以下全是0了
{
k--;
free_x[num++]=col;
continue;
}
for(int i=k+;i<n;i++)// 枚举要删除的行
if(a[i][col])
for(int j=col;j<m+;j++)
a[i][j]^=a[k][j];
} // debug();
// printf("%d %d\n", col, k); for(int i=k;i<n;i++)
if(a[i][col])
return -; // 无解 // if(k<m) //m-k为自由未知量个数
// {
int stat=<<(m-k);
int ans=INT_MAX;
for(int i=;i<stat;i++)
{
int cnt=;
for(int j=;j<m-k;j++)
if(i&(<<j))
{
x[free_x[j]]=;
cnt++;
}
else
x[free_x[j]]=;
for(int j=k-;j>=;j--)
{
int tmp;
for(tmp=j;tmp<m;tmp++)
if(a[j][tmp])
break;
x[tmp]=a[j][m];
for(int l=tmp+;l<m;l++)
if(a[j][l])
x[tmp]^=x[l];
cnt+=x[tmp];
}
if(cnt<ans)
ans=cnt;
}
return ans;
// }
//
// // 唯一解 回代
// for(int i=m-1;i>=0;i--)
// {
// x[i]=a[i][m];
// for(int j=i+1;j<m;j++)
// x[i]^=(a[i][j] && x[j]);
// }
// int ans=0;
// for(int i=0;i<n*n;i++)
// ans+=x[i];
// return ans;
} void init()
{
memset(a, , sizeof(a));
memset(x, , sizeof(x));
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]=;
}
} int main()
{
int t;
scanf("%d", &t);
while(t--)
{
scanf("%d", &n);
init();
for(int i=;i<n;i++)
for(int j=;j<n;j++)
{
char ch;
cin>>ch;
a[i*n+j][n*n]=(ch=='w');
}
int t=Gauss(n*n, n*n);
if(t==-)
{
printf("inf\n");
continue ;
}
printf("%d\n", t);
}
return ;
}
POJ 1681
注意 对于无穷解的情况, 初等行变换中的交换会影响判断哪些是自由未知量, 那么就要记录交换
[Gauss]POJ1681 Painter's Problem的更多相关文章
- [POJ1681]Painter's Problem(高斯消元,异或方程组,状压枚举)
题目链接:http://poj.org/problem?id=1681 题意:还是翻格子的题,但是这里有可能出现自由变元,这时候枚举一下就行..(其实这题直接状压枚举就行) /* ━━━━━┒ギリギリ ...
- poj1681 Painter's Problem(高斯消元法,染色问题)
题意: 一个n*n 的木板 ,每个格子 都 可以 染成 白色和黄色,( 一旦我们对也个格子染色 ,他的上下左右都将改变颜色): 给定一个初始状态 , 求将 所有的 格子 染成黄色 最少需要染几次? ...
- POJ1681 Painter's Problem(高斯消元)
题目看似与线性方程组无关,但可以通过建模转化为线性方程组的问题. 对于一块砖,刷两次是没有必要的,我们令x=1表示刷了一次,x=0没有刷,一共有n*n个,所以相当于有n*n个未知量x. 定义aij表示 ...
- poj1681 Painter's Problem
题目描述: 和那道关灯差不多,求最少涂几次. 题解: 高消,然后深搜枚举自由元更新答案. 貌似这道题没卡贪心但是其他题基本都卡了. 比如$Usaco09Nov$的$lights$ 代码: #inclu ...
- Painter's Problem poj1681 高斯消元法
Painter's Problem Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4420 Accepted: 2143 ...
- POJ 1681 Painter's Problem 【高斯消元 二进制枚举】
任意门:http://poj.org/problem?id=1681 Painter's Problem Time Limit: 1000MS Memory Limit: 10000K Total ...
- poj 1681 Painter's Problem
Painter's Problem 题意:给一个n*n(1 <= n <= 15)具有初始颜色(颜色只有yellow&white两种,即01矩阵)的square染色,每次对一个方格 ...
- Painter's Problem (高斯消元)
There is a square wall which is made of n*n small square bricks. Some bricks are white while some br ...
- POJ 1681 Painter's Problem(高斯消元+枚举自由变元)
http://poj.org/problem?id=1681 题意:有一块只有黄白颜色的n*n的板子,每次刷一块格子时,上下左右都会改变颜色,求最少刷几次可以使得全部变成黄色. 思路: 这道题目也就是 ...
随机推荐
- 安装Numpy和matplotlib
(1)测试程序 这是我从网上(http://www.open-open.com/lib/view/open1393488232380.html)找到的一个使用Numpy和matplotlib的 ...
- for循环例题
1· 一对幼兔一个月后长成小兔(每对兔子默认一公一母),再过一个月长成成兔并且生下一对小兔,以此类推,两年后有多少对兔子? Console.Write("输入年:"); ...
- 测试中的几个case
一.页面上对引起 大量数据提交的 按钮/链接 点击一次后, disable 需求: 对于重要的表单.数量庞大/响应慢的系统,在做提交时, 又有页面还在loading状态, 此时连续做两次点击, 经常 ...
- tomcat安装不成功-提示找不到JAVA虚拟机
今天重装tomcate,但是总是提示找不到java虚拟机,但是我明明装了jre和jdk,太烦人了 后来搜了各种方法,终于找到了解决方法,现在和大家分享下 到提示找java虚拟机那一步的时候,选择到jr ...
- c#解析Josn(解析多个子集,数据,可解析无限级json)
首先引用 解析类库 using System; using System.Collections.Generic; using System.Linq; using System.Text; name ...
- 转:ASP.NET MVC 3 and App_Code folder
问题: In ASP.NET Webform, App_Code is standardfolder to putting code and using it at run-time.But I th ...
- gradle使用国内源
// 设置 maven 库地址 repositories { maven { url 'http://maven.oschina.net/content/groups/public/' } } ...
- poj代码搬家啦啦啦
我的poj代码搬家啦,大家想看可以到 blog.csdn.net/michaelysm 来看.欢迎哦
- 在Mac OS X中搭建STM32开发环境(2)
本文原创于http://www.cnblogs.com/humaoxiao,非法转载者请自重! 在上一篇文章中,我们在OSX中编译了ST-Link2调试工具,并且简单的对其功能进行了测试 ...
- QML鼠标区域控制
鼠标操作使用很多,下面给出一个示例: import QtQuick 2.4 import QtQuick.Controls 1.3 import QtQuick.Window 2.2 import Q ...