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 ...
随机推荐
- C# winform 跨线程更改窗体控件的属性
当winform程序中新开一个线程,是无法改变主线程中窗体控件的属性的,否则运行时会报错. 若想在其他线程中控制主线程中的窗体控件,则必须利用BeginInvoke方法. 例如:添加一个名为textb ...
- java中scanner类的用法
在Eclipse中编写程序时,如果我们的变量是需要手动输入的时候,我们就可以用到scanner类了. Scanner类,这是一个用于扫描输入文本的新的实用程序.由于任何数据都必须通过同一模式的捕获组检 ...
- express框架
一.express的使用 安装express 在根目录下输入 npm install express 引入express的包 const express = require("express ...
- "飞机大战"游戏_Java实现_详细注释
1 package cn.xiaocangtian.Util; import java.awt.Frame; import java.awt.Graphics; import java.awt.Ima ...
- PHP反射API
近期忙着写项目,没有学习什么特别新的东西,所以好长时间没有更新博客.我们的项目用的是lumen,是基于laravel的一个轻量级框架,我看到里面用到了一些反射API机制来帮助动态加载需要的类.判断方法 ...
- AOP 面向切面编程, Attribute在项目中的应用
一.AOP(面向切面编程)简介 在我们平时的开发中,我们一般都是面对对象编程,面向对象的特点是继承.多态和封装,我们的业务逻辑代码主要是写在这一个个的类中,但我们在实现业务的同时,难免也到多个重复的操 ...
- CSS布局经典—圣杯布局与双飞翼布局
在我之前的博客网页整体布局完全剖析-剖完你不进来看一下么?中总结单列.两列.三列固宽与变宽布局,我还以为已经囊括了所有经典的网页布局方法了呢,当然除了CSS3的弹性盒模型没有涉及到,现在看来确实是自己 ...
- linux系统加快大文件的写入速度
linux系统加快大文件的写入速度 setvbuf进行优化内存IO
- linux线程
线程:轻量级进程,在资源.数据方面不需要进行复制 不间断地跟踪指令执行的路径被称为执行路线 进程的结构:task_struck:地址空间 线程:轻量级的进程 在同一个进程中创建的线程,在共享进程的地址 ...
- 学习微信小程序之css3display
一display diaplay改变标签的模式,行内装块级(block),块级转行内(inline) 通过设置display为none,可以 让整个标签在页内移除掉 设置visibility为hidd ...