POJ:1753-Flip Game(二进制+bfs)
题目链接: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)的更多相关文章
- POJ 1753 Flip Game(bfs+位压缩运算)
http://poj.org/problem?id=1753 题意:一个4*4的棋盘,只有黑和白两种棋子,每次翻转一个棋子,并且其四周的棋子也跟着翻转,求棋盘全为黑或全为白时所需的最少翻转次数. 思路 ...
- 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 ...
- 枚举 POJ 1753 Flip Game
题目地址:http://poj.org/problem?id=1753 /* 这题几乎和POJ 2965一样,DFS函数都不用修改 只要修改一下change规则... 注意:是否初始已经ok了要先判断 ...
- POJ 1753 Flip Game(高斯消元+状压枚举)
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 45691 Accepted: 19590 Descr ...
- POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 37427 Accepted: 16288 Descr ...
- poj 1753 Flip Game 枚举(bfs+状态压缩)
题目:http://poj.org/problem?id=1753 因为粗心错了好多次……,尤其是把1<<15当成了65535: 参考博客:http://www.cnblogs.com/k ...
- POJ 1753 Flip Game(状态压缩+BFS)
题目网址:http://poj.org/problem?id=1753 题目: Flip Game Description Flip game is played on a rectangular 4 ...
- POJ 1753 Flip Game(二进制枚举)
题目地址链接:http://poj.org/problem?id=1753 题目大意: 有4*4的正方形,每个格子要么是黑色,要么是白色,当把一个格子的颜色改变(黑->白或者白->黑)时, ...
- 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 ...
- OpenJudge/Poj 1753 Flip Game
1.链接地址: http://bailian.openjudge.cn/practice/1753/ http://poj.org/problem?id=1753 2.题目: 总时间限制: 1000m ...
随机推荐
- 《从0到1学习Flink》—— Mac 上搭建 Flink 1.6.0 环境并构建运行简单程序入门
准备工作 1.安装查看 Java 的版本号,推荐使用 Java 8. 安装 Flink 2.在 Mac OS X 上安装 Flink 是非常方便的.推荐通过 homebrew 来安装. brew in ...
- 选择控件js插件和使用方法
记录一下 选择控件js插件 /* * 滚动控件,包含地址选择.时间选择.自定义单选 */ //js_inputbox input 输入框 //js_pickviewselect 下拉单项选择 //js ...
- 在Unity3d中解析Lua脚本的方法
由于近期项目中提出了热更新的需求,因此本周末在Lua的陪伴下度过.对Lua与Unity3d的搭配使用,仅仅达到了一个初窥门径的程度,记录一二于此.水平有限,欢迎批评指正. 网络上关于Lua脚本和Uni ...
- .NET CORE IIS 500.21
最近遇到的.NET CORE 500.21的错误 官方解决方案地址:https://docs.microsoft.com/en-us/dynamics-nav/troubleshooting-http ...
- Wrinkles should merely indicate where smiles have been.
Wrinkles should merely indicate where smiles have been. 皱纹应该只是微笑留下的印记.
- Brackets安装angularjs插件
Brackets是Adobe公司研发的一款开源WEB前端开发框架,界面清爽简约,代码提示功能比较强大,而且支持第三方插件,其提供的插件库中有大量的对Brackets感兴趣的开发人员所开发的插件,使用者 ...
- i-nex安装教程
sudo add-apt-repository ppa:i-nex-development-team/stable sudo apt-get updatesudo apt-get i-nex
- CentOS7.2上搭建httpbin环境
CentOS7上搭建httpbin环境 1.安装python31)安装python3.6可能使用的依赖yum -y install openssl-devel bzip2-devel expat-de ...
- Windows7(x86) xampp php5.5 imagick install
I hate windows. 1. 下载安装 ImageMagick, 选择合适您电脑的版本,我下载的是: ImageMagick-6.8.9-1-Q16-x86-dll.exe http://ww ...
- linux下杀毒工具clamav
ClamAV 杀毒是Linux平台最受欢迎的杀毒软件,ClamAV属于免费开源产品,支持多种平台,如:Linux/Unix.MAC OS X.Windows.OpenVMS.ClamAV是基于病毒扫描 ...