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

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

 
题目大意:
    一个4✖️4的棋盘上布满了棋子,棋子具有黑白两面,当翻转一个棋子的时候,周边的4个棋子包括自己都会变色。给出一个棋盘,问最少翻转几次,全部变白或者全部变黑。
 
思路:
    棋盘很小,所以暴力枚举即可。从别的大犇那里了解到翻转的顺序对结果是不影响的,但是不是很懂,如果大佬知道可以在评论区里评论。
 
 #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<list>
#include<vector>
#include<stack>
#include<string>
#include<map>
#include<numeric>
#include<set>
#include<functional>
#include<sstream>
#include<time.h>
using namespace std;
int mapp[][];
int turn1[]={,-,,,};
int turn2[]={,,,,-};
const int inf = 0x3f3f3f3f;
int ans=inf; int judge( void ) //判断棋盘是否全为0,或全为1
{
int temp=mapp[][];
for(int i = ; i<; i++)
{
for(int j = ; j<; j++)
{
if(temp != mapp[i][j])
return ;
}
}
return ;
} void turn(int x, int y)
{
for(int i=;i<;i++)
{
if(x+turn1[i]>=&&x+turn1[i]<&&y+turn2[i]>=&&y+turn2[i]<)
mapp[x+turn1[i]][y+turn2[i]]= !mapp[x+turn1[i]][y+turn2[i]];
}
} int dfs( int x, int y, int t)
{
if(judge())
{
ans=min(ans,t);
return ;
}
if(x >= || y >= )
return ;
int nx=(x+)%; //坐标,满4换成第二行
int ny=y+(x+)/; dfs(nx,ny,t); //搜索翻转
turn(x,y);
dfs(nx,ny,t+);
turn(x,y); return ;
}
int main()
{ for(int i=;i<;i++)
{
char s[];
gets(s);
for(int j=;j<;j++)
{
if(s[j]=='b') mapp[i][j]=;
else mapp[i][j]=;
}
} dfs(,,); if(ans!=inf)
printf("%d\n",ans);
else
printf("Impossible\n");
}

poj 1753 Flip Game(暴力枚举)的更多相关文章

  1. POJ 1753 Flip Game DFS枚举

    看题传送门:http://poj.org/problem?id=1753 DFS枚举的应用. 基本上是参考大神的.... 学习学习.. #include<cstdio> #include& ...

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

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

  3. POJ 1753 Flip Game【枚举】

    题目链接: http://poj.org/problem?id=1753 题意: 由白块黑块组成的4*4方格,每次换一个块的颜色,其上下左右的块也会被换成相反的颜色.问最少换多少块,使得最终方格变为全 ...

  4. POJ 1753 Flip Game (枚举)

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 26492   Accepted: 11422 Descr ...

  5. POJ 1753 Flip Game 暴力 深搜

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

  6. 枚举 POJ 1753 Flip Game

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

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

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

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

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

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

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

随机推荐

  1. Docker 容器高级操作[Docker 系列-3]

    关注公众号,大家可以在公众号后台回复“博客园”,免费获得作者 Java 知识体系/面试必看资料. 上篇文章向读者介绍了一个 Nginx 的例子,对于 Nginx 这样一个容器而言,当它启动成功后,我们 ...

  2. codeforces 576 div2 A-D题解

    A题 Description 题目链接: https://codeforces.com/contest/1199/problem/A 题意: 给定长度为n(1≤n≤100000)的一个序列a,以及两个 ...

  3. JavaScript数据结构——字典和散列表的实现

    在前一篇文章中,我们介绍了如何在JavaScript中实现集合.字典和集合的主要区别就在于,集合中数据是以[值,值]的形式保存的,我们只关心值本身:而在字典和散列表中数据是以[键,值]的形式保存的,键 ...

  4. Python pip包管理器安装第三方库超时解决方案

    一.国内镜像安装 使用方法:pip install --index 镜像网站 第三方库名 二.镜像网站 http://pypi.douban.com/simple/ 豆瓣 http://mirrors ...

  5. 【错误】【vscode】输出中文是乱码问题

  6. quick-cocos2dx在eclipse下的lua调试

    文中大部分内容来自http://cn.quick-x.com/?p=253,绿色标记部分为修改部分. 配置编译环境的基本步骤: 安装 Visual Studio 2012 安装 Java SDK 安装 ...

  7. 一文了解:Redis的AOF持久化

    Redis的AOF持久化 每当Redis-Server接收到写数据时,就把命令以文本形式追加到AOF文件里,当重启Redis服务时,AOF文件里的命令会被重新执行一次,重新恢复数据.当AOF过大时将重 ...

  8. SoapSerialization——手机号码归属地

    public class MainActivity extends AppCompatActivity { private EditText etNumber; private TextView tv ...

  9. 怎么把PicPick设置成中文版?

    1.首先打开软件 2.在File文件中中点击能看到Program Options这一选项,单击打开 3.右下方有个Language选项,改成简体中文

  10. 纯数据结构Java实现(3/11)(链表)

    题外话: 篇幅停了一下,特意去看看其他人写的类似的内容:然后发现类似博主喜欢画图,喜欢讲解原理. (于是我就在想了,理解数据结构的确需要画图,但我的文章写给懂得人看,只配少量图即可,省事儿) 下面正题 ...