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 ...
随机推荐
- 20145237《Java程序设计》第一周学习总结
教材学习内容总结 java可分为Java SE.Java EE.Java ME三大平台. java SE分为JVM.JRE.JDK.与java语言四个部分. JRE包括java SE API和JVM. ...
- 团队作业4——第一次项目冲刺(Alpha版本)2017.11.19
第三次会议:2017-11-16 第二次会议讨论的还没有完全实现,于是在第三次会议上对此进行了一些对我们工作上的看法,得出结论:多花时间啊!!!! 又没照照片图: 会议主要内容: 1.登录注册完善 2 ...
- Angular组件——组件生命周期(一)
组件声明周期以及angular的变化发现机制 红色方法只执行一次. 变更检测执行的绿色方法和和组件初始化阶段执行的绿色方法是一个方法. 总共9个方法. 每个钩子都是@angular/core库里定义的 ...
- Vue-cli+Vue.js2.0+Vuex2.0+vue-router+es6+webpack+node.js脚手架搭建和Vue开发实战
Vue.js是一个构建数据驱动的web界面的渐进式框架.在写这边文章时Vue版本分为1.0++和2.0++,这个是基于Vue2.0的项目. Vue-cli是构建单页应用的脚手架,这个可是官方的. Vu ...
- python性能分析--cProfile
Python标准库中提供了三种用来分析程序性能的模块,分别是cProfile, profile和hotshot,另外还有一个辅助模块stats.这些模块提供了对Python程序的确定性分析功能,同时也 ...
- JAVA_SE基础——21.二维数组的定义
2 二维数组的定义 基本与一维数组类似 //定义一个3行5列的二维数组 //方法1,先new对象,然后再初始化每个元素 int[][] a = new int[3][5]; a[0][0]=1; a[ ...
- 用Java语言实现简单的词法分析器
编译原理中的词法分析算是很重要的一个部分,原理比较简单,不过网上大部分都是用C语言或者C++来编写,笔者近期在学习Java,故用Java语言实现了简单的词法分析器. 要分析的代码段如下: 输出结果如下 ...
- Python扩展模块——自动化(testlinkAPI的使用)
使用TESTLINKAPI首先要安装TestLink_API_Python_client-0.6.4(当前最新版本) 目前只使用到了通过api获取testlink中的自定义字段and值 url = ' ...
- vmware 12 安装 mac os 10.12正式版
1.首先下载安装vmware 12 pro ,将VT打开(虚拟功能,以前安装过虚拟机点的同学可忽略). 2.下载mac ox 10.12正式版镜像文件(cdr后缀). 3.下载Unlocker208( ...
- 关于PHP7
目前一直使用php7也看了许多文档视频等,整理一下相关细节(仅为记录-),对于PHP7性能,如下图所示. * 在wordpress3.0.1中 php7比php5.6性能提升约3倍左右 新特性 一.变 ...