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

本文共 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

你可能感兴趣的文章
androidstudio 之 svn配置 汇总
查看>>
html基本标签
查看>>
Ubuntu 12.04 安装 VMware Tools 找不到linux-headers 问题
查看>>
TP5 Tree类无限极分类
查看>>
阶段性总结-贪心算法
查看>>
C++namespace
查看>>
小程序初体验:手把手教你写出第一个小程序(一)
查看>>
【翻译】数据库设计——范式
查看>>
C# 事件(Event)
查看>>
【算法】6西格玛
查看>>
ny8 一种排序 sort
查看>>
Staying on Track with Location Services--WWDC 2012 session 303
查看>>
前端开发利器,webStorm
查看>>
Java中final、finally、finalize的区别
查看>>
[Leetcode]695. Max Area of Island
查看>>
第一篇博客
查看>>
面向对象程序设计第二次作业
查看>>
Linux 典型应用之缓存服务
查看>>
Docker版本与安装介绍
查看>>
dzzoffice应用如何安装
查看>>