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. IC/RFID/NFC 关系与区别

    IC卡 (Integrated Circuit Card,集成电路卡) 有些国家和地区也称智能卡(smart card).智慧卡(intelligent card).微电路卡(microcircuit ...

  2. Calico在Docker中的搭建

    一,Multi-host网络需求 开始之前推荐两篇文章 http://xelatex.github.io/2015/11/15/Battlefield-Calico-Flannel-Weave-and ...

  3. TCP的几个状态

    转自: TCP的几个状态 (SYN, FIN, ACK, PSH, RST, URG)     http://www.cnblogs.com/lidabo/p/5713569.html

  4. iOS 关于iphone6 和 iphone6 plus 的适配

    http://www.ui.cn/detail/26980.html 根据上面说的,iphone6 plus的屏幕的编程时的宽度应该是414,我理解的也是这样,但是我用iphone6 plus 模拟器 ...

  5. Linux-PAM认证模块

    Linux-PAM认证模块     用户访问服务器的时候,服务器的某一个服务程序把用户的谁请求发送到PAM模块进行认证.对于不同的服务器应用程序所对应的PAM模块也是不同的.如果想查看某个程序是否支持 ...

  6. angular.js初探

    2015年7月27日 22:26:35 星期一 用在我论坛里的小栗子: 先列出来一级回帖, 点击帖子前边的"查看回复"按钮无刷新的去请求该帖子的所有回复 首先要引用js文件, 我这 ...

  7. MySQL 主键范围查找问题

    背景: 今天遇到一个主键范围查找的情况: id是主键,每次取10000.上面的这个查询id范围越往后面消耗的时间越久.通过id自增主键去查找数据应该不会出现这个现象的.以前都没有注意这个奇怪的现象,现 ...

  8. HTML DOM scale() 方法

    语法 scale(sx, sy) 参数 参数 描述 sx, sy 水平和垂直的缩放因子. 描述 scale() 方法为画布的当前变换矩阵添加一个缩放变换.缩放通过独立的水平和垂直缩放因子来完成.例如, ...

  9. C#中XmlTextWriter读写xml文件详细介绍(转)

    转自http://www.jb51.net/article/35230.htm   .NET中包含了很多支持XML的类,这些类使得程序员使用XML编程就如同理解XML文件一样简单.在这篇文章中,我将给 ...

  10. 【leetcode】atoi (hard) ★

    虽然题目中说是easy, 但是我提交了10遍才过,就算hard吧. 主要是很多情况我都没有考虑到.并且有的时候我的规则和答案中的规则不同. 答案的规则: 1.前导空格全部跳过  “      123” ...