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;
}

最新文章

  1. Python使用TuShare将股票数据保存到Oracle数据
  2. mmap为什么比read/write快(兼论buffercache和pagecache)
  3. shell脚步传参
  4. CentOS 6.5 x86_64系统手动释放内存
  5. 开发Eclipse自定义控件
  6. golang的cgo支持调用C++的方法
  7. UESTC 876 爱管闲事 --DP
  8. Java基础--重写(Overriding,覆盖)-重载(Overloading)
  9. 在centos6.3用yum安装redis
  10. listview的tag
  11. lib_mysqludf_sys的安装过程
  12. 初学Java ssh之Spring 第三篇
  13. postman接口测试工具3.0版本的坑
  14. Pomelo的component组件
  15. linkin大话数据结构--字符串,数组,list之间的互转
  16. canvas绘制时钟及注释及save和restore的用法
  17. 一、下载安装superset
  18. python虚拟环境迁移
  19. 【练习】Python第一,二次
  20. AS插件-GsonFormat

热门文章

  1. app之间的跳转和传参问题
  2. 在O(1)时间复杂度删除链表节点
  3. 浏览器 DNS缓存与DNS prefetch (DNS预解析)
  4. char的有无符号类型
  5. display :inline-block 处理点小障碍
  6. redis 设置密码验证
  7. 7- vue django restful framework 打造生鲜超市 -商品类别数据展示(上)
  8. win 系统下制作U盘安装 linux系统
  9. eclipse中使用git上传项目
  10. JavaScript对象创建的九种方式