博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
J - 最强王者 POJ - 1753 Flip Game 搜索+状态压缩
阅读量:6139 次
发布时间:2019-06-21

本文共 2512 字,大约阅读时间需要 8 分钟。

 

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

bwwbbbwbbwwbbwww

Sample Output

4

题意:翻棋子,翻一个,相邻的也要翻(上下左右的),给出初始棋盘,问最少多少步能把棋盘变成全白OR全黑

搜索+状压:把每个棋子都翻一下,分别记录状态,再在这个基础上循环前面的步骤,

                    vis[i],把i变成二进制,则对应位上的数字代表棋盘对应位置上的棋子状态

#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
typedef long long LL;typedef long double LD;using namespace std;const int maxn=5;char ma[maxn][maxn];int vis[65536+10];//int ff[8][2]={ {-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};int f[5][2]={ {-1,0},{1,0},{0,-1},{0,1},{0,0}};int ff[16]={1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768};//int f[8][2]={ {-2,-1},{-2,1},{2,-1},{2,1},{-1,-2},{1,-2},{-1,2},{1,2}};int N=4,M=4,K;struct node{ int status; int step; void setnode(int sta,int ss) { status=sta; step=ss; } friend bool operator <(node a,node b) { return a.step>b.step; }};priority_queue
q;bool OK(int x,int y)//过界{ if(x>=0&&y>=0&&x

 

 

转载于:https://www.cnblogs.com/107acm/p/9428313.html

你可能感兴趣的文章
深入python的set和dict
查看>>
C++ 11 lambda
查看>>
Hadoop2.5.0 搭建实录
查看>>
实验吧 recursive write up
查看>>
Android JSON数据解析
查看>>
DEV实现日期时间效果
查看>>
java注解【转】
查看>>
Oracle表分区
查看>>
centos 下安装g++
查看>>
嵌入式,代码调试----GDB扫盲
查看>>
类斐波那契数列的奇妙性质
查看>>
配置设置[Django]引入模版之后报错Requested setting TEMPLATE_DEBUG, but settings are not configured....
查看>>
下一步工作分配
查看>>
Response. AppendHeader使用大全及文件下载.net函数使用注意点(转载)
查看>>
Wait Functions
查看>>
代码描述10313 - Pay the Price
查看>>
jQuery最佳实践
查看>>
centos64i386下apache 403没有权限访问。
查看>>
vb sendmessage 详解1
查看>>
jquery用法大全
查看>>