题目描述:

和那道关灯差不多,求最少涂几次。

题解:

高消,然后深搜枚举自由元更新答案。

貌似这道题没卡贪心但是其他题基本都卡了。

比如$Usaco09Nov$的$lights$

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = ;
int t,n,dx[]={-,,,},dy[]={,,,-};
char mp[N][N];
bool check(int x,int y)
{
return x>=&&x<=n&&y>=&&y<=n;
}
int _id(int x,int y)
{
return (x-)*n+y;
}
int a[N*N][N*N],ans;
void gs()
{
for(int l1=;l1<=n*n;l1++)
{
int tmp = l1;
while(tmp<=n*n&&!a[tmp][l1])tmp++;
if(tmp>n*n)continue;
if(tmp!=l1)
for(int i=l1;i<=n*n+;i++)swap(a[tmp][i],a[l1][i]);
for(int i=;i<=n*n;i++)if(a[i][l1]&&i!=l1)
for(int j=l1;j<=n*n+;j++)a[i][j]^=a[l1][j];
}
}
void dfs(int dep,int now)
{
if(now>=ans)return ;
if(!dep){ans=now;return ;}
if(a[dep][dep])
{
dfs(dep-,now+a[dep][n*n+]);
}else
{
if(a[dep][n*n+])return ;
dfs(dep-,now);
for(int i=dep-;i>=;i--)if(a[i][dep])
a[i][n*n+]^=;
dfs(dep-,now+);
for(int i=dep-;i>=;i--)if(a[i][dep])
a[i][n*n+]^=;
}
}
int main()
{
scanf("%d",&t);
while(t--)
{
memset(a,,sizeof(a));
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%s",mp[i]+);
for(int i=;i<=n;i++)for(int j=;j<=n;j++)
{
int u = _id(i,j);
a[u][u]=;
for(int k=;k<;k++)
{
int x = i+dx[k],y = j+dy[k];
if(check(x,y))
a[u][_id(x,y)]=;
}
a[u][n*n+]=(mp[i][j]=='w');
}
gs();
ans=0x3f3f3f3f;
dfs(n*n,);
if(ans==0x3f3f3f3f)puts("inf");
else printf("%d\n",ans);
}
return ;
}

poj1681 Painter's Problem的更多相关文章

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

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

  2. [Gauss]POJ1681 Painter's Problem

    和POJ1222(分析)完全相同 题意也类似, 可以涂自己以及上下左右五个位置的颜色 问几次能全部涂色 不能输出inf 01方程组 用异或来求解就好了 ][]; // 增广矩阵 ]; // 解 ]; ...

  3. poj1681 Painter's Problem(高斯消元法,染色问题)

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

  4. POJ1681 Painter's Problem(高斯消元)

    题目看似与线性方程组无关,但可以通过建模转化为线性方程组的问题. 对于一块砖,刷两次是没有必要的,我们令x=1表示刷了一次,x=0没有刷,一共有n*n个,所以相当于有n*n个未知量x. 定义aij表示 ...

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

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

  6. poj 1681 Painter's Problem

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

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

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

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

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

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

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

随机推荐

  1. githunb竟然可以识别MQ4

    完全是上班时候突发奇想,没想到能识别.只是没有现成的gitignore

  2. C++结构体的应用_YCOJ

    结构体是一种自定义的东西,用struct来定义.在他里面, 可以装许多东西,比如int,string,char,bool等等等等. 如: struct a{ string name; int a; i ...

  3. CF767E ChangeFree【贪心/优先队列】By cellur925

    题目传送门 $naive$想法 最开始的一个贪心策略是每次尽量花掉硬币 ,如果不满足条件,就花纸币.而且不满足条件的时候,要尽量向百取整.(显然是不对的,因为有时候不够)但是显然这个贪心策略是错误的, ...

  4. oauth2(spring security)报错method_not_allowed(Request method 'GET' not supported)解决方法

    报错信息 <MethodNotAllowed> <error>method_not_allowed</error> <error_description> ...

  5. 用sublime text3 直接编译C/C++,java

    首先你得下载好 这是我之前安装codeblocks时留下的里面有cpp,c++,gcc,g++. 第二步就是建立环境变量 这三个配置完成就ok 了 然后进入sublime text 3中,找到工具(t ...

  6. [NewTrain 10][poj 2329]Nearest Number - 2

    题面: http://poj.org/problem?id=2329 题解: 这题有很多做法 1. 搜索 复杂度$O(n^4)$ 但是实际上远远达不到这个复杂度 所以可以通过 2. 对于每一个格子,我 ...

  7. Android中ProgressBar显示小数的方法

    Android原生的ProgressBar的ProgressDialog.STYLE_HORIZONTAL(即水平样式)默认setMax和setProgress只能传int型的参数,而实际项目中我需要 ...

  8. [已读]图解CSS3核心技术与案例实战

    买的时候犹豫了好久,也征询了下几个前端朋友.我一直蛮怕买华章的书,好在这本内容很不错,买得值了. 大漠的css功底很深厚,这本书也很厚= =,读完之后对css圆角以及background-origin ...

  9. Solr打分排序规则自定义【转】

    在搭建好solrCloud搜索集群后,通过编写基本的查询显示语句已经能够通过输入关键字查询到相应结果进行显示,但是在显示结果排序上以及不相关信息过滤问题上,如何制定合理的打分规则得到理想的结果集确实比 ...

  10. 小程序java解密

    <dependency> <groupId>org.bouncycastle</groupId> <artifactId>bcprov-jdk16< ...