Flip Game
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 45691   Accepted: 19590

Description

Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 16 squares. One side of each piece is white and the other one is black and each piece is lying either it's black or white side up. Each round you flip 3 to 5 pieces, thus changing the color of their upper side from black to white and vice versa. The pieces to be flipped are chosen every round according to the following rules: 
  1. Choose any one of the 16 pieces.
  2. 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

The input consists of 4 lines with 4 characters "w" or "b" each that denote game field position.

Output

Write to the output file a single integer number - the minimum number of rounds needed to achieve the goal of the game from the given position. If the goal is initially achieved, then write 0. If it's impossible to achieve the goal, then write the word "Impossible" (without quotes).

Sample Input

bwwb
bbwb
bwwb
bwww

Sample Output

4

题目链接:POJ 1753

跟POJ 1222一样,只是会出现自由变量,那么我们可以用二进制枚举他们的值(由于是开关只能为0或1),然后他们的值即ans数组,然后再在高斯消元最后的回代法中考虑进这些自由变量的枚举值,计算ans中1的个数,要做两次这样的操作,因为最后的状态可以是全黑也可以是全白。代码里用模2的普通消元运算来代替异或运算,方便当模版

代码:

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <sstream>
#include <numeric>
#include <cstring>
#include <bitset>
#include <string>
#include <deque>
#include <stack>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define fin(name) freopen(name,"r",stdin)
#define fout(name) freopen(name,"w",stdout)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int, int> pii;
typedef long long LL;
const double PI = acos(-1.0);
const int N = 6;
int Mat[18][18], ans[18];
char pos[N][N];
int num; int gcd(int a, int b)
{
return b ? gcd(b, a % b) : a;
}
int lcm(int a, int b)
{
return a / gcd(a, b) * b;
}
inline int id(const int &x, const int &y)
{
return (x - 1) * 4 + y;
}
int Gaussian(int neq, int nvar)
{
int ceq, cvar;
int i, j;
num = 0;
for (ceq = 1, cvar = 1; ceq <= neq && cvar <= nvar; ++ceq, ++cvar)
{
int teq = ceq;
for (i = ceq + 1; i <= neq; ++i)
if (Mat[i][cvar] > Mat[teq][cvar])
teq = i;
if (teq != ceq)
for (i = cvar; i <= nvar + 1; ++i)
swap(Mat[ceq][i], Mat[teq][i]);
if (!Mat[ceq][cvar])
{
--ceq;
++num;
continue;
}
for (i = ceq + 1; i <= neq; ++i)
if (Mat[i][cvar])
{
int LCM = lcm(Mat[i][cvar], Mat[ceq][cvar]);
int up = LCM / Mat[ceq][cvar];
int down = LCM / Mat[i][cvar];
for (j = cvar; j <= nvar + 1; ++j)
Mat[i][j] = (Mat[i][j] * down % 2 - Mat[ceq][j] * up % 2 + 2) % 2;
}
}
for (i = ceq; i <= neq; ++i)
if (Mat[i][cvar])
return INF;
int ret = INF;
int stcnt = 1 << num;
for (int st = 0; st < stcnt; ++st)
{
int cnt = 0;
for (i = 0; i < num; ++i)
ans[ceq + i] = ((1 << i) & st) ? 1 : 0;
for (i = neq - num; i >= 1; --i)
{
ans[i] = Mat[i][nvar + 1];
for (j = i + 1; j <= nvar; ++j)
ans[i] = ((ans[i] % 2 - Mat[i][j] * ans[j] % 2) + 2) % 2;
}
for (i = 1; i <= 16; ++i)
cnt += ans[i];
ret = min(ret, cnt);
}
return ret;
}
int main(void)
{
int i, j;
while (~scanf("%s", pos[1] + 1))
{
for (i = 2; i <= 4; ++i)
scanf("%s", pos[i] + 1);
CLR(Mat, 0);
CLR(ans, 0);
for (i = 1; i <= 4; ++i)
{
for (j = 1; j <= 4; ++j)
{
Mat[id(i, j)][id(i, j)] = 1;
Mat[id(i, j)][17] = (pos[i][j] == 'b');
if (i > 1)
Mat[id(i, j)][id(i - 1, j)] = 1;
if (i < 4)
Mat[id(i, j)][id(i + 1, j)] = 1;
if (j > 1)
Mat[id(i, j)][id(i, j - 1)] = 1;
if (j < 4)
Mat[id(i, j)][id(i, j + 1)] = 1;
}
}
int Ans = Gaussian(16, 16);
CLR(Mat, 0);
CLR(ans, 0);
for (i = 1; i <= 4; ++i)
{
for (j = 1; j <= 4; ++j)
{
Mat[id(i, j)][id(i, j)] = 1;
Mat[id(i, j)][17] = (pos[i][j] == 'w');
if (i > 1)
Mat[id(i, j)][id(i - 1, j)] = 1;
if (i < 4)
Mat[id(i, j)][id(i + 1, j)] = 1;
if (j > 1)
Mat[id(i, j)][id(i, j - 1)] = 1;
if (j < 4)
Mat[id(i, j)][id(i, j + 1)] = 1;
}
}
Ans = min(Ans, Gaussian(16, 16));
Ans == INF ? puts("Impossible") : printf("%d\n", Ans);
}
return 0;
}

POJ 1753 Flip Game(高斯消元+状压枚举)的更多相关文章

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

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

  2. poj 1753 Flip Game 高斯消元

    题目链接 4*4的格子, 初始为0或1, 每次翻转一个会使它四周的也翻转, 求翻转成全0或全1最少的步数. #include <iostream> #include <vector& ...

  3. loj2542 「PKUWC2018」随机游走 MinMax 容斥+树上高斯消元+状压 DP

    题目传送门 https://loj.ac/problem/2542 题解 肯定一眼 MinMax 容斥吧. 然后问题就转化为,给定一个集合 \(S\),问期望情况下多少步可以走到 \(S\) 中的点. ...

  4. AcWing 208. 开关问题 (高斯消元+状压)打卡

    有N个相同的开关,每个开关都与某些开关有着联系,每当你打开或者关闭某个开关的时候,其他的与此开关相关联的开关也会相应地发生变化,即这些相联系的开关的状态如果原来为开就变为关,如果为关就变为开. 你的目 ...

  5. POJ 1222【异或高斯消元|二进制状态枚举】

    题目链接:[http://poj.org/problem?id=1222] 题意:Light Out,给出一个5 * 6的0,1矩阵,0表示灯熄灭,反之为灯亮.输出一种方案,使得所有的等都被熄灭. 题 ...

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

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

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

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

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

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

  9. POJ 2065 SETI(高斯消元)

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

随机推荐

  1. CopyOnWriteArrayList分析——能解决什么问题

    CopyOnWriteArrayList主要可以解决的问题是并发遍历读取无锁(通过Iterator) 对比CopyOnWriteArrayList和ArrayList 假如我们频繁的读取一个可能会变化 ...

  2. C语言字符,字符串,字节操作常用函数

    strlen 这个函数是在 string.h 的头文件中定义的 它的函数原型是 size_t strlen( const char ); size_t 是一个无符号整型,是这样定义的 typedef ...

  3. HTML第四章:初始css

    CSS样式:                 一.为什么要使用CSS;可以让页面更美观.有利于开发速度.                 二.什么是CSS:全称cascading style shee ...

  4. 初尝微信小程序2-Swiper组件、导航栏标题配置

    swiper 滑块视图容器. 很多网页的首页都会有一个滚动的图片模块,比如天猫超市首页,滚动着很多优惠活动的图片,用来介绍优惠内容,以及供用户点击快速跳转到相应页面. Swiper不仅可以滚动图片,也 ...

  5. ES6学习(二):函数的扩展

    chapter07 函数的扩展 7.1 函数默认值 7.1.1 参数默认值简介 传统做法的弊端(||):如果传入的参数相等于(==)false的话,仍会被设为默认值,需要多加入一个if判断,比较麻烦. ...

  6. 解决: Intelij IDEA 创建WEB项目时没有Servlet的jar包

    今天创建SpringMVC项目时 用到HttpServletRequest时, 发现项目中根本没有Servlet这个包, 在网上搜了一下,这个问题是因为web项目没有添加服务器导致的. 配置tomec ...

  7. [BZOJ] 2044: 三维导弹拦截

    排序去掉一维,剩下两维可以直接\(O(n^2)\)做,也可以用二维树状数组(但是不方便建边),解决第一问 第二问,按转移顺序连边,建出DAG,求最小不可重链覆盖即可 #include<algor ...

  8. 安全 - 堡垒机 - Jumpserver

    GitHub - jumpserver/Dockerfile: Jumpserver all in one Dockerfile https://github.com/jumpserver/Docke ...

  9. Linux 系统中 sudo 命令的 10 个技巧

    概览 sudo 表示 "superuser do". 它允许已验证的用户以其他用户的身份来运行命令.其他用户可以是普通用户或者超级用户.然而,大部分时候我们用它来以提升的权限来运行 ...

  10. JZOJ 5818. 【NOIP提高A组模拟2018.8.15】 做运动

    5818. [NOIP提高A组模拟2018.8.15] 做运动 (File IO): input:running.in output:running.out Time Limits: 2000 ms  ...