Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 32961   Accepted: 14407

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

dfs枚举

 #include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std; int Map[][],a[]={,,,,-},b[]={,,,-,};
int flg,step; int judge(int Map[][])
{
int i,j;
for(int i=;i<=;i++)
for(j=;j<=;j++)
{
if(Map[i][j]!=Map[][])
return ;
}
return ;
} int flip(int r,int c)
{
for(int i=;i<;i++)
{
if(Map[r+a[i]][c+b[i]]==)
Map[r+a[i]][c+b[i]]=;
else
Map[r+a[i]][c+b[i]]=;
}
} void dfs(int r,int c,int deep)
{
int i,j;
if(deep==step)
{
flg=judge(Map);
return;
}
if(flg== || r>)
{
return;
}
flip(r,c);
if(c<)
dfs(r,c+,deep+);
else
dfs(r+,,deep+);
flip(r,c);
if(c<)
dfs(r,c+,deep);
else
dfs(r+,,deep);
return;
} int main()
{
int i,j;
char k;
memset(Map,,sizeof(Map));
for(i=;i<=;i++)
{
for(j=;j<=;j++)
{
scanf("%c",&k);
if(k=='b')
Map[i][j]=;
}
getchar();
}
for(step=;step<=;step++)
{
dfs(,,);
if(flg)
break;
}
if(flg)
printf("%d\n",step);
else
printf("Impossible\n");
return ;
}

Flip Game poj1753的更多相关文章

  1. dfs关于按钮问题(flip游戏POJ1753)以及和bfs的区别+板子

    DFS深度搜索:之前一直和bfs的用法搞不太清楚:写了题才能慢慢参透吧,看了别的博客的代码,感觉能更好理解dfs在图中的应用: 这个题目的意思是一个人去救另一个人,找出最短的寻找路径: #includ ...

  2. poj1753 Flip Game(BFS+位压缩)

    题目链接 http://poj.org/problem?id=1753 题意 一个棋盘上有16个格子,按4×4排列,每个格子有两面,两面的颜色分别为黑色和白色,游戏的每一轮选择一个格子翻动,翻动该格子 ...

  3. poj1753,Flip Game,ArrayDeque&lt;Node&gt;

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 30449   Accepted: 13232 Descr ...

  4. POJ1753 Flip Game(bfs、枚举)

    链接:http://poj.org/problem?id=1753 Flip Game Description Flip game is played on a rectangular 4x4 fie ...

  5. poj1753 Flip Game

    题意:4*4的正方形,每个格子有黑白两面,翻转格子使得4*4个格子显示全黑或全白,翻转要求:选中的那个格子,以及其上下左右相邻的格子(如果存在)要同时翻转.输出最小的达到要求的翻转次数或者Imposs ...

  6. POJ1753——Flip Game

    Flip Game Description Flip game is played on a rectangular 4x4 field with two-sided pieces placed on ...

  7. POJ-1753 Flip Game---二进制枚举子集

    题目链接: https://vjudge.net/problem/POJ-1753 题目大意: 有4*4的正方形,每个格子要么是黑色,要么是白色,当把一个格子的颜色改变(黑->白或者白-> ...

  8. POJ-1753 Flip Game (BFS+状态压缩)

    Description Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of i ...

  9. POJ1753 Flip Game(位运算+暴力枚举)

    Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 16 square ...

随机推荐

  1. zw版【转发·台湾nvp系列Delphi例程】HALCON SelectObj

    zw版[转发·台湾nvp系列Delphi例程]HALCON SelectObj procedure TForm1.Button1Click(Sender: TObject);var img : HIm ...

  2. SSAS更改默认端口号,使用非默认端口号的时候Olap连接字符串的格式

    Sql server的Analysis Service服务默认使用的是2382或2383端口,但是实际上我们可以通过配置文件手动更改SSAS使用其它端口号. 修改SSAS使用端口号的方法如下,找到你的 ...

  3. Sql server analysis service 通过IIS连接时的最大连接数问题

    做过SSAS项目的大部分人都应该知道SSAS是可以通过在IIS上建立代理站点的方式来建立远程连接的,这样可以绕过连接到SSAS时需要在同一个域环境下通过域用户来验证的问题,这样即使连接到SSAS的客户 ...

  4. [转][Automation]- C# SendKey代码表

    使用 SendKeys 将键击和组合键击发送到活动应用程序.此类无法实例化.若要发送一个键击给某个类并立即继续程序流,请使用 Send.若要等待键击启动的任何进程,请使用 SendWait. 每个键都 ...

  5. c++实现mlp神经网络

    之前一直用theano训练样本,最近需要转成c或c++实现.在网上参考了一下其它代码,还是喜欢c++.但是看了几份cpp代码之后,发现都多少有些bug,很不爽.由于本人编码能力较弱,还花了不少时间改正 ...

  6. hadoop之Spark强有力竞争者Flink,Spark与Flink:对比与分析

    hadoop之Spark强有力竞争者Flink,Spark与Flink:对比与分析 Spark是一种快速.通用的计算集群系统,Spark提出的最主要抽象概念是弹性分布式数据集(RDD),它是一个元素集 ...

  7. webpack笔记_(1)_webpack 安装

    webpack不仅可以解析jsx,也可以将es6转换为es5语法.最终,它把这些代码都打包成一个叫bundle.js的文件,我们在html文件中只引入这么一个js文件就可以了! 打包后,引用的语法im ...

  8. Char、AnsiChar、WideChar、PChar、PAnsiChar、PWideChar 的用法

     varc: Char; {Char 类型的取值范围是: #0..#255, 用十六进制表示是: #$0..#$FF}begin{用十进制方式赋值:}c := #65;ShowMessage(c); ...

  9. 虚拟机 本地 本机 双启动 运行 vhd local Dual Boot

    在使用虚拟机的过程中, 可能会遇到虚拟机的运行要求过高, 电脑力不从心的情况. 为了让虚拟机使用更多电脑资源, 可以让虚拟机以本地双系统的方式,访问本地计算机资源. 打开磁盘管理,在磁盘上右键,选择 ...

  10. js中字符串转换为数字的方法

    parseInt; parseFload; +; parseInt() 和 parseFloat() 函数会尝试逐个解析字符串中的字符,直到遇上一个无法被解析成数字的字符,然后返回该字符前所有数字字符 ...