Flip Game
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 30449   Accepted: 13232

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

import java.util.ArrayDeque;
import java.util.Scanner; /*
* 首先有两点:
* 1.两颗棋子的翻动顺序不影响翻动结果
* 2.一颗棋子的偶数次翻动与0次翻动效果等同,一颗棋子的奇数次翻动与1次翻动效果等同
*
*/
public class poj1753 {
public static void main(String[] args) {
Scanner cin=new Scanner(System.in);
while (cin.hasNext()){
ArrayDeque<Node> qu=new ArrayDeque<Node>();//Java队列
Node node=new Node(0,0,0);
for (int i=1;i<=4;i++){
String s=cin.next();
for (int j=0;j<s.length();j++){
char c=s.charAt(j);
if (c=='b') {
node.state=(node.state<<1)+1;//黑棋子则将当前状态表示的二进制位设为1
}else{
node.state=node.state<<1;//白棋子则将当前状态表示的二进制位设为0
}
}
}
qu.add(node);//初始状态增加队列
boolean flag=false;
while (qu.size()!=0){
Node tou=qu.pop();//队头出队。以此队头衍生出下一颗棋子的翻动,并增加队尾
if (tou.state==0 || tou.state==(1<<16)-1){//(1<<16)-1的二进制位为16个1
System.out.println(tou.ways);//找到方法输出翻动棋子数
flag=true;
break;
}
if (qu.size()>0xFFFF) continue;//所有可能状态所有入队。不在有新的状态入队,下面循环体不再运行
for (int i=tou.location+1;i<=16;i++){//从当前头节点的位置向后找,1到16表示16个棋格的翻动
//状态(后改动),当前为第i个棋格的翻动。翻动棋格数为队头棋格翻动数+1
node=new Node(0,i,tou.ways+1);
//翻动当前棋子:队头与当前棋格翻动状态(比如左上角i=1状态为1后15个 0,即1000000000000000)的“异或”
node.state=tou.state^(1<<(16-i));
//翻动上相邻棋子:i-4>=1推断当前棋格是否有上相邻棋子
if (i-4>=1) node.state^=1<<(16-i+4);
//翻动下相邻棋子:i+4<=16推断当前棋格是否有下相邻棋子
if (i+4<=16) node.state^=1<<(16-i-4);
//翻动右相邻棋子:4的倍数为棋盘的右边界,无右相邻棋子,16-i再减1位运算少向左移动一位,所以是右边
if (i%4!=0) node.state^=1<<(16-i-1);
//翻动左相邻棋子:i-1为有边界时,加1后的i就是左边界
if ((i-1)%4!=0) node.state^=1<<(16-i+1);
qu.add(node);
}
}
if (!flag)//找不出合适翻动
System.out.println("Impossible");
}
}
}
class Node{
int state;//状态:state的二进制位表示16个棋格的状态
int location;//位置:当前状态为第i个棋格的翻动
int ways;//翻动棋格数
Node (int state,int location,int ways){
this.state=state;
this.location=location;
this.ways=ways;
}
}

java的ArrayDeque的使用

题目大意:4X4的一个棋盘上有白子("w")和黑子("b"),棋子的还有一面是相反的颜色,翻动一颗棋子。上下左右跟着翻动。问至少要翻动几颗棋子,才干使棋盘全白或全黑

poj1753,Flip Game,ArrayDeque&lt;Node&gt;的更多相关文章

  1. poj1753 Flip Game(BFS+位压缩)

    题目链接 http://poj.org/problem?id=1753 题意 一个棋盘上有16个格子,按4×4排列,每个格子有两面,两面的颜色分别为黑色和白色,游戏的每一轮选择一个格子翻动,翻动该格子 ...

  2. poj1753 Flip Game

    题意:4*4的正方形,每个格子有黑白两面,翻转格子使得4*4个格子显示全黑或全白,翻转要求:选中的那个格子,以及其上下左右相邻的格子(如果存在)要同时翻转.输出最小的达到要求的翻转次数或者Imposs ...

  3. POJ-1753 Flip Game (BFS+状态压缩)

    Description Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of i ...

  4. poj1753 Flip Game —— 二进制压缩 + dfs / bfs or 递推

    题目链接:http://poj.org/problem?id=1753 Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submis ...

  5. POJ1753 Flip Game(bfs、枚举)

    链接:http://poj.org/problem?id=1753 Flip Game Description Flip game is played on a rectangular 4x4 fie ...

  6. POJ1753——Flip Game

    Flip Game Description Flip game is played on a rectangular 4x4 field with two-sided pieces placed on ...

  7. POJ-1753 Flip Game---二进制枚举子集

    题目链接: https://vjudge.net/problem/POJ-1753 题目大意: 有4*4的正方形,每个格子要么是黑色,要么是白色,当把一个格子的颜色改变(黑->白或者白-> ...

  8. POJ1753 Flip Game(位运算+暴力枚举)

    Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 16 square ...

  9. [POJ1753]Flip Game(异或方程组,高斯消元,枚举自由变量)

    题目链接:http://poj.org/problem?id=1753 题意:同上. 这回翻来翻去要考虑自由变元了,假设返回了自由变元数量,则需要枚举自由变元. /* ━━━━━┒ギリギリ♂ eye! ...

随机推荐

  1. python 关于文件操作的一些理解

    在用python进行数据处理编程中,往往涉及到文件IO口读写,IO口的读写性能会极大的影响程序的运行时间.在进行文件写入时,一般会存在两种情况.第一种是数据到来马上进行数据写入,即来一条写一条,第二种 ...

  2. A - Infinite Sequence

    Problem description Consider the infinite sequence of integers: 1, 1, 2, 1, 2, 3, 1, 2, 3, 4, 1, 2,  ...

  3. .net core2.0 自定义中间件

    一.中间件(Middleware) 中间件是被组装成一个应用程序管道来处理请求和响应的软件组件. 二.编写SimpleMiddleware using Microsoft.AspNetCore.Htt ...

  4. android.system.ErrnoException: open failed: ENOENT (No such file or directory) 07-19 20:27:45.011 66

    在操作安卓版本23+的文件读取时,不仅要在maniests中声明,还要在代码中动态声明: ; private static String[] PERMISSIONS_STORAGE = { Manif ...

  5. do…while语句

    有些情况下,不论条件是否满足,循环过程必须至少执行一次,这时可以采用do...while语句.就像如图7.4所示登录账号一样,需要先输入密码和账户名,后进行判断:如果密码始终不正确,则循环要求用户输入 ...

  6. 书不在多,精读则灵 - Oracle入门书籍推荐

      作者:eygle |English [转载时请标明出处和作者信息]|[恩墨学院 OCM培训传DBA成功之道]链接:http://www.eygle.com/archives/2006/08/ora ...

  7. div 背景放图和直接放图区别

    <html> <head> <meta charset="UTF-8"> <title></title> <sty ...

  8. SLAM: 图像角点检测的Fast算法(时间阈值实验)

    作为角点检测的一种快速方法,FastCornerDetect算法比Harris方法.SIft方法都要快一些,应用于实时性要求较高的场合,可以直接应用于SLAM的随机匹配过程.算法来源于2006年的Ed ...

  9. mysql中int、bigint、smallint 和 tinyint的区别与长度

    各种整形,总结留作参考. bigint 从 -2^63 (-9223372036854775808) 到 2^63-1 (9223372036854775807) 的整型数据(所有数字).存储大小为 ...

  10. json-lib与Jackson的区别和用法分析

    一.Jackson概述 1.jackson包和版本 Jackson fasterxml和codehaus的区别: 他们是Jackson的两大分支.也是两个版本的不同包名.Jackson从2.0开始改用 ...