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. tomcat配置启动文件

    修改tomcat到指定文件夹 conf -> server.xml <Host name="localhost" appBase="webapps" ...

  2. 语音控制单片机工作【百度语音识别,串口发送数据到单片机】【pyqt源码+软件】!!

    前些天闲着没事,就做了个语音识别结合串口发送指令的软件,用的是pyqt写的,软件打开后对着笔记本的话筒说话, 他就能识别返回文字结果,然后匹配语音中的关键词,如果有关键词就发送关键词对应的命令,比如语 ...

  3. 大型系列课程之-七夕告白之旅vbs篇

    也许,世间所有的美好的东西,都是需要仪式感的,遇到了一年一度的七夕节,怎么过这个节日,成了很多心中有爱的人关注的事情,七夕不浪漫,人间不值得,七夕,发源于中国,这个美好的节日,来自动人的神话故事传说牛 ...

  4. 深入理解JVM-类加载器深入解析(3)

    深入理解JVM-类加载器深入解析(3) 获得ClassLoader的途径 获得当前类的ClassLoader clazz.getClassLoader() 获得当前线程上下文的ClassLoader ...

  5. Redis进阶应用:Redis+Lua脚本实现复合操作

    一.引言 Redis是高性能的key-value数据库,在很大程度克服了memcached这类key/value存储的不足,在部分场景下,是对关系数据库的良好补充.得益于超高性能和丰富的数据结构,Re ...

  6. vue之手把手教你写日历组件

    ---恢复内容开始--- 1.日历组件 1.分析功能:日历基本功能,点击事件改变日期,样式的改变 1.结构分析:html 1.分为上下两个部分 2.上面分为左按钮,中间内容展示,右按钮 下面分为周几展 ...

  7. SpringBoot学习系列之一(反射)

    最近在学习SpringBoot的知识,动起手来学习的时候才发现SpringBoot项目采用了大量的反射机制,晕,作为一个应届毕业生,以前学习反射的时候给我的感觉就是,这个到底用来干嘛的,好像没啥用啊, ...

  8. mvnjar包冲突解决方法

    命令 mvn dependency:tree -Dverbose 结果: [INFO] +- com.esotericsoftware:kryo:jar:4.0.2:test [INFO] | +- ...

  9. 分享一个非常好用又好看的终端工具--Hyper (支持windows、MacOS、Linux)

    分享一个非常好用又好看的终端工具--Hyper 官网地址: https://hyper.is/ 打开官网,选择对应版本安装即可:(可能网络原因,无法下载, 可以从我分享的链接下载 链接: https: ...

  10. LK的NOIP膜拟赛

    T1 Learn to 签到 [题目描述] 希希最喜欢二进制了.希希最喜欢的运算是\(\wedge\). 希希还喜欢很多\(01\)序列.这些序列一共有\(n\)个,每个的长度为\(m\). 希希有一 ...