Flip Game
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 26492   Accepted: 11422

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

Northeastern Europe 2000

题意:

有一个4*4的正方形,每个格子上的棋子(即块状物)有正反两面,一面黑色,一面白色,当一个格子里的棋子被翻转时,其周围的四个棋子(如果存在的话)也被翻转,问至少翻转几个格子可以使4*4的正方形里的棋子变为全白或全黑?

思路:

一开始凭感觉吧,同个棋子被翻转2次的话好像作用和未翻转一样,那题目就成了一个棋子是否被翻转的问题了(要么被翻转1次,要么不被翻转)。

那就想到枚举每个棋子的情况,最多枚举 2 ^ (4 * 4) = 2 ^ 16 次,应该可以过。

然后又想到以前书上看过类似的一题,枚举第一排就能推出之后的几排的情况,然后翻书,继续想想,就想通了。确实当第一排被确定时可以推出下面几排的情况。而且只需要枚举 2 ^ 4 * 2次(乘以2是考虑到翻转成全白或者全黑两种情况)即可。

/*************************************************************************
> File Name: POJ1753.c
> Author: BSlin
> Mail:
> Created Time: 2013年10月30日 星期三 19时44分55秒
************************************************************************/ #include <stdio.h>
#include <string.h>
#define INF 20 int map1[5][5],map2[5][5]; int min(int a, int b) {
return a > b ? b : a;
} int inmap(int x, int y) {
if(x >= 0 && x < 4 && y >= 0 && y < 4)
return 1;
return 0;
} void change(int x, int y) {
if(inmap(x,y)) {
map2[x][y] = 1 - map2[x][y];
}
if(inmap(x-1,y)) {
map2[x-1][y] = 1 - map2[x-1][y];
}
if(inmap(x,y+1)) {
map2[x][y+1] = 1 - map2[x][y+1];
}
if(inmap(x+1,y)) {
map2[x+1][y] = 1 - map2[x+1][y];
}
if(inmap(x,y-1)) {
map2[x][y-1] = 1 - map2[x][y-1];
}
} int check(int s, int num) {
int i, j, cnt, x, y;
for(i=0; i<4; i++) {
for(j=0; j<4; j++) {
map2[i][j] = map1[i][j];
}
}
cnt = 0;
for(j=0; j<4; j++) {
if(s & (1 << j)) {
cnt ++;
change(0,j);
}
}
//if(s == 1) {
//for(x=0; x<4; x++) {
//for(y=0; y<4; y++) {
//printf("%d",map2[x][y]);
//}
//printf("\n");
//}
//printf("\n");
//}
for(i=0; i<3; i++) {
for(j=0; j<4; j++) {
if(map2[i][j] != num) {
cnt ++;
change(i+1,j);
//if(s == 1) {
//for(x=0; x<4; x++) {
//for(y=0; y<4; y++) {
//printf("%d",map2[x][y]);
//}
//printf("\n");
//}
//printf("\n");
//}
}
}
}
for(j=0; j<4; j++) {
if(map2[3][j] != num)
return INF;
}
return cnt;
} int main() {
freopen("in.txt","r",stdin);
int i,j,ans;
char ch;
for(i=0; i<4; i++) {
for(j=0; j<4; j++) {
scanf("%c",&ch);
if(ch == 'b') {
map1[i][j] = 1;
} else if(ch == 'w') {
map1[i][j] = 0;
}
}
getchar();
}
ans = INF;
for(i=0; i<16; i++) {
ans = min(ans, check(i,0));
}
for(i=0; i<16; i++) {
ans = min(ans, check(i,1));
}
if(ans == INF) {
printf("Impossible\n");
} else {
printf("%d\n",ans);
}
}

POJ 1753 Flip Game (枚举)的更多相关文章

  1. POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法

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

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

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

  3. 枚举 POJ 1753 Flip Game

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

  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【枚举】

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

  9. poj 1753 Flip Game(bfs状态压缩 或 dfs枚举)

    Description Flip game squares. One side of each piece is white and the other one is black and each p ...

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

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

随机推荐

  1. C++雾中风景7:闭包

    本来说好要聊一聊命名空间的,因为最近在看C++lambda表达式的内容,所以借这个机会我们来好好聊一聊C++的闭包. 1.什么是闭包? 闭包(closure)是函数式编程的重要的语法结构. 闭包的概念 ...

  2. Python 中的面向对象和异常处理

    在之前我们已经说过了 Python 中内置的主要的几种对象类型,(数,字符串,列表,元组和字典).而面向对象的核心人物还没出场呢 .那么我们常说的对象是什么类型的呢,其实他的类型就是“类”.继承封装和 ...

  3. Linux-UDP-Socket编程

    接收CAN总线上的数据并将其发送出去 创建客户端: /******************************************************************** * co ...

  4. js包

    1.base.js /*语法: $("选择器") 工厂函数 */       /*寻找页面中name属性值是haha的元素*/   $("[name='haha']&qu ...

  5. Windows系统php5.6安装Imagick库

    Windows上的安装坑比较多 1.安装Imagick,需要下载6.9.3之下版本的 http://imagemagick.org/script/download.php 官网都是新版本不可以用 我安 ...

  6. [BZOJ4850][JSOI2016]灯塔(分块/决策单调性优化DP)

    第一种方法是决策单调性优化DP. 决策单调性是指,设i>j,若在某个位置x(x>i)上,决策i比决策j优,那么在x以后的位置上i都一定比j优. 根号函数是一个典型的具有决策单调性的函数,由 ...

  7. bzoj 4573: [Zjoi2016]大森林 lct splay

    http://www.lydsy.com/JudgeOnline/problem.php?id=4573 http://blog.csdn.net/lych_cys/article/details/5 ...

  8. poj 3667 线段树

    题意:1 a:询问是不是有连续长度为a的空房间,有的话住进最左边2 a b:将[a,a+b-1]的房间清空思路:记录区间中最长的空房间线段树操作:update:区间替换 query:询问满足条件的最左 ...

  9. bzoj 3997 Dilworth定理

    看到这道题感觉像是网络流,如果没有权值,可以用DAG最小路径覆盖,有权值,感觉可以求一个上下界最小可行流,但内存卡了....时间估计也悬. 正解要用到一些数学知识,这里梳理一下: 定义: 偏序关系: ...

  10. ICD2 VPP limiter for new PIC microcontrollers.

    http://www.circuitsathome.com/mcu/pic_vpp_limiter VOUT = 2.5V * ( 1 + 24/10 ) = 2.5 * 3.4 = 8.5V New ...