Flip Game
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 37055   Accepted: 16125

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

思路:

和前面那些问题差不多吧,只是它最后结果要求全黑或者全白,所以算了两次

poj1753
/*
同样的基于二维矩阵的开关问题,只是题目要求最终结果要
全亮或者全黑,于是算两次即可
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
typedef long double ld; using namespace std;
const int maxn = 40; int equ,var;
int a[maxn][maxn];
int b[maxn][maxn];
int x[maxn];
int free_x[maxn];
int free_num; int Gauss()
{
int max_r,col,k;
free_num = 0;
for(k = 0,col = 0; k < equ && col < var; k++,col++)
{
max_r = k;
for(int i = k+1; i < equ; i++)
{
if(abs(a[i][col]) > abs(a[max_r][col]))
max_r = i;
}
if(a[max_r][col] == 0)
{
k --;
free_x[free_num++] = col;
continue;
}
if(max_r != k)
{
for(int j = col; j < var+1; j++)
swap(a[k][j],a[max_r][j]); }
for(int i = k + 1; i < equ; i++)
{
if(a[i][col] != 0)
{
for(int j = col; j < var+1; j++)
a[i][j] ^= a[k][j];
}
} }
for(int i = k; i < equ; i++)
if(a[i][col] != 0)
return -1;
if(k < var) return var-k; for(int i = var-1; i >= 0; i--)
{
x[i] = a[i][var];
for(int j = i +1; j < var; j++)
x[i] ^= (a[i][j] && x[j]); }
return 0; } int n;
void ini()
{
memset(a,0,sizeof(a));
memset(x,0,sizeof(x));
equ = n*n;
var = n*n;
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
int tt = i*n+ j;
a[tt][tt] =1;
b[tt][tt] = 1;
if(i > 0) a[(i-1)*n+j][tt] = 1;
if(i < n-1) a[(i+1)*n+j][tt] = 1;
if(j > 0) a[tt-1][tt] = 1;
if(j < n-1) a[tt+1][tt] =1;
}
}
} int solve()
{
int t = Gauss();
if(t == -1)
{
return t;
}
else if(t == 0)
{
int ans = 0;
for(int i = 0; i < n*n; i++)
ans += x[i];
return ans;
}
else
{
int ans = 0x3f3f3f3f;
int tot = (1 << t);
for(int i = 0; i < tot; i++)
{
int cnt = 0;
for(int j = 0; j < t; j++)
{
if(i & (1 << j))
{
cnt ++;
x[free_x[j]]= 1;
}
else x[free_x[j]]= 0;
} for(int j = var-t-1; j >= 0; j--)
{
int dex;
for(dex = j; dex < var; dex++)
if(a[j][dex])
break;
x[dex] = a[j][var];
for(int l = dex +1; l <var ; l++)
{
if(a[j][l])
x[dex] ^= x[l];
}
cnt += x[dex];
}
ans = min(ans,cnt);
}
return ans;
}
} char str[30][30]; int main()
{
int T;
char color;
scanf("%d",&T);
while(scanf("%s",str[0]) !=EOF)
{
n = 4;
ini();
for(int j = 0; j < n; j++)
{
if(str[0][j] == 'b')
{
a[j][n*n] = 0;
}
else
{
a[j][n*n] = 1;
}
} for(int i = 1; i < n; i++)
{
scanf("%s",str[i]);
for(int j = 0; j < n; j++)
{
if(str[i][j] == 'b')
{
a[i*n+j][n*n] = 0;
}
else
{
a[i*n+j][n*n] = 1;
}
}
}
int ans1 = solve();
//cout <<ans1 <<endl;
ini();
for(int i = 0;i < n;i++)
{
for(int j = 0;j < n;j++)
{
if(str[i][j] == 'b')
{
a[i*n+j][n*n] = 1;
}
else
{
a[i*n+j][n*n] = 0;
}
}
}
int ans2 = solve();
//cout << ans2<<endl;
if(ans1 == -1 && ans2 == -1)
printf("Impossible\n");
else if(ans1 == -1)
printf("%d\n",ans2);
else if(ans2 == -1)
printf("%d\n",ans1);
else
printf("%d\n",min(ans1,ans2));
}
return 0;
}

  

poj1753 高斯消元的更多相关文章

  1. poj1753(高斯消元解mod2方程组)

    题目链接:http://poj.org/problem?id=1753 题意:一个 4*4 的棋盘,初始时上面放满了黑色或白色的棋子.对 (i, j) 位置进行一次操作后 (i, j), (i + 1 ...

  2. POJ 1222 POJ 1830 POJ 1681 POJ 1753 POJ 3185 高斯消元求解一类开关问题

    http://poj.org/problem?id=1222 http://poj.org/problem?id=1830 http://poj.org/problem?id=1681 http:// ...

  3. 【BZOJ-3143】游走 高斯消元 + 概率期望

    3143: [Hnoi2013]游走 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2264  Solved: 987[Submit][Status] ...

  4. 【BZOJ-3270】博物馆 高斯消元 + 概率期望

    3270: 博物馆 Time Limit: 30 Sec  Memory Limit: 128 MBSubmit: 292  Solved: 158[Submit][Status][Discuss] ...

  5. *POJ 1222 高斯消元

    EXTENDED LIGHTS OUT Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9612   Accepted: 62 ...

  6. [bzoj1013][JSOI2008][球形空间产生器sphere] (高斯消元)

    Description 有一个球形空间产生器能够在n维空间中产生一个坚硬的球体.现在,你被困在了这个n维球体中,你只知道球 面上n+1个点的坐标,你需要以最快的速度确定这个n维球体的球心坐标,以便于摧 ...

  7. hihoCoder 1196 高斯消元·二

    Description 一个黑白网格,点一次会改变这个以及与其连通的其他方格的颜色,求最少点击次数使得所有全部变成黑色. Sol 高斯消元解异或方程组. 先建立一个方程组. \(x_i\) 表示这个点 ...

  8. BZOJ 2844 albus就是要第一个出场 ——高斯消元 线性基

    [题目分析] 高斯消元求线性基. 题目本身不难,但是两种维护线性基的方法引起了我的思考. void gauss(){ k=n; F(i,1,n){ F(j,i+1,n) if (a[j]>a[i ...

  9. SPOJ HIGH Highways ——Matrix-Tree定理 高斯消元

    [题目分析] Matrix-Tree定理+高斯消元 求矩阵行列式的值,就可以得到生成树的个数. 至于证明,可以去看Vflea King(炸树狂魔)的博客 [代码] #include <cmath ...

随机推荐

  1. python 使用Nginx和uWSGI来运行Python应用

    参考:http://zmrenwu.com/post/20/ uWSGI是一个Web应用服务器,它具有应用服务器,代理,进程管理及应用监控等功能.它支持WSGI协议,同时它也支持自有的uWSGI协议, ...

  2. edittext实现自动查询,刷新listview

    mEdittextqueryvalue.addTextChangedListener(new TextWatcher() {             @Override             pub ...

  3. Flask 应用最佳实践

    一个好的应用目录结构可以方便代码的管理和维护,一个好的应用管理维护方式也可以强化程序的可扩展性 应用目录结构 假定我们的应用主目录是"flask-demo",首先我们建议每个应用都 ...

  4. CentOS7安装配置iptables防火墙

    转载请注明出处:http://blog.csdn.net/l1028386804/article/details/50779761 CentOS7默认的防火墙不是iptables,而是firewall ...

  5. Raid 5数据恢复原理以及raid 5数据恢复实际操作案例

    Raid 5数据恢复算法原理 要理解 raid 5数据恢复原理首先要先认识raid5,"分布式奇偶校验的独立磁盘结构"也就是我们称之为的raid 5数据恢复有一个概念需要理解,也就 ...

  6. 一次“峰回路转”的troubleshooting经历

    某天,用户现场人员找到我,说应用的某个功能一点就报错,在数据库上直接跑功能对应的SQL也报错,SQL大致如下: 后来向他们要了alert.log和trace files,通过分析,确定为用户数据库版本 ...

  7. python __str__ 和__repr__方法

    看下面的例子就明白了 class Test(object): def __init__(self, value='hello, world!'): self.data = value >> ...

  8. Extensions in UWP Community Toolkit - Overview

    概述 UWP Community Toolkit  中有一个 Extensions 的集合,它们可以帮助开发者实现很多基础功能,省去自己造轮子的过程,本篇我们先来看一下 Extensions 的功能都 ...

  9. 基于dns搭建eureka集群

    eureka集群方案: 1.通常我们部署的eureka节点多于两个,根据实际需求,只需要将相邻节点进行相互注册(eureka节点形成环状),就达到了高可用性集群,任何一个eureka节点挂掉不会受到影 ...

  10. 新概念英语(1-137)A pleasant dream

    Lesson 137 A pleasant dream 美好的梦 Listen to the tape then answer this question. What would Julie like ...