题目链接

4*4的格子, 初始为0或1, 每次翻转一个会使它四周的也翻转, 求翻转成全0或全1最少的步数。

#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-;
const int mod = 1e9+;
const int inf = ;
const int dir[][] = { {-, }, {, }, {, -}, {, } };
int a[][], ans[], x[];
int n, l[], free_x[];
int gauss(int equ,int var)
{
int i,j,k, max_r, col = , temp, free_index, num = ;
for(int i=;i<=var;i++)
{
x[i]=;
free_x[i]=;
}
for(k = ;k < equ && col < var;k++,col++)
{
max_r=k;
for(i=k+;i<equ;i++)
{
if(abs(a[i][col])>abs(a[max_r][col])) max_r=i;
}
if(max_r!=k)
{
for(j=k;j<var+;j++) swap(a[k][j],a[max_r][j]);
}
if(a[k][col]==)
{
k--;
free_x[num++]=col;
continue;
}
for(i=k+;i<equ;i++)
{
if(a[i][col]!=)
{
for(j=col;j<var+;j++)
{
a[i][j] ^= a[k][j];
}
}
}
}
for (i = k; i < equ; i++)
{
if (a[i][col] != ) return inf;
}
int stat = <<(var-k);
int res = inf;
for(i=;i<stat;i++)
{
int cnt=;
int index=i;
for(j=;j<var-k;j++)
{
x[free_x[j]]=(index&);
if(x[free_x[j]]) cnt++;
index>>=;
}
for(j=k-;j>=;j--)
{
int tmp=a[j][var];
int t=;
while(a[j][t]==)t++;
for(int l=t+;l<var;l++)
if(a[j][l]) tmp^=x[l];
x[t]=tmp;
if(x[t])cnt++;
}
if(cnt<res)res=cnt;
}
return res;
}
void init() {
mem(a);
for(int i = ; i<; i++) {
for(int j = ; j<; j++) {
a[i*+j][i*+j] = ;
for(int k = ; k<; k++) {
int x = dir[k][]+i;
int y = dir[k][]+j;
if(x>=&&x<&&y>=&&y<) {
a[i*+j][x*+y] = ;
}
}
}
}
}
int main()
{
char c[][];
for(int i = ; i<; i++) {
cin>>c[i];
}
init();
for(int i = ; i<; i++) {
for(int j = ; j<; j++) {
if(c[i][j] == 'b')
a[i*+j][] = ;
else
a[i*+j][] = ;
}
}
int ans = gauss(, );
init();
for(int i = ; i<; i++) {
for(int j = ; j<; j++) {
if(c[i][j] == 'w')
a[i*+j][] = ;
else
a[i*+j][] = ;
}
}
ans = min(ans, gauss(, ));
if(ans == inf) {
cout<<"Impossible"<<endl;
} else {
cout<<ans<<endl;
}
return ;
}

poj 1753 Flip Game 高斯消元的更多相关文章

  1. POJ 1753 Flip game ( 高斯消元枚举自由变量)

    题目链接 题意:给定一个4*4的矩阵,有两种颜色,每次反转一个颜色会反转他自身以及上下左右的颜色,问把他们全变成一种颜色的最少步数. 题解:4*4的矩阵打表可知一共有四个自由变元,枚举变元求最小解即可 ...

  2. POJ 1830 开关问题 高斯消元,自由变量个数

    http://poj.org/problem?id=1830 如果开关s1操作一次,则会有s1(记住自己也会变).和s1连接的开关都会做一次操作. 那么设矩阵a[i][j]表示按下了开关j,开关i会被 ...

  3. A - The Water Bowls POJ - 3185 (bfs||高斯消元)

    题目链接:https://vjudge.net/contest/276374#problem/A 题目大意:给你20个杯子,每一次操作,假设当前是对第i个位置进行操作,那么第i个位置,第i+1个位置, ...

  4. POJ 1166 The Clocks 高斯消元 + exgcd(纯属瞎搞)

    依据题意可构造出方程组.方程组的每一个方程格式均为:C1*x1 + C2*x2 + ...... + C9*x9 = sum + 4*ki; 高斯消元构造上三角矩阵,以最后一个一行为例: C*x9 = ...

  5. POJ 2065 SETI(高斯消元)

    题目链接:http://poj.org/problem?id=2065 题意:给出一个字符串S[1,n],字母a-z代表1到26,*代表0.我们用数组C[i]表示S[i]经过该变换得到的数字.给出一个 ...

  6. POJ 1830 开关问题 (高斯消元)

    题目链接 题意:中文题,和上篇博客POJ 1222是一类题. 题解:如果有解,解的个数便是2^(自由变元个数),因为每个变元都有两种选择. 代码: #include <iostream> ...

  7. Poj 2947 widget factory (高斯消元解同模方程)

    题目连接: http://poj.org/problem?id=2947 题目大意: 有n种类型的零件,m个工人,每个零件的加工时间是[3,9],每个工人在一个特定的时间段内可以生产k个零件(可以相同 ...

  8. NEFU 504 new Flip Game (高斯消元)

    题目链接 题解:和 poj1753Filp game 差不多,区别在于t组数据并且翻转的时候多了一个左上角. #include <iostream> #include <cstdio ...

  9. POJ 3532 Resistance(高斯消元+基尔霍夫定理)

    [题目链接] http://poj.org/problem?id=3532 [题目大意] 给出n个点,一些点之间有电阻相连,求1~n的等效电阻 [题解] 有基尔霍夫定理:任何一个点(除起点和终点)发出 ...

随机推荐

  1. IE 调试JS加断点不管用 增加debugger

    ie按F12添加断点调试js不管用 有个方法在你想加断点前面加debugger,再调试就管用了

  2. 【转】SQL Server 2008 新类型介绍之Date和Time

     转自CSDN TJVictor专栏:http://blog.csdn.net/tjvictor/archive/2009/07/13/4344429.aspx   SQL Server 2008除了 ...

  3. asp.net、html、javascript等比较有用的网站

    Learn模块下web.mvc.razor等. http://www.asp.net/ 包括html.javascript.jquery.xml等教程. http://www.w3school.com ...

  4. QML Performance

    1) Limit JavaScript a) inline JavaScript:  内联的JavaScript方法;  1. 将js方法放置在Element内部; 2. 尝试将语句写在一行内; e. ...

  5. As Easy As A+B

    Problem Description These days, I am thinking about a question, how can I get a problem as easy as A ...

  6. Gow工具

    一 Gow 是什么 Gow (Gnu On Windows) is the lightweight alternative to Cygwin. It uses a convenient NSIS i ...

  7. codeforces 455C 并查集

    传送门 给n个点, 初始有m条边, q个操作. 每个操作有两种, 1是询问点x所在的连通块内的最长路径, 就是树的直径. 2是将x, y所在的两个连通块连接起来,并且要合并之后的树的直径最小,如果属于 ...

  8. 个人收集资料整理-WinForm

    [2016-03-23 20:29:56] 别人收集常用: http://www.cnblogs.com/babycool/p/3541192.html

  9. python进阶6 HTTP协议客户端实现

    httplib 1.httplib 是 python中http 协议的客户端实现,可以使用该模块来与 HTTP 服务器进行交互. httplib的内容不是很多,也比较简单.以下是一个非常简单的例子,使 ...

  10. 常见iis错误之一

    1.win7配置iis 出现:HTTP 错误 403.14 - Forbidden 打开 IIS 管理器. 在“功能”视图中,双击“目录浏览”. 在“目录浏览”页上,在“操作”窗格中单击“启用”. 确 ...