POJ1753(枚举)


Flip Game

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

题意

给了你一个4*4的棋盘,棋盘上的每一个格子里都有一颗一面黑一面白的棋子,一开始给出你棋盘的初始状态,你可以给任何一个棋子翻面,但是如果你翻了某一个棋子,那他上下左右的棋子都要翻面。问你最少需要翻多少次才能让16枚棋子都为黑或都为白。

思路

对一个格子操作偶数次等于没有操作,操作奇数次等于操作一次,所以答案在0~16以及impossible之间。从n=0开始枚举n次操作可能的组成情况,即操作哪几个格子,若某种组合能变图案为纯色则停止。
由于总组合数达到2^16,枚举组合情况可以用dfs来进行回溯枚举。

代码

public class FlipGame {int[][] checkerboard = new int[6][6];int count = 33;public static boolean check(int[][] checkerboard) {int result = 0;for (int i = 0; i < checkerboard.length; i++) {for (int j = 0; j < checkerboard[i].length; j++) {result = result + checkerboard[i][j];}}if (result == -16 || result == 16) {return true;} else {return false;}}public void flip(int s) {int x = s / 4 + 1;int y = s % 4 + 1;checkerboard[x][y] = -checkerboard[x][y];checkerboard[x - 1][y] = -checkerboard[x - 1][y];checkerboard[x][y - 1] = -checkerboard[x][y - 1];checkerboard[x + 1][y] = -checkerboard[x + 1][y];checkerboard[x][y + 1] = -checkerboard[x][y + 1];}public void dfs(int s,int b){//进行深搜.s代表当前的方格,b代表翻转的方格数if(check(this.checkerboard)){//如果是同一种颜色if (this.count > b){this.count = b;return;}}if(s >=16){return;}dfs(s+1,b);this.flip(s);dfs(s+1,b+1);this.flip(s);}public static void main(String[] args) {FlipGame fg = new FlipGame();Scanner sc = new Scanner(System.in);for (int i = 1; i < fg.checkerboard.length - 1; i++) {String str = sc.next();for (int j = 1; j < fg.checkerboard[i].length - 1; j++) {if (str.charAt(j - 1) == 'w') {fg.checkerboard[i][j] = 1;} else {fg.checkerboard[i][j] = -1;}}}fg.dfs(0,0);if(fg.count == 33){System.out.println("Impossible");}else {System.out.println(fg.count);}sc.close();}
}

POJ1753(枚举)相关推荐

  1. poj-1753 枚举

    Flip 枚举 题目链接 大概题意: 给你一个4x4的棋盘,每个棋子都有黑白两面,问最少翻多少个棋子能让棋盘上全部是黑或者全部是白 思路: 深搜枚举 从第一个点开始开始,我每个棋子都选择翻转或者不翻转 ...

  2. poj1753 Flip Game(枚举Enum+dfs)

    转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj.org/problem?id=1753 ------ ...

  3. POJ1753题解(枚举)

    Sample Inputbwwb bbwb bwwb bwww Sample Output4 题意:给一个4x4的棋盘,棋盘上有16个棋子,棋子正反两面分别为黑色和白色,给出初始状态(棋子可能是正面可 ...

  4. POJ-1753 Flip Game 枚举 状态压缩

    刚开始做这题时总是在想应该用何种的策略来进行翻装,最后还是没有想出来--- 这题过的代码的思路是用在考虑到每个点被翻装的次数只有0次或者是1次,所以对于16个点就只有2^16中请况了.再运用位运算将状 ...

  5. poj1753 解题思路

    poj1753属于枚举类型的题目,思路很简单,主要把握住两个点: · 1.每个点只有两种情况,翻0次和翻1次,翻偶数次是没有意义的: · 2.哪个点先翻哪个点后翻都是一样的. 明白了以上的基本要点,就 ...

  6. 【C#】枚举_结构体_数组

    最近看C#视频,关于这部分,先看了一遍,又照着敲了一遍,自己敲的过程发现了一些有意思的东西. 枚举:定义一个枚举类型的变量,这个变量有很多相同类型的值.比如性别Gender这个变量可以有男和女这两个值 ...

  7. Go 学习笔记(7)— 常量定义、常量使用、枚举用法、iota 常量、不设置初始值常量

    1. 常量定义及分类 1.1 定义 常量是指在程序运行时,不会被修改的量. 常量中的数据类型只可以是布尔型.数字型(整数型.浮点型和复数)和字符串型. 常量的定义格式: const identifie ...

  8. 1137 - Sin your life sin公式 + 枚举

    http://www.ifrog.cc/acm/problem/1137 和差化积公式, 变成2 * sin((x + y) / 2) * cos((x - y) / 2) + sin(n - (x ...

  9. C语言程序设计 细节总结(第9章 结构体共用体枚举)

    第9章 结构体.共用体.枚举 9.1 结构体 1.对于同类型结构体变量之间可以整体一次赋值 9.2 指向结构体的指针 9.2.1结构体变量的指针 1.定义格式:struct 结构体名 *结构体指针变量 ...

最新文章

  1. java导出highcharts_java实现highcharts导出图片至excel
  2. 公司前台打印机的连接方法(超级简单)
  3. 【持久化框架】SpringMVC+Spring4+Mybatis3集成,开发简单Web项目+源码下载【转】
  4. nlp文本相似度_用几行代码在Python中搜索相似文本:一个NLP项目
  5. 物联网通信协议——比较-MQTT、 DDS、 AMQP、XMPP、 JMS、 REST、 CoAP
  6. 涉及反射/内省/泛型的优化实践
  7. Ubuntu su: authentication failure切换用户失败
  8. 关于分布式系统架构模块通讯方式选择的问题
  9. 登录时 按Enter 进入登录界面 或者下一行
  10. 歪果仁眼中的中国理工科科研调查:让学术氛围更自由成最大呼声 | 报告
  11. 一次外网打不开网站的故障总结
  12. SpringBoot+RabbitMQ ,保证消息100%投递成功并被消费(实例)
  13. python之类介绍
  14. KVYcam(网络摄像头软件) v13.0.3.0
  15. 中国智慧园区未来发展趋势及投资战略规划研究报告2022年版
  16. 360千兆路由计算机安装方法,千兆路由器怎么安装?
  17. linux shell有哪些变量,Linux Bash Shell有关变量
  18. Eclipse+Java+Swing实现企业人事管理系统
  19. html5 js获取设备信息,js怎么获取电脑硬件信息
  20. 2022年MSI赛程安排时间表 MSI赛事规则

热门文章

  1. RabbitMQ:镜像队列Mirrored queue
  2. Android Studio Arctic Fox | 2020.3.1、Gradle 7.0升级记录
  3. 事件分发机制流程图,Android免打包多渠道统计如何实现?Android核心知识点
  4. 清华叉院弋力:从谷歌研究科学家到清华任教,我想看远一点
  5. ios怎么打开c语言文件操作函数,C++ ofstream和ifstream详细用法以及C语言的file用法...
  6. android studio虚拟机图库不显示图片,照片显示图片解决办法
  7. 破解携程中文验证码爬取机票价格数据
  8. 打开Xmind提示The contiolrator Userslwangappication DatalXMindlconfiquration-cathy win32-R3.79.2019120523
  9. 2021-04-06-MSF之永恒之蓝
  10. unity3d热更新插件uLua