Flip Game
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 37050   Accepted: 16122

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

没点思路-_-

转载请注明出处:優YoU  http://user.qzone.qq.com/289065406/blog/1299076400

提示:翻转棋,可以建模为多叉树

本题难点有两个,一个就是不要以全黑(或全白)作为目标进行搜索,而是要把全黑(或全白)作为“根”,去搜索树叶,看看是否有 输入的棋盘状态。

另一个难点需要一点数学功底,就是要知道 树 的最大高度,这是“状态不存在”的判断标准

提示:其实每格棋子最多只可以翻转一次(实际是奇数次,但这没意义),只要其中一格重复翻了2(不论是连续翻动还是不连翻动),那么它以及周边的棋子和没翻动时的状态是一致的,由此就可以确定这个棋盘最多只能走16步,最多只能有翻出2^16种状态

其实也想过dfs,找不到终止状态,对,就是16就ok了,因为如果每一个状态都翻转了一遍,那么你在翻转一个牌,那么就和他之前没翻转是一样的,所以没必要了

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
bool chess[][];
int step,flag;
int r[] = {-,,,};
int c[] = {,,-,};
int judge_all()
{
for(int i = ; i <= ; i++)
{
for(int j = ; j <= ; j++)
{
if(chess[i][j] != chess[][])
return false;
}
}
return true;
}
void flip(int row, int col)
{
chess[row][col] = !chess[row][col];
for(int i = ; i < ; i++)
{
int fx = row + r[i];
int fy = col + c[i];
if(fx > && fx <= && fy > && fy <= )
chess[fx][fy] = !chess[fx][fy];
}
}
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 + );
} flip(row, col); //回溯,把原来的转过来 if(col < )
{
dfs(row, col + , deep); //回溯时要判断上一步是否已经结束,因此传的是deep,判断当前状态,如果没结束,就按row,col+1变化
}
else
{
dfs(row + , , deep);
}
return;
}
int main()
{
char s[];
while(scanf("%s", s) != EOF)
{
memset(chess, false, sizeof(chess));
for(int i = ; i < ; i++)
{
if(s[i] == 'b')
{
chess[][i + ] = true;
}
}
for(int i = ; i <= ; i++)
{
scanf("%s", s);
for(int j = ; j < ; j++)
{
if(s[j] == 'b')
chess[i][j + ] = true;
}
}
flag = ;
for(step = ; step <= ; step++)
{
dfs(,,);
if(flag)
break;
}
if(flag)
printf("%d\n", step);
else
printf("Impossible\n");
} return ;
}

POJ1753Flip Game(DFS + 枚举)的更多相关文章

  1. POJ1288 Sly Number(高斯消元 dfs枚举)

    由于解集只为{0, 1, 2}故消元后需dfs枚举求解 #include<cstdio> #include<iostream> #include<cstdlib> ...

  2. POJ 2429 GCD & LCM Inverse (Pollard rho整数分解+dfs枚举)

    题意:给出a和b的gcd和lcm,让你求a和b.按升序输出a和b.若有多组满足条件的a和b,那么输出a+b最小的.思路:lcm=a*b/gcd   lcm/gcd=a/gcd*b/gcd 可知a/gc ...

  3. POJ 1270 Following Orders (拓扑排序,dfs枚举)

    题意:每组数据给出两行,第一行给出变量,第二行给出约束关系,每个约束包含两个变量x,y,表示x<y.    要求:当x<y时,x排在y前面.让你输出所有满足该约束的有序集. 思路:用拓扑排 ...

  4. HDU 2489 Minimal Ratio Tree(dfs枚举+最小生成树)

    想到枚举m个点,然后求最小生成树,ratio即为最小生成树的边权/总的点权.但是怎么枚举这m个点,实在不会.网上查了一下大牛们的解法,用dfs枚举,没想到dfs还有这么个作用. 参考链接:http:/ ...

  5. poj 1753 Flip Game(bfs状态压缩 或 dfs枚举)

    Description Flip game squares. One side of each piece is white and the other one is black and each p ...

  6. POJ 1753 Flip Game (DFS + 枚举)

    题目:http://poj.org/problem?id=1753 这个题在開始接触的训练计划的时候做过,当时用的是DFS遍历,其机制就是把每一个棋子翻一遍.然后顺利的过了.所以也就没有深究. 省赛前 ...

  7. HDU1045 Fire Net(DFS枚举||二分图匹配) 2016-07-24 13:23 99人阅读 评论(0) 收藏

    Fire Net Problem Description Suppose that we have a square city with straight streets. A map of a ci ...

  8. poj 2965 The Pilots Brothers&#39; refrigerator(dfs 枚举 +打印路径)

    链接:poj 2965 题意:给定一个4*4矩阵状态,代表门的16个把手.'+'代表关,'-'代表开.当16个把手都为开(即'-')时.门才干打开,问至少要几步门才干打开 改变状态规则:选定16个把手 ...

  9. The Pilots Brothers' refrigerator DFS+枚举

    Description The game “The Pilots Brothers: following the stripy elephant” has a quest where a player ...

随机推荐

  1. Maven 小结

    Maven 的各项功能通过插件实现,有需要的时候学习那些插件的配置即可 一般一个大型项目会有 A:父管理工程,定义了所有的依赖和插件 B:工具工程 C:web 项目的父工程,同时也是一个聚合工程 D: ...

  2. [转]iOS 应用内付费(IAP)开发步骤

    FROM : http://blog.csdn.net/xiaoxiangzhu660810/article/details/17434907 参考文章链接: (1)http://mobile.51c ...

  3. Socket Programming in C#--Getting Started

    Getting Started You can argue that one can overcome these shortcomings by multithreading meaning tha ...

  4. C语言 野指针与空指针

    //野指针与空指针的区别 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> //野指针是 ...

  5. C语言 读取文件中特定数据

    //读取文件数据 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> struct jia ...

  6. OpenCV人脸检测demo--facedetect

    &1 问题来源 在运行官网的facedetect这个demo的时候,总是不会出来result的图形,电脑右下角提示的错误是“显示器驱动程序已停止响应,而且已恢复 windows 8(R)”. ...

  7. WCF与ASMX Web服务差异比较[译]

    First of all, it needs to understand that WCF Service provides all the capabilities of .NET web serv ...

  8. HTTP协议简介2

    几个相关的知识点: 客户端发送请求时,请求类型为GET与POST的主要差别是什么? 1.请求类型不同,一个为GET,一个为POST 2.当请求类型为GET时,请求的数据以参数的形式添加到url的后面, ...

  9. Asp.Net的两种开发方式

    来源:http://www.zhidao91.com/asp-net/ 在经过对.Net平台深入的学习以后,我发现很多语言开发动态网站时,它的后台逻辑都差不多是相同的,今天在这里我给大家来聊聊在.Ne ...

  10. SQL脚本循环修改数据库字段类型

    数据库在设计的时候也许考虑不全面,导致某些字段类型不太准确.比如设计的时候是varchar(1024),但是实际使用的时候却发现太小了,装不下,于是需要修改字段类型为ntext什么的. 我最近就遇到了 ...