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. Xml命名空间浅析

    XML实例 在介绍xml命名空间之前,我们先来看段xml代码: <?xml version="1.0" encoding="UTF-8"?> < ...

  2. JavaScript中的各种小坑汇总

    1.Number()将部分非数字类型转换为0 强制转换为数值类型函数: parseFloat.parseInt 优点:对非数值类型统一返回NaN 缺点:会将一部分符合数值类型的字符串也识别为数值 pa ...

  3. EF实体框架之CodeFirst五

    上一博客学习了下基本的约定配置,留下几个遗漏的,这篇就是学习下遗漏一复杂类型. 一.什么是复杂类型? 书中说道:“复杂类型也可视作值类型(?)可以作为附加属性添加到其他类.复杂类型与实体类型的区别在于 ...

  4. 同步git修改文件到远端服务器脚本

    #!/usr/bin/perl -w @files=`git status -s` ; @sync_files = (); foreach (@files) { ); # 固定前2个字符为状态 + 1 ...

  5. ArcGIS中的北京54和西安80投影坐标系详解

    ArcGIS中的北京54和西安80投影坐标系详解 1.首先理解地理坐标系(Geographic coordinate system),Geographic coordinate system直译为地理 ...

  6. css基本的东西

    0 css本来也是一个比较乱的东西,我们需要在最恰当的情况下,写出最杂乱的效果.1 面对body设置了 -webkit-font-smoothing:antialiased (默认值为subpixel ...

  7. android之显示数据库信息

    关键字 ListView adapter MVC 在android开发中也使用到了MVC架构,其中的xml布局文件就是V,M就是我们定义好的javabean类,而控制器就是就是适配器类adapter ...

  8. Javascript基础系列之(六)循环语句(for循环)

    如果您希望一遍又一遍地运行相同的代码,并且每次的值都不同,那么使用循环是很方便的. document.write(cars[0] + "<br>"); document ...

  9. JS面向对象概述

    这部分内容还是比较难理解的,像借用构造函数这种方法,实际工作中还是很常见的,不过对于后面的寄生理解还有点困难,只能慢慢学习了. 思维导图

  10. oracle练习题

    题干:设有一数据库,包括四个表:学生表(Student).课程表(Course).成绩表(Score)以及教师信息表(Teacher). 建表后数据如下: SQL> select * from ...