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. 有了这套flex页面布局方案,页面什么的,那都不是事。

    一.CSS3弹性盒子弹性盒子是CSS3的一种新布局模式.CSS3 弹性盒( Flexible Box 或 flexbox),是一种当页面需要适应不同的屏幕大小以及设备类型时确保元素拥有恰当的行为的布局 ...

  2. XShell通过中转服务器直接连接目标服务器

    最近由于公司生产环境的变化,使得我们不能使用自己的机器连接到生产环境去,而是要通过跳板机中转才可以连接.于是今天尝试使用 XShell 通过跳板机直接转接到生产环境. 一.使用代理方式 首先填写连接信 ...

  3. sublime3176注册码破解汉化及常用插件

    官方网站下载地址:https://www.sublimetext.com/3 破解软件下载地址:https://www.lanzous.com/i1a7zfi 破解软件下载地址备用:https://d ...

  4. C语言学习常见错误分析

    错误分类     语法错 逻辑错 运行错 0.忘记定义变量: int main() { x=3;y=6;  printf("%d/n",x+y); } 1.C语言的变量一定要先定义 ...

  5. hdu 5316 Magician(2015多校第三场第1题)线段树单点更新+区间合并

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5316 题意:给你n个点,m个操作,每次操作有3个整数t,a,b,t表示操作类型,当t=1时讲a点的值改 ...

  6. 解决CIFilter滤镜后图片大小和方向发生变化

    调用contextWithOptions:和createCGImage: fromRect:方法创建CIContext.与以往不同的地方是CIImage没有frame与bounds属性:只有exten ...

  7. 我们为什么需要Map-Reduce?

    在讨论我们是否真的需要Map-Reduce这一分布式计算技术之前,我们先面对一个问题,这可以为我们讨论这个问题提供一个直观的背景. 问题 我们先从最直接和直观的方式出发,来尝试解决这个问题: 先伪一下 ...

  8. SQL2008″Unable to read the list of previously registered servers on this system”

    打开SQL2008,弹出”Unable to read the list of previously registered servers on this system”错误, 微软官方的解决方法:h ...

  9. xcode .h文件编译时 版本不正确

    在终端里面   执行下面的命令 rm -rf ~/Library/Developer/Xcode/DerivedData/ModuleCache/*

  10. 关于ANDROID模拟器的一些事

    转载请注明本文出自大苞米的博客(http://blog.csdn.net/a396901990),谢谢支持! 继上一篇Android Studio VS Eclipse的文章后接着来分享AnDevCo ...