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. Couchbase

    Couchbase介绍,更好的Cache系统 在移动互联网时代,我们面对的是更多的客户端,更低的请求延迟,这当然需要对数据做大量的 Cache 以提高读写速度. 术语 节点:指集群里的一台服务器. 现 ...

  2. EL表达式简介

    EL表达式简介 EL 全名为Expression Language.EL主要作用: 1.获取数据 EL表达式主要用于替换JSP页面中的脚本表达式,以从各种类型的web域 中检索java对象.获取数据. ...

  3. 161125、Java网络编程之统一资源定位符URL

    统一资源定位符URL(Uniform Resource Locator)是www客户机访问Internet时用来标识资源的名字和地址.超文本链路由统一资源定位符URL维持.URL的格式是: <M ...

  4. 给MD5加上salt随机盐值加密算法实现密码安全的php实现

    给MD5加上salt随机盐值加密算法实现密码安全的php实现 如果直接对密码进行散列,那么黑客可以对通过获得这个密码散列值,然后通过查散列值字典(例如MD5密码破解网站),得到某用户的密码.加上sal ...

  5. iOS 解决的问题

    1. 字符超过一定长度会闪退. 2. 发送完会弹出警告框. 3. 加入语音. 4. 连接按钮做peripheval是否为空的判断.

  6. Getting Started with the C# Driver

    1.下载 如果下载的.zip文件,只需要解压即可. 如果安装的.msi文件,它会将C#驱动DLL放在C:\Program Files (x86)\MongoDB\CSharp Driver xxx的位 ...

  7. Centos7下用命令下载jdk7及jboss-eap-6

    计划在南非的一台云主机上搭建一个web环境,首先需要在云主机上搭建我们指定版本的JDK和JBOSS. 在云上搭特定版本的环境,软件包传输是一件十分艰巨的任务.我先后尝试了:公司电信专线.公司联通专线. ...

  8. Centos6.5和Centos7 php环境搭建如何实现呢

    首先我们先查看下centos的版本信息 代码如下: #适用于所有的linux lsb_release -a#或者cat /etc/redhat-release#又或者rpm -q centos-rel ...

  9. Window驱动开发

    驱动开发 参考文章: Windbg+Vmware驱动调试 http://blog.csdn.net/xuepiaosong/article/details/8236702 驱动调试攻略(WinDbg) ...

  10. uname -r和uname -a了解

    1.uname -r :显示操作系统的发行版号2.uname -a :显示系统名.节点名称.操作系统的发行版号.操作系统版本.运行系统的机器 ID 号. #uname -aHP-UX RX1600 B ...