POJ1753 Flip Game(bfs、枚举)
链接:http://poj.org/problem?id=1753
Flip Game
Description
- 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
Output
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、枚举)的更多相关文章
- poj1753 Flip Game(BFS+位压缩)
题目链接 http://poj.org/problem?id=1753 题意 一个棋盘上有16个格子,按4×4排列,每个格子有两面,两面的颜色分别为黑色和白色,游戏的每一轮选择一个格子翻动,翻动该格子 ...
- poj1753 Flip Game —— 二进制压缩 + dfs / bfs or 递推
题目链接:http://poj.org/problem?id=1753 Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submis ...
- POJ-1753 Flip Game---二进制枚举子集
题目链接: https://vjudge.net/problem/POJ-1753 题目大意: 有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 ...
- POJ1753 Flip Game(位运算+暴力枚举)
Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 16 square ...
- [POJ1753]Flip Game(异或方程组,高斯消元,枚举自由变量)
题目链接:http://poj.org/problem?id=1753 题意:同上. 这回翻来翻去要考虑自由变元了,假设返回了自由变元数量,则需要枚举自由变元. /* ━━━━━┒ギリギリ♂ eye! ...
- [POJ1753]Flip Game(开关问题,枚举)
题目链接:http://poj.org/problem?id=1753 和上一个题一样,将初始状态存成01矩阵,就可以用位运算优化了.黑色白色各来一遍 /* ━━━━━┒ギリギリ♂ eye! ┓┏┓┏ ...
- POJ 1753 Flip Game (枚举)
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26492 Accepted: 11422 Descr ...
- poj1753 Flip Game
题意:4*4的正方形,每个格子有黑白两面,翻转格子使得4*4个格子显示全黑或全白,翻转要求:选中的那个格子,以及其上下左右相邻的格子(如果存在)要同时翻转.输出最小的达到要求的翻转次数或者Imposs ...
随机推荐
- 安装Java的IDE Eclipse时出现java.net.SocketException,出现错误Installer failed,show.log
ERROR: org.eclipse.equinox.p2.transport.ecf code=1002 Unable to read repository at http://download.e ...
- MFC 静态文本的自绘 空心字的实现
想在对话框里,显示几个字是很简单的,只要用静态文本的输出就可以了.然而有时候我们需要显示特效的字,我们希望显示的文字就像Word里的艺术字一样,看起来美观.这时我们可以重写CStatic类.用Draw ...
- iOS - 如何切图适配各种机型
关于iPhone6/6+适配问题一直有争议,今天小编专门为大家整理了相关的有效方案,希望对大伙儿有帮助! 移动app开发中多种设备尺寸适配问题,过去只属于Android阵营的头疼事儿,只是很多设计师选 ...
- c# 使用泛型类型作为参数
泛型作为一种经常使用的类型,有时需要在方法之间传递,不了解的话可能会束手无策. 比如我定义一个方法 processList 需要把List<T> 作为参数传入,这时可以这样写 publ ...
- 【Java远程debug】
转自 http://blog.csdn.net/hongchangfirst/article/details/44191925 一.远程debug原理 Java远程调试的原理是两个JVM之间通过deb ...
- {二逼小青年的记事簿}为什么treelist不会显示子节点的文字?
<TreeView Name="treeView" DockPanel.Dock="Left" MinWidth="200" > ...
- HTTP 的重定向301,302,303,307(转)
HTTP 的重定向301,302,303,307(转) (2012-12-11 11:55:04) 转载▼ 标签: 杂谈 分类: 网络 301 永久重定向,告诉客户端以后应从新地址访问.302 作为H ...
- 配置mongoDB服务
上一节说到mongoDB的环境搭建,但是那种方法启动mongoDB太繁琐了. 今天先说说简化mongoDB启动的配置. 首先在命令行中运行的”C:\Program Files\MongoDB 2.6 ...
- 使用 CommandLineApplication 类创建专业的控制台程序
闲话 在很久很久以前,电脑是命令行/终端/控制台的天下,那屏幕上的光标在行云流水般的键盘敲击下欢快地飞跃着,那一行行的字符输出唰唰唰地滚动着--直到 Windows 95 的出现(那时候我还不知道苹果 ...
- 上传图片预览JS脚本 Input file图片预览的实现示例
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...