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 ...
随机推荐
- java从网络中下载图片到本地
public class imageDownload { public static void main(String[] args) { String url = "http://loca ...
- 北亚关于HP EVA4400/6400/8400/P6000的数据恢复解决方案
[引言]本文档建立在针对HP EVA的大量测试性研究基础上,所有的细节几乎均为对EVA的破译型研究,目前全球范围内尚未发现类似资料,故可能表述方式和结论并不精确,仅为参考之用.我们公司为研究HP EV ...
- EasyUI 中easyui-textbox和easyui-searchbox文本框的点击事件。
html: <input id="txtsearch" class="easyui-textbox" data-options="buttonT ...
- php里面的变量的使用
php里面的变量一般可以直接使用不需要声明,但是这种var_dump($a);就会报错,还有sql语句里面如果某个变量为空也会报错. 如果变量为null,空,未声明都==false,但是不===fal ...
- http缓存浅谈
我们在访问百度首页的时候,会发现不管怎么刷新页面,静态资源基本都是返回 200(from cache): 随便点开一个静态资源是酱的: 哎哟有Response报头数据呢,看来服务器也正常返回了etag ...
- 用C#(.NET Core) 实现简单工厂和工厂方法模式
本文源自深入浅出设计模式. 只不过我是使用C#/.NET Core实现的例子. 前言 当你看见new这个关键字的时候, 就应该想到它是具体的实现. 这就是一个具体的类, 为了更灵活, 我们应该使用的是 ...
- Linux知识积累(5) 关机shutdown和重启reboot
Linux centos关机与重启命令详解与实战 Linux centos重启命令: 1.reboot 2.shutdown -r now 立刻重启(root用户使用) 3.shutdown -r 1 ...
- 由路由器AP隔离引起的WEB服务不能访问的问题
最近在自己的电脑上运行了一个Web服务,发现它只能被本机访问,无法在同一个局域网的其它设备访问. 于是在网上搜索了各种资料,通通都是在操作系统层面来分析解决问题的. 而我怎么尝试都不成功的情况下,差点 ...
- QT5.5与MYSQL5.6数据库连接的具体方法与实现
由于毕设需要用到QT读取数据库中的数据,并将数据保存至数据库中.花了一天的时间,总算实现了从QT中读取数据库中的数据.网上相关资料很多,但是写得不是很全,中间出现了一些问题,解决起来比较麻烦.所以本文 ...
- SendMessage 遇到的神坑
场景 两个进程A和B,需要从A中设置B中的文本框的内容 过程 x.x.x.x. 成功获取了B中的内容,惊喜,离成功更近异步 xxxx ***** ....... x.x.x.x. 大约查找了几百个网页 ...