点击打开链接

Flip Game
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 25674   Accepted: 11093

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

题目大意就是给一个棋盘,每次可以选一个点进行一次操作,操作的方法就是:把这个点和这个点上下左右的四个点的颜色反一下,就是黑变成白,白变成黑,问最少多少步整个棋盘能变成一个颜色

广度优先搜索,需要注意的是把一个棋盘压缩成一个整数,每一位代表棋盘上的一个棋子,这样就涉及到了位操作,

#include<stdio.h>
#include<queue>
using namespace std; queue<int> q;
bool flag[0x10000];
int step[0x10000];
int calculate(int num, int i)
{
int p = 1<< i;
if(i % 4 != 0)
p |= 1<<(i - 1);
if((i + 1) % 4 != 0)
p |= 1 <<(i + 1);
if(i > 3)
p |= 1 <<(i - 4);
if(i < 12)
p |= 1 <<(i + 4);
return num ^ p;
}
int bfs()
{
while(!q.empty())
{
int num = q.front();
q.pop();
int i;
for(i = 0; i < 16; i++)
{
int new_num = calculate(num, i); if(flag[new_num] != 1)
{
if(new_num == 0 || new_num == 0xffff)
return step[num] + 1;
q.push(new_num);
flag[new_num] = 1;
step[new_num] = step[num] + 1;
}
}
}
return -1;
}
int main()
{
char ch;
int num = 0;
int i = 16;
// freopen("test.txt", "r", stdin);
while(i--)
{
scanf("%c", &ch);
// memset(flag, 0, sizeof(flag));
if(ch == '\n' || ch == ' ')
{
i++;
continue;
}
num <<= 1;
if(ch == 'w')
num++;
}
flag[num] =1;
q.push(num);
step[num] = 0;
if(num == 0 || num == 0xffff)
{
printf("0\n");
return 0;
}
int t = bfs();
if(t != -1)
printf("%d\n", t);
else
printf("Impossible\n"); return 0;
}

poj 1753 Flip Game的更多相关文章

  1. 枚举 POJ 1753 Flip Game

    题目地址:http://poj.org/problem?id=1753 /* 这题几乎和POJ 2965一样,DFS函数都不用修改 只要修改一下change规则... 注意:是否初始已经ok了要先判断 ...

  2. POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37427   Accepted: 16288 Descr ...

  3. POJ 1753 Flip Game(高斯消元+状压枚举)

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 45691   Accepted: 19590 Descr ...

  4. OpenJudge/Poj 1753 Flip Game

    1.链接地址: http://bailian.openjudge.cn/practice/1753/ http://poj.org/problem?id=1753 2.题目: 总时间限制: 1000m ...

  5. POJ 1753 Flip Game DFS枚举

    看题传送门:http://poj.org/problem?id=1753 DFS枚举的应用. 基本上是参考大神的.... 学习学习.. #include<cstdio> #include& ...

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

    题目网址:http://poj.org/problem?id=1753 题目: Flip Game Description Flip game is played on a rectangular 4 ...

  7. poj 1753 Flip Game 枚举(bfs+状态压缩)

    题目:http://poj.org/problem?id=1753 因为粗心错了好多次……,尤其是把1<<15当成了65535: 参考博客:http://www.cnblogs.com/k ...

  8. POJ 1753 Flip Game (DFS + 枚举)

    题目:http://poj.org/problem?id=1753 这个题在開始接触的训练计划的时候做过,当时用的是DFS遍历,其机制就是把每一个棋子翻一遍.然后顺利的过了.所以也就没有深究. 省赛前 ...

  9. POJ 1753 Flip Game(二进制枚举)

    题目地址链接:http://poj.org/problem?id=1753 题目大意: 有4*4的正方形,每个格子要么是黑色,要么是白色,当把一个格子的颜色改变(黑->白或者白->黑)时, ...

  10. POJ 1753 Flip Game(bfs+位压缩运算)

    http://poj.org/problem?id=1753 题意:一个4*4的棋盘,只有黑和白两种棋子,每次翻转一个棋子,并且其四周的棋子也跟着翻转,求棋盘全为黑或全为白时所需的最少翻转次数. 思路 ...

随机推荐

  1. C#.Net中的转义字符

    当声明一个字符串变量时有一些字符是不能以平常的方式包含在变量中的.为了解决这个问题,C#提供了两种不同的方法. 第一种方法是使用’转义序列’.例如,我们想得到如下的字符串 “Hello World H ...

  2. 在阿里云 centos 6.3上面安装php5.2(转)

    由于php程序使用了Zend Optimizer,只能使用php5.2, yum 上的php 是5.3的版本,只能重新安装php:安装步骤如下: 先卸载 php5.3的相关东西: yum remove ...

  3. 使用UpdatePanel 页面脚本不起作用

    在后台中这样注册js方法 ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "", " ...

  4. c#.net 访问SQL SERVER 时提示:尝试读取或写入受保护的内存。这通常指示其他内存已损坏

    解决方法: 管理员身份运行 cmd ->  输入 netsh winsock reset 然后重启电脑. 引用 :http://blog.csdn.net/laijieyao/article/d ...

  5. Chrome离线下载地址

    每当chrome有更新之后,都有不少用户想要下载离线版的安装文件,但苦于找不到下载地址而发愁,其实这个问题很简单,下面我来分享一下方法(仅针对Windows操作系统): 对于稳定版(正式版)Chrom ...

  6. sublime安装DocBlockr注释插件

    点击sublime的菜单栏 view->show console :现在打开了控制台, 这个控制台有上下两栏, 上面一栏会实时显示sublime执行了什么插件,输出执行结果, 如果你安装的某个插 ...

  7. 数据库随机取n条记录

    SQL Server SELECT TOP N  *  FROM  表名  ORDER BY  NEWID() My SQL  SELECT  *  FROM  表名 ORDER BY RAND() ...

  8. ADF_General JSF系列1_创建一个简单的JSF Application

    2015-02-17 Creatd By BaoXinjian

  9. ADF_ADF Faces系列1_使用JSF开发基于Ajax的用户界面:ADF Faces 富客户端组件简介(Part1)

    2013-05-01 Created By BaoXinjian

  10. 2016 Multi-University Training Contest 5 ATM Mechine

    ATM Mechine 本文转自:http://blog.csdn.net/queuelovestack/article/details/52096337 题意: 这题的意思还是比较费解的 Alice ...