poj1681 高斯消元
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 5352 | Accepted: 2588 |
Description
Input
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
Sample Input
2
3
yyy
yyy
yyy
5
wwwww
wwwww
wwwww
wwwww
wwwww
Sample Output
0
15
题意:
一个n*n 的木板,每个格子都可以染成白色和黄色,一旦我们对某个格子染色,其上下左右都将改变颜色
求将所有的格子染成黄色最少需要几次,若不能则输出inf。
当有多个自由变元时,需要进行枚举求出最小值
/*
poj1681
类似于一元开关问题。把二维矩阵转换成一维矩阵来看。
在求出答案之后,对于有很多解的考虑枚举自由变元来求解
最小值
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
typedef long double ld; using namespace std;
const int maxn = ; int equ,var;
int a[maxn][maxn];
int x[maxn];
int free_x[maxn];
int free_num; int Gauss()
{
int max_r,col,k;
free_num = ;
for(k = ,col = ; k < equ && col < var; k++,col++)
{
max_r = k;
for(int i = k+; i < equ; i++)
{
if(abs(a[i][col]) > abs(a[max_r][col]))
max_r = i;
}
if(a[max_r][col] == )
{
k --;
free_x[free_num++] = col;
continue;
}
if(max_r != k)
{
for(int j = col; j < var+; j++)
swap(a[k][j],a[max_r][j]); }
for(int i = k + ; i < equ; i++)
{
if(a[i][col] != )
{
for(int j = col; j < var+; j++)
a[i][j] ^= a[k][j];
}
} }
for(int i = k; i < equ; i++)
if(a[i][col] != )
return -;
if(k < var) return var-k; for(int i = var-; i >= ; i--)
{
x[i] = a[i][var];
for(int j = i +; j < var; j++)
x[i] ^= (a[i][j] && x[j]); }
return ; } int n;
void ini()
{
memset(a,,sizeof(a));
memset(x,,sizeof(x));
equ = n*n;
var = n*n;
for(int i = ;i < n;i++)
{
for(int j = ;j < n;j++)
{
int tt = i*n+ j;
a[tt][tt] =;
if(i > ) a[(i-)*n+j][tt] = ;
if(i < n-) a[(i+)*n+j][tt] = ;
if(j > ) a[tt-][tt] = ;
if(j < n-) a[tt+][tt] =;
}
}
} char str[][];
/*
2
3
yyy
yyy
yyy
5
wwwww
wwwww
wwwww
wwwww
wwwww
*/
int main()
{
int T;
char color;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
ini();
for(int i = ; i < n; i++)
{
scanf("%s",str[i]);
for(int j = ; j < n; j++)
{
if(str[i][j] == 'y')
a[i*n+j][n*n] = ;
else
a[i*n+j][n*n] = ;
}
}
int t = Gauss();
if(t == -)
{
printf("inf\n");
}
else if(t == )
{
int ans = ;
for(int i = ; i < n*n; i++)
ans += x[i];
printf("%d\n",ans);
}
else
{
int ans = 0x3f3f3f3f;
int tot = ( << t);
for(int i = ; i < tot; i++)
{
int cnt = ;
for(int j = ; j < t; j++)
{
if(i & ( << j))
{
cnt ++;
x[free_x[j]]= ;
}
else x[free_x[j]]= ;
} for(int j = var-t-; j >= ; j--)
{
int dex;
for(dex = j; dex < var; dex++)
if(a[j][dex])
break;
x[dex] = a[j][var];
for(int l = dex +; l <var ; l++)
{
if(a[j][l])
x[dex] ^= x[l];
}
cnt += x[dex];
}
ans = min(ans,cnt);
}
printf("%d\n",ans);
}
}
return ;
}
poj1681 高斯消元的更多相关文章
- 高斯消元几道入门题总结POJ1222&&POJ1681&&POJ1830&&POJ2065&&POJ3185
最近在搞高斯消元,反正这些题要么是我击败了它们,要么就是这些题把我给击败了.现在高斯消元专题部分还有很多题,先把几道很简单的入门题总结一下吧. 专题:http://acm.hust.edu.cn/vj ...
- POJ 1222 POJ 1830 POJ 1681 POJ 1753 POJ 3185 高斯消元求解一类开关问题
http://poj.org/problem?id=1222 http://poj.org/problem?id=1830 http://poj.org/problem?id=1681 http:// ...
- 【BZOJ-3143】游走 高斯消元 + 概率期望
3143: [Hnoi2013]游走 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2264 Solved: 987[Submit][Status] ...
- 【BZOJ-3270】博物馆 高斯消元 + 概率期望
3270: 博物馆 Time Limit: 30 Sec Memory Limit: 128 MBSubmit: 292 Solved: 158[Submit][Status][Discuss] ...
- *POJ 1222 高斯消元
EXTENDED LIGHTS OUT Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9612 Accepted: 62 ...
- [bzoj1013][JSOI2008][球形空间产生器sphere] (高斯消元)
Description 有一个球形空间产生器能够在n维空间中产生一个坚硬的球体.现在,你被困在了这个n维球体中,你只知道球 面上n+1个点的坐标,你需要以最快的速度确定这个n维球体的球心坐标,以便于摧 ...
- hihoCoder 1196 高斯消元·二
Description 一个黑白网格,点一次会改变这个以及与其连通的其他方格的颜色,求最少点击次数使得所有全部变成黑色. Sol 高斯消元解异或方程组. 先建立一个方程组. \(x_i\) 表示这个点 ...
- BZOJ 2844 albus就是要第一个出场 ——高斯消元 线性基
[题目分析] 高斯消元求线性基. 题目本身不难,但是两种维护线性基的方法引起了我的思考. void gauss(){ k=n; F(i,1,n){ F(j,i+1,n) if (a[j]>a[i ...
- SPOJ HIGH Highways ——Matrix-Tree定理 高斯消元
[题目分析] Matrix-Tree定理+高斯消元 求矩阵行列式的值,就可以得到生成树的个数. 至于证明,可以去看Vflea King(炸树狂魔)的博客 [代码] #include <cmath ...
随机推荐
- oc中protocol、category和继承的区别
OC中protocol.category和继承的区别以前还是有点迷糊,面试的时候说的有点混乱,现在结合一些资料总结一下. 利用继承,多态是一个很好的保持"对扩展开放.对更改封闭"( ...
- linux系统增加开机启动服务/应用
操作 在/etc/init.d下新建示例脚本文件(customize.sh),该脚本会启动zookeeper服务.内容如下: #!/bin/sh /usr/local/zookeeper-/bin/z ...
- Spark性能优化总结
1. 避免重复加载RDD 比如一份从HDFS中加载的数据 val rdd1 = sc.textFile("hdfs://url:port/test.txt"),这个test.txt ...
- python之路--day11---迭代器和生成器
迭代: 迭代是一个重复的过程,每次重复即一次迭代,并且每次迭代的结果都是下一次迭代的初始值 为什么要有迭代器: 数据类型的取值,字符串,列表,元组依靠索引可以取值,但是字典,集合,文件这些数据类型无法 ...
- ViurtualBox配置虚拟机Linux的网络环境
之前可以使用VMware配置成功,让虚拟机和本地通信,虚拟机可以访问外网,但是VMware体积太大了,最后终于把virtualBox也配置成功,也使得两者兼备 环境:本地windows7 64位专业版 ...
- EasyUI 修改 Messager 消息框大小
需求是要修改确认消息窗口的大小. 简单的调用方法是这样的: $.messager.confirm('操作确认', '确定批量编辑文章?', function (r) { ... } 这个时候生成的弹窗 ...
- python 编码规范整理
PEP8 Python 编码规范 一 代码编排1 缩进.4个空格的缩进(编辑器都可以完成此功能),不要使用Tap,更不能混合使用Tap和空格.2 每行最大长度79,换行可以使用反斜杠,最好使用圆括号. ...
- 初学者如何查阅自然语言处理(NLP)领域学术资料
1. 国际学术组织.学术会议与学术论文 自然语言处理(natural language processing,NLP)在很大程度上与计算语言学(computational linguistics,CL ...
- java对象转字节数组,获取泛型类
对象转字节数组,字节数组在恢复成对象 Test.java class Test { public static void main(String args[]) throws IOException, ...
- MySQL8.0 原子DDL
Edit MySQL8.0 原子DDL 简介 MySQL8.0 开始支持原子 DDL(atomic DDL),数据字典的更新,存储引擎操作,写二进制日志结合成了一个事务.在没有原子DDL之前,DROP ...