poj1753 高斯消元
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 37055 | Accepted: 16125 |
Description
- Choose any one of the 16 pieces.
- Flip the chosen piece and also all adjacent pieces to the left, to the right, to the top, and to the bottom of the chosen piece (if there are any).
Consider the following position as an example:
bwbw
wwww
bbwb
bwwb
Here "b" denotes pieces lying their black side up and "w" denotes pieces lying their white side up. If we choose to flip the 1st piece from the 3rd row (this choice is shown at the picture), then the field will become:
bwbw
bwww
wwwb
wwwb
The goal of the game is to flip either all pieces white side up or all pieces black side up. You are to write a program that will search for the minimum number of rounds needed to achieve this goal.
Input
Output
(without quotes).
Sample Input
bwwb
bbwb
bwwb
bwww
Sample Output
4
思路:
和前面那些问题差不多吧,只是它最后结果要求全黑或者全白,所以算了两次
poj1753
/*
同样的基于二维矩阵的开关问题,只是题目要求最终结果要
全亮或者全黑,于是算两次即可
*/
#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 = 40; int equ,var;
int a[maxn][maxn];
int b[maxn][maxn];
int x[maxn];
int free_x[maxn];
int free_num; int Gauss()
{
int max_r,col,k;
free_num = 0;
for(k = 0,col = 0; k < equ && col < var; k++,col++)
{
max_r = k;
for(int i = k+1; i < equ; i++)
{
if(abs(a[i][col]) > abs(a[max_r][col]))
max_r = i;
}
if(a[max_r][col] == 0)
{
k --;
free_x[free_num++] = col;
continue;
}
if(max_r != k)
{
for(int j = col; j < var+1; j++)
swap(a[k][j],a[max_r][j]); }
for(int i = k + 1; i < equ; i++)
{
if(a[i][col] != 0)
{
for(int j = col; j < var+1; j++)
a[i][j] ^= a[k][j];
}
} }
for(int i = k; i < equ; i++)
if(a[i][col] != 0)
return -1;
if(k < var) return var-k; for(int i = var-1; i >= 0; i--)
{
x[i] = a[i][var];
for(int j = i +1; j < var; j++)
x[i] ^= (a[i][j] && x[j]); }
return 0; } int n;
void ini()
{
memset(a,0,sizeof(a));
memset(x,0,sizeof(x));
equ = n*n;
var = n*n;
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
int tt = i*n+ j;
a[tt][tt] =1;
b[tt][tt] = 1;
if(i > 0) a[(i-1)*n+j][tt] = 1;
if(i < n-1) a[(i+1)*n+j][tt] = 1;
if(j > 0) a[tt-1][tt] = 1;
if(j < n-1) a[tt+1][tt] =1;
}
}
} int solve()
{
int t = Gauss();
if(t == -1)
{
return t;
}
else if(t == 0)
{
int ans = 0;
for(int i = 0; i < n*n; i++)
ans += x[i];
return ans;
}
else
{
int ans = 0x3f3f3f3f;
int tot = (1 << t);
for(int i = 0; i < tot; i++)
{
int cnt = 0;
for(int j = 0; j < t; j++)
{
if(i & (1 << j))
{
cnt ++;
x[free_x[j]]= 1;
}
else x[free_x[j]]= 0;
} for(int j = var-t-1; j >= 0; j--)
{
int dex;
for(dex = j; dex < var; dex++)
if(a[j][dex])
break;
x[dex] = a[j][var];
for(int l = dex +1; l <var ; l++)
{
if(a[j][l])
x[dex] ^= x[l];
}
cnt += x[dex];
}
ans = min(ans,cnt);
}
return ans;
}
} char str[30][30]; int main()
{
int T;
char color;
scanf("%d",&T);
while(scanf("%s",str[0]) !=EOF)
{
n = 4;
ini();
for(int j = 0; j < n; j++)
{
if(str[0][j] == 'b')
{
a[j][n*n] = 0;
}
else
{
a[j][n*n] = 1;
}
} for(int i = 1; i < n; i++)
{
scanf("%s",str[i]);
for(int j = 0; j < n; j++)
{
if(str[i][j] == 'b')
{
a[i*n+j][n*n] = 0;
}
else
{
a[i*n+j][n*n] = 1;
}
}
}
int ans1 = solve();
//cout <<ans1 <<endl;
ini();
for(int i = 0;i < n;i++)
{
for(int j = 0;j < n;j++)
{
if(str[i][j] == 'b')
{
a[i*n+j][n*n] = 1;
}
else
{
a[i*n+j][n*n] = 0;
}
}
}
int ans2 = solve();
//cout << ans2<<endl;
if(ans1 == -1 && ans2 == -1)
printf("Impossible\n");
else if(ans1 == -1)
printf("%d\n",ans2);
else if(ans2 == -1)
printf("%d\n",ans1);
else
printf("%d\n",min(ans1,ans2));
}
return 0;
}
poj1753 高斯消元的更多相关文章
- poj1753(高斯消元解mod2方程组)
题目链接:http://poj.org/problem?id=1753 题意:一个 4*4 的棋盘,初始时上面放满了黑色或白色的棋子.对 (i, j) 位置进行一次操作后 (i, j), (i + 1 ...
- 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 ...
随机推荐
- Swift 2.2 的新特性
导读:本文来自SwiftGG翻译组,作者@walkingway基于苹果Swift官方博客中Ted Kremenek所撰写的"Swift 2.2 Released!"文章进行了关于S ...
- 2017 清北济南考前刷题Day 3 morning
实际得分:100+0+0=100 T1 右上角是必败态,然后推下去 发现同行全是必胜态或全是必败态,不同行必胜必败交叉 列同行 所以n,m 只要有一个是偶数,先手必胜 #include<cstd ...
- jstree的简单用法
一般我们用jstree主要实现树的形成,并且夹杂的邮件增删重命名刷新的功能 下面是我在项目中的运用,采用的是异步加载 $('#sensor_ul').data('jstree', false).emp ...
- 识别图片中文字(百度AI)
这个是百度官方的文档 https://ai.baidu.com/docs#/OCR-API/top 通用的文字识别,如果是其他的含生僻字/含位置信息的版本,请参考官方的文档,只 ...
- eclipse maven项目目录
今天遇见一个错误,关于eclipse项目的路径问题,web-inf的路径,上图和下图出现了两种web-INF,src的web-INFf和webContent的web-INF,src里面的文件需要编译以 ...
- Mybatis的mapper代理开发dao方法
看完了之前的mybatis原始的dao开发方法是不是觉得有点笨重,甚至说没有发挥mybatis 作为一个框架的优势.总结了一下,原始的dao方法有以下几点不足之处 dao接口实现方法中存在大量的模板方 ...
- c++中模板是什么?为什么要定义模板?
一.c++中模板是什么? 首先: int Max(int x, int y) { return x > y ? x : y; } float Max(float a,float b) { ret ...
- python flask框架 蓝图的使用
蓝图的目的是实现 各个模块的视图函数写在不同的py文件当中. 主视图 中 导入 分路由视图的模块,并且注册蓝图对象 分路由视图中 利用 蓝图对象 的route 进行装饰视图函数 主路由视图函数: #c ...
- Python之socketserver模块和验证客户端链接的合法性
验证客户端链接的合法性 分布式系统中实现一个简单的客户端链接认证功能 #_*_coding:utf-8_*_ from socket import * import hmac,os secret_ke ...
- 未能加载文件或程序集“ RevitAPIUI.dll”
revit二次开发中遇到的问题 RevitAPIUI.dll 只能 Native Library 中执行: 脱离了Native Library,API是跑不起来的 . 检查程序流程:登录,配置,启动r ...