Flip Game
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 59468   Accepted: 24750

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 <iostream>
#include <cstdio> using namespace std; char str[];
int a[][];
int ans=; void flip(int x,int y)
{
a[x][y]=!a[x][y];
a[x-][y]=!a[x-][y];
a[x+][y]=!a[x+][y];
a[x][y-]=!a[x][y-];
a[x][y+]=!a[x][y+];
} bool same_color(void)
{
for(int i=;i<=;++i)
{
for(int j=;j<=;++j)
{
if(a[i][j]!=a[][])
{
return false;
}
}
}
return true;
} void dfs(int x,int y,int t)
{
// 判断是否同色,同色满足,返回
if(same_color())
{
if(ans>t)
{
ans=t;
}
return;
}
// 深搜截止条件
if(x>)
{
return;
} if(y==)
{
// 当前不反转
dfs(x+,,t);
// 当前反转
flip(x,y);
dfs(x+,,t+);
// 状态还原
flip(x,y);
}
else
{
dfs(x,y+,t);
flip(x,y);
dfs(x,y+,t+);
flip(x,y);
} } int main()
{
for(int i=;i<=;++i)
{
scanf("%s",str+);
for(int j=;j<=;++j)
{
// a->0,b->1
if(str[j]=='w')
{
a[i][j]=;
}
else
{
a[i][j]=;
}
//a[i][j]=str[j]-'a';
//printf("%d ",a[i][j]);
}
//printf("\n");
} dfs(,,); if(ans==)
{
printf("Impossible\n");
}
else
{
printf("%d\n",ans);
} return ;
}

POJ 1753 Flip Game 暴力 深搜的更多相关文章

  1. poj 1753 Flip Game(暴力枚举)

    Flip Game   Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 52279   Accepted: 22018 Des ...

  2. 枚举 POJ 1753 Flip Game

    题目地址:http://poj.org/problem?id=1753 /* 这题几乎和POJ 2965一样,DFS函数都不用修改 只要修改一下change规则... 注意:是否初始已经ok了要先判断 ...

  3. POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37427   Accepted: 16288 Descr ...

  4. POJ - 1753 Flip Game(状压枚举)

    https://vjudge.net/problem/POJ-1753 题意 4*4的棋盘,翻转其中的一个棋子,会带动邻接的棋子一起动.现要求把所有棋子都翻成同一种颜色,问最少需要几步. 分析 同一个 ...

  5. Poj(2488),按照字典序深搜

    题目链接:http://poj.org/problem?id=2488 思路:按照一定的字典序深搜,当时我的想法是把所有的可行的路径都找出来,然后字典序排序. 后来,凡哥说可以在搜索路径的时候就按照字 ...

  6. POJ 1753 Flip Game(高斯消元+状压枚举)

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 45691   Accepted: 19590 Descr ...

  7. HDU--杭电--1195--Open the Lock--深搜--都用双向广搜,弱爆了,看题了没?语文没过关吧?暴力深搜难道我会害羞?

    这个题我看了,都是推荐的神马双向广搜,难道这个深搜你们都木有发现?还是特意留个机会给我装逼? Open the Lock Time Limit: 2000/1000 MS (Java/Others)  ...

  8. POJ 1753 Flip Game (状压+暴力)

    题目链接:http://poj.org/problem?id=1753 题意: 给你一个4*4的棋盘,上面有两种颜色的棋子(一种黑色,一种白色),你一次可以选择一个棋子翻转它(黑色变成白色,同理反之) ...

  9. POJ 1753 Flip Game 状态压缩,暴力 难度:1

    Flip Game Time Limit: 1000MS  Memory Limit: 65536K  Total Submissions: 4863  Accepted: 1983 Descript ...

随机推荐

  1. auth.User.groups: (fields.E304) Reverse accessor for 'User.groups'

    创建表,运行下面命令,出错 makemigrations 原因:继承auth_user 解决方案 在settings.py文件添加 AUTH_USER_MODEL = "app名称.类名&q ...

  2. [题解]CSP2019 Solution - Part B

    \(\text{orz}\) 一波现场 \(\text{A}\) 掉 \(\text{D1T3}\) 的神仙 D2T3 centroid Solution 考虑每个点 \(u\) 作为重心的贡献 假设 ...

  3. KD-tree 学习小记

    考 \(NOI\) 时不会,感觉很亏.于是学了一上午,写了一晚上. 感觉这东西就是个复杂度玄学的高级暴力 (大雾 KD-tree 基本信息 \(D\) 就是 \(Dimension\) ,维度的意思. ...

  4. python 抓一下 循环的访问也可以

    #!/usr/bin/python # -*- coding: utf-8 -*- #encoding=utf-8 #Filename:urllib2-header.py import urllib2 ...

  5. 《编写高质量代码:改善C#程序的157个建议》是C#程序员进阶修炼的必读之作

  6. hihoCoder 1387 A Research on "The Hundred Family Surnames"

    搬家一个月,庆祝一下 啪啪啪啪啪啪啪啪啪啪❀❀❀❀ 题目传送门 分析: 这什么奇奇怪怪的OJ,以前从来不知道的2333 以前只知道合并两个连通块时,其中一边直径端点为A,B,另一边为C,D D=max ...

  7. 使用requests模块的网络编程

    python操作网络,也就是打开一个网站,或者请求一个http接口,本篇是介绍使用request模块的使用方式. 在使用requests模块之前需要先安装,在cmd中输入:pip install re ...

  8. keras冒bug

    使用keras做vgg16的迁移学习实验,在实现的过程中,冒各种奇怪的bug,甚至剪贴复制还是出问题. 解决方案: 当使用组合keras和tensorflow.keras时.由于版本不一致问题导致很多 ...

  9. FileNotFoundError: [WinError 2] 系统找不到指定的文件

    用Idle运行Python脚本的时候发现如下错误: Traceback (most recent call last):  File "D:\Python\Python36-32\lib\s ...

  10. linux--->PHP常用模块解析

    PHP常用模块解析 php-gd :GD库是php处理图形的扩展库,GD库提供了一系列用来处理图片的API,使用GD库可以处理图片,或者生成图片,也可以给图片加水印 php-ldap :LDAP是轻量 ...