Flip Game
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 44450   Accepted: 19085

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: 
  1. Choose any one of the 16 pieces.
  2. 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

Source

题意:
4*4的黑白棋盘,给出初始状态,翻转一个棋子他周围的4个棋子也翻转,问把所有棋子变成同一种颜色的最少反转次数。
代码:
//求最少反转次数可以bfs,因为只有4*4所以可以把每行的状态压缩一下用四维数组来标记是否出现过此状态。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
int mp[][],vis[<<][<<][<<][<<];
struct Node{
int mp[][],tim;
}no1,no2;
void Make(Node a){
int sta[]={};
for(int i=;i<;i++){
for(int j=;j<;j++)
sta[i]|=(a.mp[i][j]<<(-j));
}
vis[sta[]][sta[]][sta[]][sta[]]=;
}
bool Chack(Node a){
for(int i=;i<;i++)
for(int j=;j<;j++)
if(a.mp[i][j]!=a.mp[][]) return false;
return true;
}
bool Vis(Node a){
int sta[]={};
for(int i=;i<;i++){
for(int j=;j<;j++)
sta[i]|=(a.mp[i][j]<<(-j));
}
if(vis[sta[]][sta[]][sta[]][sta[]]) return ;
return ;
}
Node Change(int x,int y,Node a){
for(int i=;i<;i++){
for(int j=;j<;j++){
if(i==x&&j==y){
a.mp[i][j]=!a.mp[i][j];
if(i+<=) a.mp[i+][j]=!a.mp[i+][j];
if(i->=) a.mp[i-][j]=!a.mp[i-][j];
if(j+<=) a.mp[i][j+]=!a.mp[i][j+];
if(j->=) a.mp[i][j-]=!a.mp[i][j-];
a.tim++;return a;
}
}
}
}
int Bfs(){
memset(vis,,sizeof(vis));
queue<Node>q;
no1.tim=;
q.push(no1);
Make(no1);
while(!q.empty()){
no1=q.front();q.pop();
if(Chack(no1)) return no1.tim;
for(int i=;i<;i++){
for(int j=;j<;j++){
no2=Change(i,j,no1);
if(Vis(no2)) continue;
Make(no2);
q.push(no2);
}
}
}
return -;
}
int main()
{
char s[];
for(int i=;i<;i++){
scanf("%s",s);
for(int j=;j<;j++)
no1.mp[i][j]=(s[j]=='b'?:);
}
int ans=Bfs();
if(ans==-) printf("Impossible\n");
else printf("%d\n",ans);
return ;
}

POJ 1753 BFS的更多相关文章

  1. POJ 1753 bfs+位运算

    T_T ++运算符和+1不一样.(i+1)%4 忘带小括号了.bfs函数是bool 型,忘记返回false时的情况了.噢....debug快哭了...... DESCRIPTION:求最少的步骤.使得 ...

  2. [ACM训练] 算法初级 之 基本算法 之 枚举(POJ 1753+2965)

    先列出题目: 1.POJ 1753 POJ 1753  Flip Game:http://poj.org/problem?id=1753 Sample Input bwwb bbwb bwwb bww ...

  3. POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37427   Accepted: 16288 Descr ...

  4. 枚举 POJ 1753 Flip Game

    题目地址:http://poj.org/problem?id=1753 /* 这题几乎和POJ 2965一样,DFS函数都不用修改 只要修改一下change规则... 注意:是否初始已经ok了要先判断 ...

  5. poj 1753 2965

    这两道题类似,前者翻转上下左右相邻的棋子,使得棋子同为黑或者同为白.后者翻转同行同列的所有开关,使得开关全被打开. poj 1753 题意:有一4x4棋盘,上面有16枚双面棋子(一面为黑,一面为白), ...

  6. POJ 1222 POJ 1830 POJ 1681 POJ 1753 POJ 3185 高斯消元求解一类开关问题

    http://poj.org/problem?id=1222 http://poj.org/problem?id=1830 http://poj.org/problem?id=1681 http:// ...

  7. POJ 1753 Flip Game(高斯消元+状压枚举)

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 45691   Accepted: 19590 Descr ...

  8. 穷举(四):POJ上的两道穷举例题POJ 1411和POJ 1753

    下面给出两道POJ上的问题,看如何用穷举法解决. [例9]Calling Extraterrestrial Intelligence Again(POJ 1411) Description A mes ...

  9. poj 1753 Flip Game 枚举(bfs+状态压缩)

    题目:http://poj.org/problem?id=1753 因为粗心错了好多次……,尤其是把1<<15当成了65535: 参考博客:http://www.cnblogs.com/k ...

随机推荐

  1. oraclize预言机资料

    oraclize预言机资料 智能合约如何可信的与外部世界交互: https://blog.csdn.net/sportshark/article/details/77477842 国外一篇讲得很详细的 ...

  2. KVM存储虚拟化---玩转openstack

    KVM 的存储虚拟化是通过存储池(Storage Pool)和卷(Volume)来管理的. Storage Pool 是宿主机上可以看到的一片存储空间,可以是多种类型,后面会详细讨论.Volume 是 ...

  3. MyBatis 插件 : 打印 SQL 及其执行时间

    Plugins 摘一段来自MyBatis官方文档的文字. MyBatis允许你在某一点拦截已映射语句执行的调用.默认情况下,MyBatis允许使用插件来拦截方法调用: Executor(update. ...

  4. chameleon-Mini(迷你变色龙)

    Chameleon Mini(迷你变色龙)是一个比一般信用卡稍大的小型开发板,是开源产品. 如图 Chameleon Mini可以完全复制许多商业非接触式智能卡包括UID卡,在内的全部内容,因此可以用 ...

  5. MySQL用户管理及权限管理

    MySQL 默认有个root用户,但是这个用户权限太大,一般只在管理数据库时候才用.如果在项目中要连接 MySQL 数据库,则建议新建一个权限较小的用户来连接. 在 MySQL 命令行模式下输入如下命 ...

  6. 常用linux命令相关

    [查看端口] netstat -tlnp netstat命令 netstat -an | grep 3306 3306替换成需要grep的端口号 lsof命令 通过list open file命令可以 ...

  7. 《剑指Offer》题一~题十

    一.赋值运算符函数 题目:如下为类型CMyString的声明,请为该类型添加赋值运算符函数. class CMyString { public: CMyString(char *pData = nul ...

  8. 【Android 应用开发】Ubuntu 下 Android Studio 开发工具使用详解

    . 基本上可以导入项目开始使用了 ... . 作者 : 万境绝尘 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/21035637 ...

  9. 硬盘引导扇区、多分区图、不通硬盘的LINUX逻辑分区数量

    主要启动记录区(Master Boot Record,MBR):可以安装开机管理程序的地方,有446byte 分割表(Paritition table):记录整块硬盘分割的状态,有64bytes 下面 ...

  10. 伟大的淘宝IP库的API接口竟然提示503挂掉了

    1 淘宝IP库惊现503错误 吃完晚饭,大概6点半了,天色已暗,太阳早就落山了.回到宿舍打开博客一看,傻眼了:博客每篇文章的评论者的地理信息全部处于“正在查询中……”的状态.这神马情况,不会是被淘宝封 ...