POJ 1753 Flip Game
Flip Game
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 53632 | Accepted: 22497 |
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:
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.
- Choose any one of the 16 pieces.
- 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).

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
Sample Output
Source
【题意】
有4×4的棋盘,上面的棋子一面是黑的,一面是白的。规定翻转一个棋子的同时也要翻转它的上、下、左、右的棋子,问给定一个棋盘的棋子状态,至少需要翻转多少个棋子,能使得所有棋子朝上都是白的或黑的。
【分析】
1、对于每个格子,它要么反转0次,要么反转1次(当然,它的邻格子也跟着反转),因为它反转偶数次和反转0次的效果是一样的,同理反转奇数次的效果和反转1次的效果是一样的。
2、数据规模小,考虑我们最少不翻任何棋子(初始状态就是全白或全黑),最多只翻16个棋子(再翻就有棋子被翻两次了),因此总的状态数为C160+C161+⋯+C1616=216,直接枚举或搜索即可。
3、考虑要求的是最小值,因此从翻0个开始,搜索翻i个,直到翻16个,中间搜索到即为最小值,翻16个都不满足即输出Impossible。
4、由于棋子都只有黑、白两面,可以用0、1表示,因此可以位压缩成一个数字来进行判断,翻棋子的操作可使用位运算,有两种方法
5、只有16个棋子,直接状态压缩,一个int型变量就能存下这16个0/1了,所以可以直接压缩成一个数字。如i=2,j=2,则与20032(0100 1110
0100 0000)相异或
至此 迭代加深搜索 已经很明确了
【代码】
__EOF__

本文作者:shenben
本文链接:https://www.cnblogs.com/shenben/p/10367184.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。您的鼓励是博主的最大动力!
本文链接:https://www.cnblogs.com/shenben/p/10367184.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。您的鼓励是博主的最大动力!
分类:
搜索
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
2017-02-12 LA 3882 And Then There Was One[约瑟夫问题的变形]
2017-02-12 cf-341C Iahub and Permutations
2017-02-12 Times[2017-01-25at JiNan]
2017-02-12 2017-01-20_dp测试
2017-02-12 4514: [Sdoi2016]数字配对