题目链接: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. TDH-大数据基础

    ------------------------------------------------------------------------------------*******大数据概念和基础* ...

  2. 牛客网Java刷题知识点之抽象类与接口

    不多说,直接上干货! 接口和内部类为我们提供了一种将接口与实现分离的更加结构化的方法. 抽象类与接口是Java语言中对抽象概念进行定义的两种机制,正是由于它们的存在才赋予java强大的面向对象的能力. ...

  3. 开发工具~nuget配置本地源

    我们在本地部署了自己的nuget服务器,有时可以需要用到nuget restore命令去恢复包包,它会从下面的nuget.config里读你的配置源信息,就是在这里,我们要把内测的nuget服务器路径 ...

  4. Java基础语法(自定义类、ArrayList集合)

    Java基础语法 今日内容介绍 u 自定义类 u ArrayList集合 第1章 引用数据类型(类) 1.1 引用数据类型分类 提到引用数据类型(类),其实我们对它并不陌生,如使用过的Scanner类 ...

  5. 使用OpenSSH远程管理Linux服务器

    一.使用OpenSSH远程管理Linux服务器 sshd是OpenSSH的服务器端守护进程,与之对应的Windows下客户端软件有SecureCRT/Xshell/PuTTY等. OpenSSH一般为 ...

  6. Android 4.4及以后将内容布局延伸到状态栏

    首先说明:该文章不是大家说的沉浸式状态栏,网上沉浸式状态栏的博客很多,搜索就有了! 该篇博客的主要目的就是为了将图片显示在状态栏上,让APP看起来更有型!如下图所示:   界面 这个界面的布局就是co ...

  7. Nginx和Apache服务器上配置反向代理

    在实际项目过程中,由于网站要用到一个在线编辑器(个性化的在线编辑软件),需要跨域进行通信!由于跨域通信较多,所以当时就想到在网站服务器上代理编辑软件的请求! 这就是“反向代理”的实际需求! 一.Ngi ...

  8. AutoIt上传非input控件方式的文件脚本

    AutoIt目前最新是v3版本,这是一个使用类似BASIC脚本语言的免费软件,它设计用于Windows GUI(图形用户界面)中进行自动化操作.它利用模拟键盘按键,鼠标移动和窗口/控件的组合来实现自动 ...

  9. Modelsim与Simulink协同仿真

    当使用硬件描述语言(HDL)完成电路设计时,往往需要编写Testbench对所设计的电路进行仿真验证,测试设计电路的功能是否与预期的目标相符.而编写Testbench难度之大,这时可以借助交互式图形化 ...

  10. BZOJ 3130: [Sdoi2013]费用流 网络流+二分

    3130: [Sdoi2013]费用流 Time Limit: 10 Sec  Memory Limit: 128 MBSec  Special JudgeSubmit: 1230  Solved: ...