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. QT5笔记:关闭应用程序和窗口的函数

    23333 QT一坨,求一门面向傻瓜的语言. QT中 quit(),exit()以及close():常用的三个槽   对主程序的退出,可以调用成员函数exit(),同时也可以调用槽quit(),二者此 ...

  2. Unity3d《Shader篇》描边

    Shader "Custom/OutLine" {Properties { _Color ("Main Color", Color) = (.5,.5,.5,1 ...

  3. mysql备份与还原

    一.直接拷贝数据库文件 直接拷贝数据库文件一般是使用文件系统备份工具cp,适合小型数据库,是最可靠的. 当你拷贝数据库文件时,必须保证表没有正在使用.如果服务器在你拷贝一个表的时候改变这个表,拷贝就失 ...

  4. 1.【转】spring MVC入门示例(hello world demo)

    1. Spring MVC介绍 Spring Web MVC是一种基于Java的实现了Web MVC设计模式的请求驱动类型的轻量级Web框架,即使用了MVC架构模式的思想,将web层进行职责解耦,基于 ...

  5. Class和ClassLoader的getResourceAsStream区别

    这两个方法还是略有区别的, 以前一直不加以区分,直到今天发现要写这样的代码的时候运行 错误, 才把这个问题澄清了一下. 基本上,两个都可以用于从 classpath 里面进行资源读取,  classp ...

  6. 【leetcode】Merge Intervals(hard)

    Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...

  7. php数据访问(查询)

    查询:常用关键字查询 和 准确查询 单条件查询 创建添加查询元素 <br /> <form action="main.php" method="post ...

  8. ramnit病毒

    今天打开Hbuilder,发现每个html文档都有如下代码: <SCRIPT Language=VBScript><!--DropFileName = "svchost.e ...

  9. Java反射实战

    一.背景 最近的项目中需要使用到Java 反射的知识,以前不怎么了解,也基本没怎么用过,抽出一片时间,来具体学习和实战下Java的反射!拿来和大家分享以及记录方便以后学习! 二.反射相关概念解析 1. ...

  10. July 9th, Week 28th Saturday, 2016

    Every cloud has a silver lining. 山穷水尽疑无路,柳暗花明又一村. Every cloud has a silver lining, that just because ...