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. 从windows平台转战ubuntu

    说到ubuntu,可能很多人会有些陌生,但对于有些人很熟悉.ubuntu是linux里面最为流行的一版,以下来自百度百科.       Ubuntu(乌班图)是基于Debian GNU/Linux,支 ...

  2. 保存MTLAB图片是想去掉白边

    在做一些matlab小实验的时候,生成的图片需要临时保存的时候会有多余的白边,如何能解决这种问题? 输入 iptsetpref('ImshowBorder','tight'); 后,再show一次图即 ...

  3. Java----面向对象(继承&多态)

    一.继承 什么是继承 ? 让类与类之间产生了子父类关系 ; 继承的好处是: 提高代码的复用性和维护性 java中继承的特点是: 只支持单继承.不支持多继承,但是可以多层继承; 四种权限修饰符是 : p ...

  4. MySQL-5.7.21非图形化下载、安装、连接问题记录

    1.安装包下载链接:https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.21-winx64.zip 官网:https://www.mysql.co ...

  5. 使用RedisMQ 做一次分布式改造

    引言 熟悉TPL Dataflow博文的朋友可能记得这是个单体程序,使用TPL Dataflow 处理工作流任务, 在使用Docker部署的过程中, 有一个问题一直无法回避: 在单体程序部署的瞬间会有 ...

  6. cogs 1254. 最难的任务 Dijkstra + 重边处理

    1254. 最难的任务 ★   输入文件:hardest.in   输出文件:hardest.out   简单对比时间限制:1 s   内存限制:128 MB [题目描述] 这个真的很难.算出 123 ...

  7. Spring入门编程问题集锦Top10

    我写的一篇文章,希望对spring初学者有所帮助: 1.如何学习Spring? 你可以通过下列途径学习spring: ①. spring下载包中doc目录下的MVC-step-by-step和samp ...

  8. java并发编程(二十六)----ThreadLocal的使用

    其实ThreadLocal很多接触过多线程的同学都可能会很陌生,他不像current包里面那些耳熟能详的api一样在我们面前经常出现,更多的他作为一个本地类出现在系统设计里面.我们可以说一下Sprin ...

  9. mongoDB的CRUD的总结

    今天开始接触非关系型数据库的mongoDB,现在将自己做的笔记发出来,供大家参考,也便于自己以后忘记了可以查看. 首先,mongoDB,是一种数据库,但是又区别与mysql,sqlserver.orc ...

  10. mysql主从不同步处理过程分享

    背景  8月7日15:58收到报障数据库出现不同步:数据库共四台,分别为10.255.70.11,10.255.70.12,10.255.70.13,10.255.70.14(ip为虚拟ip) 数据库 ...