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语法基础(总结)
1,关键字:其实就是某种语言赋予了特殊含义的单词. 保留字:其实就是还没有赋予特殊含义,但是准备日后要使用过的单词. 2,标示符:其实就是在程序中自定义的名词.比如类名,变量名,函数名.包含 0-9. ...
- js常用API方法
String对象常用的API:API指应用程序编程接口,实际上就是一些提前预设好的方法. charAt() 方法可返回指定位置的字符. stringObject.charAt(index) index ...
- java 实现多文件打包下载
jsp页面js代码: function downloadAttached(){ var id = []; id.push(infoid); var options = {}; options.acti ...
- OAuth2.0学习(1-11)新浪开放平台微博认证-使用OAuth2.0调用微博的开放API
使用OAuth2.0调用API 使用OAuth2.0调用API接口有两种方式: 1. 直接使用参数,传递参数名为 access_token URL 1 https://api.weibo.com/2/ ...
- Window7下安装Jmeter
解压Jmeter,存放位置为D:\apache-jmeter-2.11 用户变量——>新建变量名JMETER_HOME,变量值为存放目录 系统变量——>添加;%JMETER_HOME%/l ...
- 三十天学不会TCP,UDP/IP网络编程 -- RTT的计算
欢迎去gitbook(https://www.gitbook.com/@rogerzhu/)看到完整版. 如果对和程序员有关的计算机网络知识,和对计算机网络方面的编程有兴趣,虽然说现在这种“看不见”的 ...
- Python之进程
进程 进程(Process)是计算机中的程序关于某数据集合上的一次运行活动,是系统进行资源分配和调度的基本单位,是操作系统结构的基础.在早期面向进程设计的计算机结构中,进程是程序的基本执行实体:在当代 ...
- uvalive 3213 Ancient Cipher
https://vjudge.net/problem/UVALive-3213 题意: 输入两个字符串,问是否可以由第一个字符串的每个字符一一映射得到第二个字符串,字符是可以随意移动的. 思路: 统计 ...
- MyBatis(1)——快速入门
MyBatis 简介 MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为 ...
- sqlserver数据库导入Mysql数据库问题
近来遇到一个问题,之前的项目用的是SQLServer数据库,但是现在要换成MySQL数据库,所有整理了一些数据导入的步骤,供需要的人参考! 第一步: 第二步: 第三步: 第四步: 第五步: 第六步: ...