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 ...
随机推荐
- MVC Code First 自动生成数据库时生成的表名会多了一个s
如图:我的类文件都是不带s的
- [转]程序员趣味读物:谈谈Unicode编码
from : http://pcedu.pconline.com.cn/empolder/gj/other/0505/616631_all.html#content_page_1 这是一篇程序员写给程 ...
- STL string的构造函数
前几天在网上,一位网友问我几个问题如下: , 'A'); string S1 = "abcdefg"; , ); ); cout << "s0 = " ...
- ASP.NET知识总结(8.AJAX异步)
AJAX:”Asynchronous JavaScript and XML” 中文意思:异步JavaScript和XML. 指一种创建交互式网页应用的网页开发技术. 不是指一种单一的技术,而是有机 ...
- $\mathscr{F}$类
$\mathscr{F}$类:在单位元盘$B(0,1)$中满足$$f(0)=0,f'(0)=1$$ 的双全纯函数的全体.
- UIAlertController使用
// 将UIAlertController模态出来 相当于UIAlertView show 的方法// 初始化一个一个UIAlertController // 参数preferredStyle: ...
- Lattice FIFO 使用之FIFO_DC输入输出宽度不同时 的一个注意事项
在使用FIFO_DC的时候,我们知道这个FIFO的一个功能是可以输入输出的数据宽度不一样,比如: 输入数据为128bit,输出数据为16bit,FIFO内部可以实现这样的转换,但是输出的时候是先送出一 ...
- 手机开发中的AP与BP的概念
转自:http://blog.csdn.net/macong01/article/details/15504611 手机的AP和BP: AP:ApplicationProcessor,即应用芯片 BP ...
- SecureCRT在远程主机和本地之间传输文件
概述 Linux系统不像Window系统有清晰的操作页面,数据能Copy下来,远程Linux的数据,就牵扯到本地数据与远程服务器数据交换的问题.Linux远程攻击SecureCrt提供了2种方式,可以 ...
- Leetcode: Palindrome Partitioning II
参考:http://www.cppblog.com/wicbnu/archive/2013/03/18/198565.html 我太喜欢用dfs和回溯法了,但是这些暴力的方法加上剪枝之后复杂度依然是很 ...