POJ 1753 Flip Game(高斯消元+状压枚举)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 45691 | Accepted: 19590 |
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
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;
}
最新文章
- Python使用TuShare将股票数据保存到Oracle数据
- mmap为什么比read/write快(兼论buffercache和pagecache)
- shell脚步传参
- CentOS 6.5 x86_64系统手动释放内存
- 开发Eclipse自定义控件
- golang的cgo支持调用C++的方法
- UESTC 876 爱管闲事 --DP
- Java基础--重写(Overriding,覆盖)-重载(Overloading)
- 在centos6.3用yum安装redis
- listview的tag
- lib_mysqludf_sys的安装过程
- 初学Java ssh之Spring 第三篇
- postman接口测试工具3.0版本的坑
- Pomelo的component组件
- linkin大话数据结构--字符串,数组,list之间的互转
- canvas绘制时钟及注释及save和restore的用法
- 一、下载安装superset
- python虚拟环境迁移
- 【练习】Python第一,二次
- AS插件-GsonFormat