题目链接

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. iOS 8.0正式公布啦

    从昨晚開始,苹果新版移动操作系统iOS 8.0開始提供免费下载服务.新版操作系统提供有数百个新功能,当中包含相机功能中添加了延时摄影模式,能够使用其它厂商开发的输入法.购买的App应用程序能够与最多6 ...

  2. 浅谈Spring(四)

    一.Spring+MyBatis整合 spring大大简化了Mybatis的开发步骤. 1.MyBatis的开发要点: mybatis-config.xml配置文件:配置与数据库的链接.mapper文 ...

  3. 调度器(scheduler)

    调度器(schedule)为游戏提供定时事件和定时调用服务. 调度器(schedule)的功能和事件监听器(eventlistener)的功能有点类似:都是在特定情况下调用某个事先准备好的回调函数. ...

  4. Bash shell使用环境的终端的环境设置:stty

    Bash shell使用环境的终端的环境设置:stty Bash shell使用环境的终端的环境设置:stty stty -a 将当前所有的stty参数列出来 intr:给正在运行的程序发送中断信号 ...

  5. Android 一个抽奖应用的逆向破解全流程之加固自己应用

    转自: <a href="http://www.pedant.cn/2014/07/22/crack-a-draw-app/">http://www.pedant.cn ...

  6. srand() rand() time(0)

    标准库<cstdlib>(被包含于<iostream>中)提供两个帮助生成伪随机数的函数: 函数一:int rand(void):从srand (seed)中指定的seed开始 ...

  7. 树的判断(poj nyoj hduoj)

    题目: http://ac.jobdu.com/problem.php?pid=1481 http://acm.nyist.net/JudgeOnline/problem.php?pid=129 ht ...

  8. Java基础—ClassLoader的理解(转)

    默认的三个类加载器 Java默认是有三个ClassLoader,按层次关系从上到下依次是: Bootstrap ClassLoader Ext ClassLoader System ClassLoad ...

  9. Android eng版系统烧录

    这里我只能记录下过程 Android有四层,最底层的Linux是一个kernel,然后是运行时库层,驱动大部分在这两层完成,接着是Framework层,主要是Android源码,最后是在源码基础上进行 ...

  10. move.js

    function startMove(obj,json,fn){ var flag=true;//标志所有运动是否到达目标值 clearInterval(obj.timer); obj.timer=s ...