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. powermockito单元测试之深入实践

    概述 由于最近工作需要, 在项目中要做单元测试, 以达到指定的测试用例覆盖率指标.项目中我们引入的powermockito来编写测试用例, JaCoCo来监控单元测试覆盖率.关于框架的选择, 网上讨论 ...

  2. 【有容云案例系列】基于Jenkins和Kubernetes的CI工作流

    嘉宾介绍 黄文俊 有容云资深系统架构师 主要负责容器云平台产品架构及设计. 8年工作经验, 有着企业级存储, 云计算解决方案相关理解. 关注于微服务设计思考, 开发流程优化, docker及kuber ...

  3. 学习vue感触

    大学还没毕业,想着先找工作,感觉计算机专业在老家做没有太大的发展,于是就在大学所在城市找了份工作.来到公司的第一天,带我的师傅让我学习vue.之前完全没有接触过框架,而且专业知识比较薄弱,前几天一直处 ...

  4. 在线图片base64编码

    图片Base64编码https://oktools.net/image2base64 在线工具https://oktools.net JSON格式化https://oktools.net/json U ...

  5. weblogic 内存溢出解决 java.lang.OutOfMemoryError: PermGen space

    解决办法: 1.在idea中,运行时给weblogic server中 VM options 配置增加内存的参数:-server -XX:PermSize=1024m -XX:MaxPermSize= ...

  6. luogu1373_小a和uim之大逃离 多维dp

    传送门 巧妙之处在于dp的设计只用设计差值即可,因此不会mle,枚举的顺序问题也解决了 #include <bits/stdc++.h> using namespace std; #def ...

  7. 用CSS来定义<p>标签,要求实现以下效果:字体颜色再IE6下为黑色,IE7下为红色,IE8下为绿色,其他浏览器下为黄色。

    <!DOCTYPE html><html><head><meta charset="utf-8"><meta name=&qu ...

  8. MySQL一键生成实体文件的神器-ginbro

    Java转过来的同学对Mybatis的使用肯定不陌生,特别是对一堆表去生成相应的dao和entity的时候使用Mybatis generator所带来的感触,无比深刻.前面我们也讲过原生的数据库使用, ...

  9. android ——调用摄像头拍照和相册

    先在布局文件中加入两个按钮和一个图片控件 <?xml version="1.0" encoding="utf-8"?> <LinearLayo ...

  10. 谈谈我对Ext的认识,元芳,你怎么看

    实用Ext第一步当然是引用jar包啦. 下载地址 在页面上加上div用于显示这也是必须的 <div id='loginpanel' ></div> 在js中我们肯定需要将Ext ...