Flip Game
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 25573   Accepted: 11052

题目链接:http://poj.org/problem?id=1753

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

代码实现:
 #include<stdio.h>
#include<string.h>
#include<stdlib.h>
int map[][];
int step;
int flag;
int check()
{
int i,j;
for(i=;i<=;i++)
for(j=;j<=;j++)
if(map[i][j]!=map[][])
return ;
return ;
}
void fanzhuan(int x,int y)
{
map[x][y+]=!map[x][y+];
map[x][y-]=!map[x][y-];
map[x+][y]=!map[x+][y];
map[x-][y]=!map[x-][y];
map[x][y]=!map[x][y];
}
void dfs(int x,int y,int sum)
{
if(sum==step)
{
flag=check();
return ;
}
if(x<=)
{
fanzhuan(x,y);
if(y<=)//必须是3,因为下面的y+1让y变成了4,达到最大值
dfs(x,y+,sum+);
else dfs(x+,,sum+);
if(flag==)return ;
else
{
fanzhuan(x,y);
if(y<=)
dfs(x,y+,sum);
else
dfs(x+,,sum);
}
}
else return ;
}
int main()
{
memset(map,,sizeof(map));
int i;
char str[][];
for(i=;i<=;i++)
scanf("%s",str[i]);
int t;
for(i=;i<=;i++)
{
for(t=;t<=;t++)
if(str[i][t]=='b')
map[i+][t+]=;
}
for(step=;step<=;step++)
{
dfs(,,);
if(flag)
{
printf("%d\n",step);
break;
}
}
if(flag==)printf("Impossible\n");
return ;
}

Filp Game的更多相关文章

  1. php扩展1:filp/whoops(用于调试,方便定位错误点)

    一.composer下载filp/whoops: 1.在composer.json中添加:"filp/whoops": "*",如下所示: 2.执行compos ...

  2. matlab学习笔记11_3高维数组操作 filp, shiftdim, size, permute, ipermute

    一起来学matlab-matlab学习笔记11 11_3 高维数组处理和运算 filp, shiftdim, size, permute, ipermute 觉得有用的话,欢迎一起讨论相互学习~Fol ...

  3. Metro Win8风格的按钮(Filp翻转)

    原地址->http://www.cnblogs.com/yk250/p/5661093.html 介绍:简约而不简单....颜色可随意调制,最好用Blend工具. 效果图如下:话说这个图会不会太 ...

  4. java Channel filp compact

    import java.nio.ByteBuffer; //Listing 7-1. Copying Bytes from an Input Channel to an Output Channel ...

  5. Bringing Whoops Back to Laravel 5

    You might be missing the "prettier" Whoops error handler from Laravel 4. If so, here's how ...

  6. Linux设备管理(二)_从cdev_add说起

    我在Linux字符设备驱动框架一文中已经简单的介绍了字符设备驱动的基本的编程框架,这里我们来探讨一下Linux内核(以4.8.5内核为例)是怎么管理字符设备的,即当我们获得了设备号,分配了cdev结构 ...

  7. Linux字符设备驱动框架

    字符设备是Linux三大设备之一(另外两种是块设备,网络设备),字符设备就是字节流形式通讯的I/O设备,绝大部分设备都是字符设备,常见的字符设备包括鼠标.键盘.显示器.串口等等,当我们执行ls -l ...

  8. blocking and unblocking mechanism for linux drivern code

    概念: 1> 阻塞操作      是指在执行设备操作时,若不能获得资源,则挂起进程直到满足操作条件后再进行操作.被挂起的进程进入休眠,被从调度器移走,直到条件满足: 2> 非阻塞操作  在 ...

  9. Linux基礎知識 —— open&close

    下面說一下在用戶空間調用open/close/dup跟驅動中的open和release的對應. 下面是測試驅動: #include <linux/module.h> #include &l ...

随机推荐

  1. linux shell脚本常用语句

    linux shell 指令 诸如-d, -f, -e之类的判断表达式: 文件比较运算符-e filename  如果 filename存在,则为真  [ -e /var/log/syslog ]-d ...

  2. MySql大数据量恢复

    用下面方法解决(管理mysql用的是navicat).,设置以下几个参数的值后就正常了,以下语句也可以在mysql的控制台上执行 . show variables like '%timeout%'; ...

  3. phpcms不能后台编辑模板

    刚安装的phpcms后台无法编辑模板,实际上只需要改一个系统文件的值即可 打开phpcms项目根目录下的: caches/configs/system.php 找到第20行,代码如下: 'tpl_ed ...

  4. wxPython中文教程入门实例

    这篇文章主要为大家分享下python编程中有关wxPython的中文教程,分享一些wxPython入门实例,有需要的朋友参考下     wxPython中文教程入门实例 wx.Window 是一个基类 ...

  5. 在VS中MFC、ATL与WIN32有什么联系或区别?

    有时候遇到一些初学者问我这个问题:在VS中使用MFC和ATL与使用WIN32有什么联系或区别?通俗来说,win32是通过调用windows api去实现需要的功能.而MFC和ATL是封装好的类库,包含 ...

  6. eclipse 工作环境配置

    1.更换编辑颜色, http://eclipse-color-theme.github.io/update/ 下载离线安装包,解压缩 eclipse-color-theme-update-site\u ...

  7. Lock+Condition 相对于 wait+notify 的一个优势案例分析

    问题的描述 启动3个线程打印递增的数字, 线程1先打印1,2,3,4,5, 然后是线程2打印6,7,8,9,10, 然后是线程3打印11,12,13,14,15. 接着再由线程1打印16,17,18, ...

  8. 数组里的数据绑定到dataset中

    string [] an = {"a","b","c"};DataTable dt = new DataTable(); dt.Column ...

  9. ASM:《X86汇编语言-从实模式到保护模式》第12章:存储器的保护

    12章其实是11章的拓展,代码基本不变,就是在保护模式下展开讨论. ★PART1:存储器的保护机制 1. 修改段寄存器的保护 当执行把段选择子传到段寄存器的选择器部分的时候,处理器固件在完成传送之前, ...

  10. codeforces 514B. Han Solo and Lazer Gun 解题报告

    题目链接:http://codeforces.com/problemset/problem/514/B 题目意思:给出双头枪的位置(x0, y0),以及 n 个突击队成员的坐标.双头枪射击一次,可以把 ...