链接:http://poj.org/problem?id=1753

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的格子,可以用二进制的0、1表示每个格子的黑白,第i个格子为黑即第i位的二进制数为1。这样16个格子需要16位二进制,即一共有2^16-1个状态。从初始状态开始枚举每一种状态改变16个格子的颜色所能导致的状态。出现16位全1或者全0就输出Impossible。
代码:
#include <iostream>
#include <stdio.h>
#include <queue>
#include <string.h>
using namespace std;
#define MAXX (1<<16)
bool flag[MAXX];
int step[MAXX];
queue<int> que;
void input()
{
int state=;
char ch;
int i;
for(i=;i<MAXX;i<<=)
{
scanf("%c",&ch);
if(ch=='\n')
scanf("%c",&ch);
if(ch=='b')
state+=i;
}
que.push(state);
memset(flag,false,sizeof(flag));
memset(step,,sizeof(step));
flag[state]=true;
//printf("%d ",state);
}
void change(int n,int state)
{
int temp=state;
if(n->=)
state^=(<<(n-));
if(n+<)
state^=(<<(n+));
if(n!=&&n!=&&n!=&&n!=)
state^=(<<(n-));
if(n!=&&n!=&&n!=&&n!=)
state^=(<<(n+));
state^=(<<n);
if(!flag[state])
{
flag[state]=true;
que.push(state);
step[state]=step[temp]+;
}
}
void getall()
{
int state;
int i;
while(!que.empty())
{
state=que.front();
if(state==||state==MAXX-)
{
printf("%d\n",step[state]);
return;
}
que.pop();
for(i=;i<;i++)
{
change(i,state);
}
}
printf("Impossible\n");
}
int main()
{
input();
getall();
return ;
}

POJ1753 Flip Game(bfs、枚举)的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

  7. [POJ1753]Flip Game(开关问题,枚举)

    题目链接:http://poj.org/problem?id=1753 和上一个题一样,将初始状态存成01矩阵,就可以用位运算优化了.黑色白色各来一遍 /* ━━━━━┒ギリギリ♂ eye! ┓┏┓┏ ...

  8. POJ 1753 Flip Game (枚举)

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 26492   Accepted: 11422 Descr ...

  9. poj1753 Flip Game

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

随机推荐

  1. Visual Studio Emulator for Android 的安装与使用 感觉最干净好看的模拟器.

    Step1 win8+ 6G+ 添加删除程序\ hyper  创建虚拟机 安装visual studio android 模拟器, 自带三个模拟器 使用管理员打开模拟器 参考文献 Visual Stu ...

  2. 深入理解Web标准(网站标准)

    深入理解Web标准(网站标准)   我觉得一名Web前端应该好好理解Web标准到底是什么,为什么要在我们的实际实践中遵循Web标准. 什么是Web标准.百度百科的解释是: WEB标准不是某一个标准,而 ...

  3. 只有IE64位能上网。

  4. css实现容器垂直水平居中的七中方法

    方法一:position加margin 方法二: diaplay:table-cell 方法三:position加 transform 方法四:flex;align-items: center;jus ...

  5. Linux中文件颜色所代表的属性和颜色

    绿色文件: 可执行文件,可执行的程序    红色文件:压缩文件或者包文件   蓝色文件:目录   白色文件:一般性文件,如文本文件,配置文件,源码文件等    浅蓝色文件:链接文件,主要是使用ln命令 ...

  6. linux作业

    第二单元 (1)以root用户登录GNOME图形界面 语言支持选择为汉语 (2)使用快捷键切换到虚拟终端2,使用普通用户身份登录,查看系统提示符 (3)使用命令退出虚拟终端2上登录的用户 (4)使用快 ...

  7. 数据库连接池c3p0学习

    这里只记录c3p0的数据源,不会涉及到其它方面和别的数据库连接池的对比 配置文件主要的实现方式有三种: 1.手写代码去加载一个配置文件 创建一个config.properties文件如下: drive ...

  8. thinkphp验证是否登录并跳转

    CommonController.class.php <?php namespace Admin\Controller; use Think\Controller; class CommonCo ...

  9. Python的多类型传值和冗余参数

    多类型传值(向函数中传递元组和字典) 1 向函数中传递元组 def func(x,y): print x+y     调用这个函数的时候,我们只需要传入两个变量就可以了,但是比如我有一个元组t = ( ...

  10. Python 学习第十八天 js 正则及其它前端知识

    一,js 正则表达式 test 判断制度串是否符合规定的正则 (1)定义正则表达式匹配规则         js 中定义正则表达式为rep=/\d+/,两个//之间为正则模式 (2)rep.test( ...