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
#include<stdio.h>
#include<string.h>
int c[][]= {,,,,-,,,-,,},min1,t;
char Map[][]; void turn(int x,int y)//改变状态;
{
for(int i=; i<; i++)
{
int dx=x+c[i][];
int dy=y+c[i][];
if(dx<||dy<||dx>=||dy>=)continue;
if(Map[dx][dy]=='b')
Map[dx][dy]='w';
else Map[dx][dy]='b';
}
} int check()//检查是否达到目标状态;
{
int i,j;
for(i=; i<; i++)
for(j=; j<; j++)
if(Map[i][j]!=Map[][])
return ;
return ;
} void dfs(int x,int y,int s)
{
if(check())//检查是否达到目标状态;
{
if(s<min1)//更新结果;
min1=s;
return ;
}
if(x==)return ;
turn(x,y);//改变状态/回溯
//翻转;
if(y==)dfs(x+,,s+);//换行;
else dfs(x,y+,s+);
//不翻转;
turn(x,y);//改变状态/回溯
if(y==)dfs(x+,,s);
else dfs(x,y+,s);
} int main()
{
int i;
min1=;
for(i=; i<; i++)
scanf("%s",Map[i]);
dfs(,,);
if(min1<=)
printf("%d\n",min1);
else printf("Impossible\n");
}

// 高斯消元

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std ;
#define INF 0x3f3f3f3f
int Map[][] , a[] , min1 , x[] , freex[] ;
char str[][] ;
void getMap(int k)
{
int i , j ;
memset(Map,,sizeof(Map)) ;
for(i = ; i < ; i++)
{
for(j = ; j < ; j++)
{
a[i*+j] = (str[i][j] - '') ^ k ;
Map[i*+j][i*+j] = ;
if( i > )
Map[i*+j][i*+j-] = ;
if( i < )
Map[i*+j][i*+j+] = ;
if( j > )
Map[i*+j][i*+j-] = ;
if( j < )
Map[i*+j][i*+j+] = ;
}
}
return ;
}
void swap1(int p,int q)
{
int j , temp ;
temp = a[p] ;
a[p] = a[q] ;
a[q] = temp ;
for(j = ; j < ; j++)
{
temp = Map[p][j] ;
Map[p][j] = Map[q][j] ;
Map[q][j] = temp ;
}
return ;
}
int solve()
{
int i , j , k , t = , num1 = ;
for(i = ; i < && t < ; t++ , i++)
{
for(j = i ; j < ; j++)
if( Map[j][t] )
break ;
if( j == )
{
freex[num1++] = t ;
i-- ;
continue ;
}
if( i != j )
swap1(i,j) ;
for(j = i+ ; j < ; j++)
{
if( Map[j][t] )
{
a[j] = a[j]^a[i] ;
for(k = t ; k < ; k++)
Map[j][k] = Map[j][k] ^ Map[i][k] ;
}
}
}
for( ; i < ; i++)
if( a[i] )
return - ;
if( num1 ) return num1 ;
for(i = ; i >= ; i--)
{
x[i] = a[i] ;
for(j = i+ ; j < ; j++)
x[i] = x[i]^(Map[i][j]*x[j]) ;
}
return num1 ;
}
int f(int s)
{
int i , j , k , key , ans , min1 = INF ;
getMap(s) ;
key = solve() ;
ans = ;
if( key == )
{
ans = ;
for(i = ; i < ; i++)
ans += x[i] ;
min1 = min(min1,ans) ;
}
else if( key > )
{
int temp = <<key ;
for(int t = ; t < temp ; t++)
{
memset(x,,sizeof(x)) ;
ans = ;
for(j = ; j < key ; j++)
if( t & (<<j) )
{
x[ freex[j] ] = ;
ans++ ;
}
for(i = ; i >= ; i--)
{
for(k = ; k < ; k++)
if( Map[i][k] )
break ;
x[k] = a[i] ;
for(j = k+ ; j < ; j++)
x[k] ^= (Map[i][j]*x[j]) ;
ans += x[k] ;
}
min1 = min(min1,ans) ;
}
}
return min1 ;
}
int main()
{
int i , j , min1 = INF , k , ans , temp ;
for(i = ; i < ; i++)
{
scanf("%s", str[i]) ;
for(j = ; j < ; j++)
{
if( str[i][j] == 'b' )
str[i][j] = '' ;
else
str[i][j] = '' ;
}
}
ans = f() ;
min1 = min(min1,ans) ;
ans = f() ;
min1 = min(min1,ans) ;
if( min1 == INF )
printf("Impossible\n") ;
else
printf("%d\n", min1) ;
return ;
}

Flip Game (高斯消元 || dfs)的更多相关文章

  1. BZOJ1770:[USACO]lights 燈(高斯消元,DFS)

    Description 貝希和她的閨密們在她們的牛棚中玩遊戲.但是天不從人願,突然,牛棚的電源跳閘了,所有的燈都被關閉了.貝希是一個很膽小的女生,在伸手不見拇指的無盡的黑暗中,她感到驚恐,痛苦與絕望. ...

  2. [luoguP2962] [USACO09NOV]灯Lights(高斯消元 + dfs)

    传送门 先进行高斯消元 因为要求最少的开关次数,那么: 对于关键元,我们可以通过带入消元求出, 对于自由元,我们暴力枚举,进行dfs,因为只有开关两种状态,0或1 #include <cmath ...

  3. bzoj 1770: [Usaco2009 Nov]lights 燈【高斯消元+dfs】

    参考:https://blog.csdn.net/qq_34564984/article/details/53843777 可能背了假的板子-- 对于每个灯建立方程:与它相邻的灯的开关次数的异或和为1 ...

  4. Codeforces 1163E 高斯消元 + dfs

    题意:给你一个集合,让你构造一个长度尽量长的排列,使得排列中任意相邻两个位置的数XOR后是集合中的数. 思路:我们考虑枚举i, 然后判断集合中所有小于1 << i的数是否可以构成一组异或空 ...

  5. POJ1288 Sly Number(高斯消元 dfs枚举)

    由于解集只为{0, 1, 2}故消元后需dfs枚举求解 #include<cstdio> #include<iostream> #include<cstdlib> ...

  6. NEFU 504 new Flip Game (高斯消元)

    题目链接 题解:和 poj1753Filp game 差不多,区别在于t组数据并且翻转的时候多了一个左上角. #include <iostream> #include <cstdio ...

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

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

  8. poj 1753 Flip Game 高斯消元

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

  9. BZOJ 2115 Wc2011 Xor DFS+高斯消元

    标题效果:鉴于无向图.右侧的每个边缘,求一个1至n路径,右上路径值XOR和最大 首先,一个XOR并能为一个路径1至n简单的路径和一些简单的XOR和环 我们开始DFS获得随机的1至n简单的路径和绘图环所 ...

随机推荐

  1. C#.net mysql There is already an open datareader associated with this command引发的问题

    [参考]There is already an open datareader associated with this command引发的问题 我在语句中并未使用 DataReader,未何也提示 ...

  2. 处理MySQL的ibdata1文件过大问题

    ibdata1文件是什么? ibdata1是一个用来构建innodb系统表空间的文件,这个文件包含了innodb表的元数据.撤销记录.修改buffer和双写buffer.如果file-per-tabl ...

  3. 第二篇 Linux 虚拟机操作

    下一个虚拟机  Oracle VM 新建一个 空间啥都给大点, 然后下一个Ubuntu镜像 然后打开 试用 try  然后进入后安装就可以用Linux 啦 发现Linux还是看着蛮牛逼,单纯的用于编程 ...

  4. 15.vue动画& vuex

    Vue.config.productionTip = false; ==是否显示提示信息== ==import/export== export xxx 必须跟跟对象或者和定义一起 对象: export ...

  5. centOS 安装gitlab-runner

    1. curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.rpm.sh | sud ...

  6. Python学习之旅(七)

    Python基础知识(6):基本数据类型之列表 在Python中,最基本的数据结构是序列.序列中的每个元素被分配一个序号——即元素的位置,也称为索引.第一个索引从0开始,如果要从右边开始,序列中的最后 ...

  7. 再次重温《Right here waiting》

    记得高中时候听到这首曲子(当时还让同桌帮我抄了这首曲子,后来这个本子也不知道扔到哪里去了), 前天偶尔在虾米遇到这首曲子,过去的青涩岁月历历在目,自己手动打打歌词,一方面是为了重温这首曲子,另一方面, ...

  8. If 与 else的性福生活。

    IF 与 ELSE 从此不再孤单 今天我们来学习java课程里的选择结构——if与else if的意思,众所周知,就是如果想必大家心里对这个词已经有丶数了 else的意思,一目了然,就是否则经过图片的 ...

  9. itoa()、atoi()、任意进制转换

    头文件:<stdlib.h> itoa --功能:将任意类型的数字转换为字符串.在<stdlib.h>中与之有相反功能的函数是atoi. atoi----功 能: 将字符串转换 ...

  10. Echarts . 在柱状图中添加自定义值 (键值对)

    x ["需求"] {"0":"使用Echarts根据数据加载一个饼状图"} {"1":"点击哪个饼状图,弹出此 ...