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

Flip Game

Time Limit: 1000MS Memory Limit: 65536K

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:

Choose any one of the 16 pieces.

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的图,要求你通过一定的翻转将图全改为b或者w。每次翻转一个点这个点的上下左右也会跟着翻转。问最少需要翻转多少次。

  • 其实是一个很有意思的搜索题,因为每个点只有两种情况,所以可以把一个图看成一个二进制的数字,就可以使用位运算来模拟翻转, 然后bfs,看到达目标数字需要的最少步骤。


#include<stdio.h>
#include<queue>
#include<cstring>
using namespace std;
const int maxn = 1e5;
bool vis[maxn];
struct node
{
int va,step;
} now,Next;
char s[10][10]; void init()
{
memset(vis,0,sizeof(vis));
for(int i=1; i<4; i++)
scanf("%s",s[i]);
now.step = 0;
now.va = 0;
int t = 1;
for(int i=0; i<4; i++)
{
for(int j=0; j<4; j++)
{
int k;
if(s[i][j] == 'b')
k = 1;
else
k = 0;
now.va += k*t;
t *= 2;
}
}
} void deal(int pos)
{
int num[20],x = now.va;
Next.step = now.step + 1;
//先转化成二进制
for(int i=0; i<=15; i++)
{
num[i] = x % 2;
x /= 2;
}
num[pos] ^= 1;
if(pos > 4)
num[pos-4] ^= 1;
if(pos%4 != 3)
num[pos+1] ^= 1;
if(pos%4 != 0)
num[pos-1] ^= 1;
if(pos < 12)
num[pos+4] ^= 1;
int t= 1,ans = 0;
//操作之后转化为10进制,10进制更好标记状态
for(int i=0;i<16;i++)
{
ans += t*num[i];
t*= 2;
}
Next.va = ans;
} int bfs()
{
if(now.va == 0 || now.va == 65535)
return now.step;
queue<node> qu;
qu.push(now);
vis[now.va] = true;
while(!qu.empty())
{
now = qu.front();
qu.pop();
for(int i=0; i<16; i++)
{
deal(i);
if(!vis[Next.va])
{
if(Next.va == 0 || Next.va == 65535)//最终的情况代表的数字就是0或者65535
return Next.step;
vis[Next.va] = true;
qu.push(Next);
}
}
}
return -1;
} int main()
{
while(scanf("%s",s[0])!=EOF)
{
init();
int ans = bfs();
if(ans != -1)
printf("%d\n",ans);
else
printf("Impossible\n");
}
return 0;
}

POJ:1753-Flip Game(二进制+bfs)的更多相关文章

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

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

  2. poj 1753 Flip Game(bfs状态压缩 或 dfs枚举)

    Description Flip game squares. One side of each piece is white and the other one is black and each p ...

  3. 枚举 POJ 1753 Flip Game

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

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

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

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

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

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

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

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

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

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

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

  9. POJ 1753 Flip Game (状态压缩 bfs+位运算)

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

  10. OpenJudge/Poj 1753 Flip Game

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

随机推荐

  1. 041 First Missing Positive 第一个缺失的正数

    给一个未排序的数组,找出第一个缺失的正整数.例如,[1,2,0] 返回 3,[3,4,-1,1] 返回 2.你的算法应该在 O(n) 的时间复杂度内完成并且使用常数量的空间.详见:https://le ...

  2. Ubuntu下安装Yarm-PM2

    首先打开yarm的官网.https://www.yarnpkg.com/zh-Hant/ (一)yarn的官方安装方法: 1.上通过 Debian 套件安裝 Yarn,粘贴以下命令 curl -sS ...

  3. Emacs中自动刷新dired缓冲区

    Emacs中自动刷新dired缓冲区 在dired模式中,如果在不同buffer间切换,buffer不会自动更新,有时还需要手工按“g”键,比较麻烦,如下设置和代码能够在buffer切换和执行shel ...

  4. SQL Server 2012安装配置(Part4 )

    SQL Server 2012安装配置(Part1) SQL Server 2012安装配置(Part2) SQL Server 2012安装配置(Part3 ) SQL Server 2012安装配 ...

  5. Python+Selenium之断言对应的元素是否获取以及基础知识回顾

    # coding=utf-8 from selenium import webdriver driver = webdriver.Firefox() driver.maximize_window () ...

  6. userBean之设置属性

    package com.java.model; public class Student { private String name;private int age; public String ge ...

  7. Java异常之RuntimeException

    人生不如意十有八九.在打Core Java里面的例子的时候总是一遍就过,但是实际上只要是自己想着动手去打造自己想要的东西,异常的状况也是十有八九的. 在Java中会使用异常处理的错误捕获机制处理这些异 ...

  8. 【UML】协作图Collaboration diagram(交互图)(转)

    http://blog.csdn.net/sds15732622190/article/details/49402269 前言         学完UML时序图,就要看一下UML协作图,因为两张图是相 ...

  9. HDU 5459 Jesus Is Here (递推,组合数学)

    有点麻烦的递推,递推的原则:向小的问题方向分解,注意边界. 字符串的递推式为 定义f为Si中的总方案数 首先可以得到 fi=fi-1+fi-2+组合(si-2,si-1) 然后考虑Si-2和Si-1之 ...

  10. 2002-2003 ACM-ICPC Northeastern European Regional Contest (NEERC 02) A Amusing Numbers (数学)

    其实挺简单的.先直接算出之前已经排在k这个数前面的数字.比如543是三位的,那么100~543都是可以的,两位的10~54. 如果还需要往前面补的话,那么依次考虑1000~5430,5430是上界不能 ...