Flip Game
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 32384   Accepted: 14142

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<iostream>
using namespace std;
bool a[][] = {false} ;
int r[] = {- , , , , } ;
int c[] = { , , - , , } ;
bool flag ;
int step ; bool judge_all ()
{
for (int i = ; i < ; i++) {
for (int j = ; j < ; j++) {
if ( a[i][j] != a[][])
return false ;
}
}
return true ;
} void flip (int row , int col)
{
for (int i = ; i < ; i++) {
a[row + r[i]][col + c[i]] = !a[row + r[i]][col + c[i]] ;
}
} void dfs (int row , int col , int deep)
{
if (deep == step) {
flag = judge_all () ;
return ;
} if (flag || row == )
return ; flip (row , col) ;
if (col < )
dfs (row , col + , deep + ) ;
else
dfs (row + , , deep + ) ;
// printf ("row = %d, col = %d, deep = %d\n" , row , col , deep) ;
flip (row , col) ;
if (col < )
dfs (row , col + , deep) ;
else
dfs (row + , , deep) ; return ;
} int main ()
{
// freopen ("a.txt" , "r" , stdin) ;
char x ;
for (int i = ; i < ; i++) {
for (int j = ; j < ; j++) {
cin >> x ;
if (x == 'b')
a[i][j] = ;
}
} for (step = ; step <= ; step++) {
dfs ( , , ) ;
if (flag)
break ;
}
/*step = 2 ;
dfs ( 1 , 1 , 0) ;*/ if (flag)
printf ("%d\n" , step) ;
else
printf ("Impossible\n") ; return ;
}

不得不说用递归写很有诀窍的,感觉这道题就是把暴力写成了递归。(虽然叫dfs)

cin 原来不收 “换行符” 。

思路:

最少0步, 最多16步。先检查0步,再1步 , 2步……,以此类推。

转载:http://www.cnblogs.com/lyy289065406/archive/2011/07/29/2120501.html

Flip Game(dfs)的更多相关文章

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

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

  2. 素数环(dfs+回溯)

    题目描述: 输入正整数n,把整数1,2...n组成一个环,使得相邻两个数和为素数.输出时从整数1开始逆时针排列并且不能重复: 例样输入: 6 例样输出: 1 4 3 2 5 6 1 6 5 2 3 4 ...

  3. (DFS)codevs1004-四子连棋

    题目地址 方法是建立dfs,并在其中加入pre变量,记录之前移动的是W还是B.外面套for循环,从1步开始逐次递增,直到在i步时可以走完(dfs返回1),break退出循环,即为最短步. 本题的关键主 ...

  4. LeetCode Subsets II (DFS)

    题意: 给一个集合,有n个可能相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: 看这个就差不多了.LEETCODE SUBSETS (DFS) class Solution { publ ...

  5. LeetCode Subsets (DFS)

    题意: 给一个集合,有n个互不相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: DFS方法:由于集合中的元素是不可能出现相同的,所以不用解决相同的元素而导致重复统计. class Sol ...

  6. HDU 2553 N皇后问题(dfs)

    N皇后问题 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description 在 ...

  7. POJ 4003 Bob’s Race && HDU4123 Bob’s Race (dfs+rmq)

    Bob’s Race Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 378   Accepted: 119 Descript ...

  8. 深搜(DFS)广搜(BFS)详解

    图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...

  9. 【算法导论】图的深度优先搜索遍历(DFS)

    关于图的存储在上一篇文章中已经讲述,在这里不在赘述.下面我们介绍图的深度优先搜索遍历(DFS). 深度优先搜索遍历实在访问了顶点vi后,访问vi的一个邻接点vj:访问vj之后,又访问vj的一个邻接点, ...

随机推荐

  1. Windows Phone & Windows App应用程序崩溃crash信息抓取方法

    最近有用户反馈,应用有崩溃的情况,可是本地调试却无法重现问题,理所当然的,我想到了微软的开发者仪表盘,可以查看一段时间内的carsh记录,不过仪表盘生成carsh记录不是实时的,而且生成的报告查看非常 ...

  2. Bootstrap系列 -- 21. 表单提示信息

    平常在制作表单验证时,要提供不同的提示信息.在Bootstrap框架中也提供了这样的效果.使用了一个"help-block"样式,将提示信息以块状显示,并且显示在控件底部. < ...

  3. Aspose.Cells 读取受保护的Excel

    最近遇到一个需求,要能够读取受密码保护的Excel内容,之前都是直接读取Excel中的数据,不需要做任何其他的处理.   当Excel双击的时候,需要输入密码,在使用Aspose.Cells 组件读取 ...

  4. RockWare RockWorks的Ollydbg调试过程及注册机(破解)思路

    最近拿到了RockWorks15的安装包,可惜没有破解,试用也只能用14天.用PEiD工具察看了一下,Delphi编写的程序,竟然没加壳.本想用OllyDBG调试进去爆破一下,不意发现注册码很简单,如 ...

  5. Linq 中查询一个表中指定的字段

    //Linq中查询一个表中指定的几个字段: ); // FindAllItems()为查询对应表的所有数据的方法: // Where 里面为查询条件 // Select 为查询的筛选条件 new{} ...

  6. redis学习笔记——(1)

    1. NoSQL&Redis介绍 NoSQL,Not Only SQL,是非关系型的数据库.传统的关系数据库不能满足超大规模和高并发的应用. 是以Key-Value的形式存储,(例如JSON, ...

  7. WRONGTYPE Operation against a key holding the wrong kind of value

    今天改动代码,一运行就跑错了,错误原因: 因为redis中已经存在了相同的key, 而且key对应的值类型并不是Set,而是SortSet(改动前):再调用smembers时,抛出此错误. 解决方法: ...

  8. 由DataGridTextColumn不能获取到父级DataContext引发的思考

    在项目中使用DataGrid需要根据业务动态隐藏某些列,思路都是给DataGrid中列的Visibility属性绑定值来实现(项目使用MVVM),如下 <DataGridTextColumn H ...

  9. 【CodeForces 616D】Longest k-Good Segment

    题意 n个数里,找到最长的一个连续序列使里面最多k个不同的数. 分析 尺取法,每次R++,如果第R个数未出现过,那么不同的数+1,然后这个数的出现次数+1,如果不同的数大于k了,那就要去掉第L个数,直 ...

  10. 【POJ 2886】Who Gets the Most Candies?

    题意 约瑟夫问题的升级版,每次出去的是前一个出去的人位置+手上的数字(正往前,负往后).第i个出去的人拿的糖是i的约数的个数.求拿糖最多的人和他的糖果数. 分析 线段树单点更新,反素数. 我竟然WA在 ...