POJ 1753 Flip Game(列舉變元的高斯消元)
以前做這道題目的時候好像是dfs過的,資料很小搜尋也很簡單。
不過這道題目也可以用高斯消元來搞,類似於16個開關的開關問題。不過終態會有兩種,所以解兩次取一個最優的結果。
最後有多種解的時候要進行列舉沒種情況所對應的解的總和,選一個最小的。
Flip Game
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 30436 | Accepted: 13222 |
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:
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.
- 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
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
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#define eps 1e-10
///#define M 1000100
#define LL __int64
///#define LL long long
#define INF 0x7ffffff
#define PI 3.1415926535898
#define zero(x) ((fabs(x)<eps)?0:x)
#define mod 2
const int maxn = 210;
using namespace std;
int a[maxn][maxn];
int x[maxn];
int equ, var;
int x1[maxn], x2[maxn];
int free_x[maxn];
int free_num;
int f[maxn];
char str;
int LCM(int a, int b)
{
return (a/(__gcd(a, b)))*b;
}
int Gauss()
{
int row, col, max_r;
int num = 0;
row = col = 0;
while(row < equ && col < var)
{
max_r = row;
for(int i = row+1; i < equ; i++)
if(abs(a[i][col]) > abs(a[max_r][col])) max_r = i;
if(max_r != row)
for(int j = col; j <= var; j++) swap(a[row][j], a[max_r][j]);
if(a[row][col] == 0)
{
free_x[num++] = col;
col++;
continue;
}
for(int i = row+1; i < equ; i++)
{
if(a[i][col] == 0) continue;
int l = LCM(abs(a[row][col]), abs(a[i][col]));
int ta = l/a[i][col];
int tb = l/a[row][col];
if(ta*tb < 0) tb *= -1;///判斷是否異號
for(int j = col; j <= var; j++)
a[i][j] = ((a[i][j]*ta - a[row][j]*tb)%mod + mod)%mod;
}
row++;
col++;
}
for(int i = row; i < equ; i++)///無解的情況;
if(a[i][col] != 0) return INF;
if(var == row)
{
for(int i = var-1; i >= 0; i--)///唯一解的情況,根據上三角陣,迭代求出每一次的值
{
int tmp = a[i][var];
for(int j = i+1; j < var; j++)
if(a[i][j] != 0) tmp = ((tmp-a[i][j]*x[j])%mod + mod)%mod;
while(tmp%a[i][i] != 0)
tmp += mod;
x[i] = tmp/a[i][i]%mod;
}
int cnt = 0;
for(int i = 0; i < 16; i++)
cnt += x[i];
return cnt;
}
int res = INF;
int n = 1<<(var-row);
int t = var-row;
for(int i = 0; i < n; i++)///列舉變元
{
int cnt = 0;
for(int j = 0; j < t; j++)
{
if(i&(1<<j))
{
x[free_x[j]] = 1;
cnt++;
}
else
x[free_x[j]] = 0;
}
for(int j = row-1; j >= 0; j--)
{
int xx;
for(xx = j; xx < var; xx++)
if(a[j][xx]) break;
x[xx] = a[j][var];
for(int k = xx+1; k < var; k++)
if(a[j][k]) x[xx] ^= x[k];
cnt += x[xx];
}
res = min(res, cnt);
}
return res;
}
void init()
{
equ = var = 16;
memset(x, 0, sizeof(x));
memset(a, 0, sizeof(a));
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 4; j++)
{
int t = i*4+j;
a[t][t] = 1;
if(i-1 >= 0) a[(i-1)*4+j][t] = 1;
if(i+1 < 4) a[(i+1)*4+j][t] = 1;
if(j-1 >= 0) a[i*4+j-1][t] = 1;
if(j+1 < 4) a[i*4+j+1][t] = 1;
}
}
}
int main()
{
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 4; j++)
{
cin >>str;
if(str == 'w')
f[i*4+j] = 0;
else
f[i*4+j] = 1;
}
}
for(int i = 0; i < 16; i++) x1[i] = 0;
for(int i = 0; i < 16; i++) x2[i] = 1;
init();
for(int i = 0; i < 16; i++) a[i][16] = f[i]^x1[i];
int flag1 = Gauss();
init();
for(int i = 0; i < 16; i++) a[i][16] = f[i]^x2[i];
int flag2 = Gauss();
if(flag1 == flag2 && flag1 == INF)
cout<<"Impossible"<<endl;
else
cout<<min(flag1, flag2)<<endl;
return 0;
}
相關文章
- POJ1288 Sly Number(高斯消元 dfs列舉)
- 高斯消元
- 高斯消元法
- 淺談高斯消元
- 高斯消元學習筆記筆記
- Note - 高斯消元法(證明略)
- 從高斯消元法到特徵值特徵向量特徵
- Python元類與列舉類Python
- 高斯消元學習筆記——P304題解筆記
- 【題解】金牌導航-高斯消元/Luogu P3232 遊走
- 洛谷P2973 [USACO10HOL]趕小豬(高斯消元 期望)
- POJ1602 昂貴的聘禮【Dijkstra+列舉】
- 矩陣消元 elimination矩陣
- 益普索Ipsos:消費者期待元宇宙改變生活元宇宙
- 《邪惡冥刻》:對元遊戲(meta Game)的重新理解遊戲GAM
- 影像濾波演算法整理--均值、中值、高斯、拉普拉斯運算元、梯度運算元:演算法梯度
- 列舉和列舉的取值範圍
- 形函式,等參單元,雅克比矩陣,高斯積分函式矩陣
- 永久代和元空間的變化
- Scala陣列、元組與集合陣列
- Java 列舉、JPA 和 PostgreSQL 列舉JavaSQL
- 列舉
- 元遊戲正在如何改變休閒遊戲的變現遊戲
- 2021年元氣森林氣泡水消費洞察
- Game Pass是如何改變Xbox的?GAM
- Java列舉-通過值查詢對應的列舉Java
- Java enum列舉類詳解 列舉的常見用法Java
- delphi 裡的 列舉
- 列舉子集的方法
- java中的列舉Java
- Python 元組,不可變的列表,滾雪球學 PythonPython
- 中銀消金今年上半年營收21.30億元,淨利潤1.01億元營收
- 澳門首發跨境消費券!超億元澳門元在微信就能領
- 讓動畫變得更簡單之FLIP技術動畫
- 阿里雲優惠3年279元/800元/797元/1650元/1615元阿里
- EasyExcel 自適應列寬、隱藏列、動態列、單元格下拉框選擇資料、單元格文字格式Excel
- Java列舉Java
- scala 列舉
- Enumeration列舉