Flip Game
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 37427   Accepted: 16288

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

Source

题目大意

  有$4\times 4$的棋盘,上面的棋子一面是黑的,一面是白的。规定翻转一个棋子的同时也要翻转它的上、下、左、右的棋子,问给定一个棋盘的棋子状态,至少需要翻转多少个棋子,能使得所有棋子都是白的或黑的(面在上)。

基本思路

一、暴力搜索出奇迹:

  1、首先要明确一点:这个翻棋子就像按位异或一样,如果一列数里面有两个数是相等的,那把它们全都异或起来,根据结合率就相当于在这过程中有一个数与自身异或,结果为0。把这两个相等的数去掉对最终异或结果是没有影响的。同样看这个翻棋子,它只有两个面,翻一次由黑面在上转为白面在上,或者白转黑,但翻两次就恢复原来的样子了,翻没翻是一样的,除非再翻多一次,甚至是奇数次,但这没有必要也没有意义。

  2、数据规模小,考虑我们最少不翻任何棋子(初始状态就是全白或全黑),最多只翻16个棋子(再翻就有棋子被翻两次了),因此总的状态数为$C^{0}_{16}+C^{1}_{16}+\cdots+C^{16}_{16}=2^{16}$,直接枚举或搜索即可。

  3、考虑要求的是最小值,因此从翻$0$个开始,搜索翻$i$个,直到翻$16$个,中间搜索到即为最小值,翻16个都不满足即输出$Impossible$。

  4、由于棋子都只有黑、白两面,可以用0、1表示,因此可以位压缩成一个数字来进行判断,翻棋子的操作可使用位运算,有两种方法:

    方法一:每一行压缩一个数字,对第$i$行第$j$列棋子进行翻转,比如$j=2$,则$i-1$、$i+1$行的棋子应该和4(0100)相异或(与1异或切换状态,与0异或不改变),而第$i$行棋子应与14(1110)相异或。

    方法二:只有16个棋子,一个int型变量就能存下这16个0/1了,所以可以直接压缩成一个数字。如$i=2, j=2$,则与20032(0100 1110 0100 0000)相异或,不过手算16位的状态是比手算4位烦一点点。

  5、搜索过程中要注意搜过的位置不需要再搜了,所以在函数里控制一下$i$、$j$,当然实现并不唯一。还要注意如果没搜成功,把棋子再翻(flip)一遍,这样就能恢复原样了。不需要memcpy,那是很蠢的做法。

方法一代码:

 #include <stdio.h>

 int field[]={};
int state[][]={{,,,},{,,,}}; void read() {
for(int i=; i<=; i++) {
for(int j=; j<=; j++) {
field[i]<<=;
if(getchar()=='b')
field[i]|=;
}
getchar();
}
} void flip(int i, int j) {--j;
field[i-]^=state[][j];
field[i] ^=state[][j];
field[i+]^=state[][j];
} bool check() {
return (field[]==||field[]==)
&& field[]==field[]
&& field[]==field[]
&& field[]==field[];
} bool find(int n, int i, int j) {
if(n==) return check();
j+=; if(j>) i+=, j=;
if(i>) return false;
for(; i<=; i++) {
for(; j<=; j++) {
flip(i, j);
if(find(n-,i,j))
return true;
flip(i, j);
}
j=;
}
return false;
} void work() {
for(int i=; i<=; i++)
if(find(i,,)) {
printf("%d\n", i);
return;
}
puts("Impossible");
} int main() {
read();
work();
return ;
}

POJ 1753 方法一

方法二代码(首先打个表):

 void init() {
for(int i=; i<=; i++) {
int v=, k=<<(i-); v|=k;
if((i+)%!=) v|=k<<;
if((i-)%!=) v|=k>>;
if(i>) v|=k>>;
if(i<) v|=k<<;
printf("%d,",v);
}
}

很丑的打表

然后就可以拿表去水了,当然直接判断也是可以的,丑。

 #include <stdio.h>

 int field;
int state[]={,,,,,,,,,,,,,,,}; void read() {
for(int i=; i<; i++) {
for(int j=; j<; j++) {
field<<=;
if(getchar()=='b')
field|=;
}
getchar();
}
} void flip(int i) {
field^=state[i];
} bool check() {
return field==0x0000||field==0xFFFF;
} bool find(int n, int i) {
if(n==) return check();
//if(i>=16) return false;
for(; i<; i++) {
flip(i);
if(find(n-,i+))
return true;
flip(i);
}
return false;
} void work() {
for(int c=; c<=; c++)
if(find(c,)) {
printf("%d\n", c);
return;
}
puts("Impossible");
} int main() {
read();
work();
return ;
}

POJ 1753 方法二

二、枚举

  1、这题应该容易想搜索,当然枚举也是比较简单能想到的。我们还是像前面方法二那样位压缩成一个数,如果不能压成一个int的话这题当然也用不了枚举。需要考虑的是如何实现$C^i_{16}$,也就是$16$个选$i$个$(i\in [0, 16])$,考虑我选哪几个棋子也表示成0/1,选择翻转的棋子我用1表示,比如要选择第1个、第3个、第5个和第6个,那就是11 0101的状态。这样枚举就很方便了,枚举值范围0x0000~0xFFFF。

  2、同样像上面方法二那样打个表,对于每个枚举的状态,用位与运算求出哪个位是1(哪个棋子要翻转),然后根据打表的数据对输入的棋盘进行异或运算。过程中对翻转后棋盘全黑或全白的情况求最少翻转数。

  3、可以顺手再打 1<<0 ~ 1<<15 的表。

 #include <stdio.h>

 int field;
int state[]={,,,,,,,,,,,,,,,};
int bit[]={,,,,,,,,,,,,,,,}; void read() {
for(int i=; i<; i++) {
for(int j=; j<; j++) {
field<<=;
if(getchar()=='b')
field|=;
}
getchar();
}
} bool check() {
return field==0x0000||field==0xFFFF;
} int minn=0xFF;
void work() {
for(int flip=; flip<=0xFFFF; flip++) {
int temp=field, cnt=;
for(int i=; i<; i++)
if(flip&bit[i]) {// flip&(1<<i)
field^=state[i];
++cnt;
}
if(check()&&minn>cnt) minn=cnt;
field=temp;
}
} void print() {
if(minn==0xFF) puts("Impossible");
else printf("%d\n", minn);
} int main() {
read();
work();
print();
return ;
}

POJ 1753 枚举

三、高斯消元法

  1、基本想法是,令a=棋盘状态矩阵,b=最终各棋子的状态,ax=b解出x=要翻转的棋子,数一下x里面1的数量就是翻转的棋子数了。因为最终状态可以是全黑或全白,因此需要对b取两次值,做两次消元。

  2、但是你会发现,这题会经常出现无穷多解的情况,也就是存在自由变元。因此需要枚举or搜索这些自由变元的值。

  (代码目前没交,待更新)

——原创by BlackStorm,转载请注明出处。

本文地址:http://www.cnblogs.com/BlackStorm/p/5231470.html

POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法的更多相关文章

  1. poj 1753 Flip Game 枚举(bfs+状态压缩)

    题目:http://poj.org/problem?id=1753 因为粗心错了好多次……,尤其是把1<<15当成了65535: 参考博客:http://www.cnblogs.com/k ...

  2. 枚举 POJ 1753 Flip Game

    题目地址:http://poj.org/problem?id=1753 /* 这题几乎和POJ 2965一样,DFS函数都不用修改 只要修改一下change规则... 注意:是否初始已经ok了要先判断 ...

  3. POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治

    The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22286 ...

  4. POJ 1753 Flip Game(高斯消元+状压枚举)

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 45691   Accepted: 19590 Descr ...

  5. POJ 1753 Flip Game DFS枚举

    看题传送门:http://poj.org/problem?id=1753 DFS枚举的应用. 基本上是参考大神的.... 学习学习.. #include<cstdio> #include& ...

  6. POJ 1753 Flip Game (DFS + 枚举)

    题目:http://poj.org/problem?id=1753 这个题在開始接触的训练计划的时候做过,当时用的是DFS遍历,其机制就是把每一个棋子翻一遍.然后顺利的过了.所以也就没有深究. 省赛前 ...

  7. POJ 1753 Flip Game(二进制枚举)

    题目地址链接:http://poj.org/problem?id=1753 题目大意: 有4*4的正方形,每个格子要么是黑色,要么是白色,当把一个格子的颜色改变(黑->白或者白->黑)时, ...

  8. POJ - 1753 Flip Game(状压枚举)

    https://vjudge.net/problem/POJ-1753 题意 4*4的棋盘,翻转其中的一个棋子,会带动邻接的棋子一起动.现要求把所有棋子都翻成同一种颜色,问最少需要几步. 分析 同一个 ...

  9. POJ 1753 Flip Game【枚举】

    题目链接: http://poj.org/problem?id=1753 题意: 由白块黑块组成的4*4方格,每次换一个块的颜色,其上下左右的块也会被换成相反的颜色.问最少换多少块,使得最终方格变为全 ...

随机推荐

  1. redis数据结构详解之Hash(四)

    序言 Hash数据结构累似c#中的dictionary,大家对数组应该比较了解,数组是通过索引快速定位到指定元素的,无论是访问数组的第一个元素还是最后一个元素,所耗费的时间都是一样的,但是数组中的索引 ...

  2. 高性能JavaScript--数据存储(简要学习笔记二)

    1.JavaScript中四种基本数据存取位置:字面量,本地变量,数组元素,对象成员. 一般来说:[字面量,局部变量]运行速度>[数组,对象成员]   2.内部属性包含了一个函数被创建的作用域中 ...

  3. WPF自定义RoutedEvent事件代码段

    今天在写东西的时候,发现常用的代码段里没有RoutedEvent的,因此,写了一个代码段,方便以后使用,顺便记录一下,如何做代码段. 1.在项目中新建一个XML文件,将扩展名修改为snippet. 2 ...

  4. 学习笔记之(console)

    今天小颖在逛博客园时,发现一位帅锅写的有意思的Console小颖看了后,就自己敲了一遍嘻嘻,为了方便以后查看,小颖把它记录下来嘻嘻,有兴趣的小伙伴也可以自己试试哦. 格式占位符 作用 %s 字符串 % ...

  5. ASP.NET Core 中文文档 第一章 入门

    原文:Getting Started 翻译:娄宇(Lyrics) 校对:刘怡(AlexLEWIS) 1.安装 .NET Core 2.创建一个新的 .NET Core 项目: mkdir aspnet ...

  6. 数百个 HTML5 例子学习 HT 图形组件 – 3D建模篇

    http://www.hightopo.com/demo/pipeline/index.html <数百个 HTML5 例子学习 HT 图形组件 – WebGL 3D 篇>里提到 HT 很 ...

  7. 基于HTML5的WebGL应用内存泄露分析

    上篇(http://www.hightopo.com/blog/194.html)我们通过定制了CPU和内存展示界面,体验了HT for Web通过定义矢量实现图形绘制与业务数据的代码解耦及绑定联动, ...

  8. Angular2 小贴士 RouterLink 导航

    AngularJS的路由一直是学习的一大难点,我们只能边看边学边掌握,边看边学边推翻.今天我们来看一下在angular2中通过routerLink实现导航的几种方式,以及各自的优缺点. Angular ...

  9. RabbitMQ Config

    默认访问地址:http://localhost:15672/ 要想修改内网访问: %APPDATA%\RabbitMQ\ 目录下添加文件 rabbitmq.config [ {rabbit, [%% ...

  10. ICSharpCode.SharpZipLib 压缩、解压文件 附源码

    http://www.icsharpcode.net/opensource/sharpziplib/ 有SharpZiplib的最新版本,本文使用的版本为0.86.0.518,支持Zip, GZip, ...