poj1681 Painter's Problem
题目描述:
和那道关灯差不多,求最少涂几次。
题解:
高消,然后深搜枚举自由元更新答案。
貌似这道题没卡贪心但是其他题基本都卡了。
比如$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的更多相关文章
- [POJ1681]Painter's Problem(高斯消元,异或方程组,状压枚举)
题目链接:http://poj.org/problem?id=1681 题意:还是翻格子的题,但是这里有可能出现自由变元,这时候枚举一下就行..(其实这题直接状压枚举就行) /* ━━━━━┒ギリギリ ...
- [Gauss]POJ1681 Painter's Problem
和POJ1222(分析)完全相同 题意也类似, 可以涂自己以及上下左右五个位置的颜色 问几次能全部涂色 不能输出inf 01方程组 用异或来求解就好了 ][]; // 增广矩阵 ]; // 解 ]; ...
- poj1681 Painter's Problem(高斯消元法,染色问题)
题意: 一个n*n 的木板 ,每个格子 都 可以 染成 白色和黄色,( 一旦我们对也个格子染色 ,他的上下左右都将改变颜色): 给定一个初始状态 , 求将 所有的 格子 染成黄色 最少需要染几次? ...
- POJ1681 Painter's Problem(高斯消元)
题目看似与线性方程组无关,但可以通过建模转化为线性方程组的问题. 对于一块砖,刷两次是没有必要的,我们令x=1表示刷了一次,x=0没有刷,一共有n*n个,所以相当于有n*n个未知量x. 定义aij表示 ...
- Painter's Problem poj1681 高斯消元法
Painter's Problem Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4420 Accepted: 2143 ...
- poj 1681 Painter's Problem
Painter's Problem 题意:给一个n*n(1 <= n <= 15)具有初始颜色(颜色只有yellow&white两种,即01矩阵)的square染色,每次对一个方格 ...
- POJ 1681 Painter's Problem 【高斯消元 二进制枚举】
任意门:http://poj.org/problem?id=1681 Painter's Problem Time Limit: 1000MS Memory Limit: 10000K Total ...
- OpenJudge 2813 画家问题 / Poj 1681 Painter's Problem
1.链接地址: http://bailian.openjudge.cn/practice/2813 http://poj.org/problem?id=1681 2.题目: 总时间限制: 1000ms ...
- Painter's Problem (高斯消元)
There is a square wall which is made of n*n small square bricks. Some bricks are white while some br ...
随机推荐
- 《高性能iOS 应用开发》之降低你 APP 的电量消耗
在编写高性能 代码时, 电量消耗是一个需要重点处理的重要因素, 就执行时间和 CPU 资源的利用而言, 我们不仅要实现高效的数据结构和算法, 还需要考虑其他的因素,如果某个应用是个电池黑洞,那么一定不 ...
- Codeforces627A【数学·有意思】
题意: 求有多少对( a , b ),a+b=s,a^b=x.(正整数) 思路: a+b=a^b+a&b*2; #include <iostream> #include <c ...
- POJ3186【区间DP】
题意: 每次只能取两端,然后第 i 次取要val[ i ]*i,求一个最大值 一切都是错觉[读者省略此段] 这道题目一开始想的就是记忆化搜索,然后太天真了?好像是,一开始用一维dp[ i ]直接代表一 ...
- python __builtins__ list类 (42)
42.'list', 转换为列表类型 class list(object) | list() -> new empty list | list(iterable) -> new list ...
- P2024 [NOI2001]食物链
第一种说法是"1 X Y",表示 X 和 Y 是同类. 第二种说法是"2 X Y",表示 X 吃 Y . 根据这句话 可以看出来这是一个种类并查集 种类并查集 ...
- vue移动端开发全家桶
一句命令搞定全家桶: npm install vue-router vue-resource vuex --save main.js配置: import Vue from 'vue' impor ...
- h5-35-ajax轮询实现推送效果
data.txt { "number1":1200, } index.html <!DOCTYPE html> <html> <head> &l ...
- Webform 内置对象2(Session、Application)、Repeater的Command操作
内置对象: 1.Session:跟Cookies一样用来存储用户数据,但保存位置不同,保存在服务器内存上 每一台电脑访问服务器,都会是独立的一套session,key值都一样,但是内容都是不一样的 S ...
- MySQL读写分离实现
数据库写入效率要低于读取效率,一般系统中数据读取频率高于写入频率,单个数据库实例在写入的时候会影响读取性能,这是做读写分离的原因.实现方式主要基于mysql的主从复制,通过路由的方式使应用对数据库的写 ...
- 获取页面URL两种方式
以请求http://localhost:8080/doctor/demo?code=1为例 一:用java代码获取 //获取URL中的请求参数.即?后的条件 code=1 String querySt ...