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. 【转】MMO即时战斗:技能实现

    转自 http://blog.csdn.net/cyblueboy83/article/details/41628743 一.前言 基本所有MMO游戏无论是回合制.策略类.即时战斗等等类型都需要有相应 ...

  2. 单元测试模块unittest使用学习

    工作原理: unittest中最核心的四个概念是:test case, test suite, test runner, test fixture. 一个TestCase的实例就是一个测试用例.什么是 ...

  3. 【第五章】MySQL数据库的安全机制

    MySQL权限表MySQL用户管理MySQL权限管理SSL加密连接

  4. 软件工程课堂作业(一)——随机产生四则运算题目(C++)

    一.设计思想: 1.首先主函数只用来调用随机产生并输出运算题目函数,随机产生并输出这一部分功能用一个randout函数实现: 2.随机产生运算数这一功能,两个运算数可以用随机函数生成,并将它们控制在1 ...

  5. linux下php环境配置

    参: http://www.laozuo.org/5542.html LAMP,基于Linux/Apache/MySQL/PHP架构的网站建设环境,对于一般的网站来说足够使用,如果我们的网站访问量或者 ...

  6. TCP源码—系统调用

    1.socket SYSCALL_DEFINE3(socket, int, family, int, type, int, protocol) sys_socket->sock_create-& ...

  7. 我爱C语言

    各位同志们好,我是来自计算机系的谢畅,我是一个平时看起来高冷其实很逗比的人,我的爱好有很多但只是会一些基础比如游泳,篮球,听听音乐什么的.我的特长是弹吉他虽然弹得不是很溜,我还喜欢朗诵.刚开始我并不是 ...

  8. PHPcms企业黄页中,会员注册之后提示经营模式不得少于1个字符 的解决办法

    后台--模块- 黄页模块 ---  企业库 --- 字段 ---   经营模式 ---  字符长度取值范围 1  改为  0. 

  9. centos设置时间同步

    1.安装ntpdate #yum install ntpdate   2. #cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime #ntpdate ...

  10. python2 对URL编码进行编译

    在请求页面时有时会返回类似: %E8%AF%A5985%E5%A4%A7%E5%AD%A6%E5%B8%B8%E5%B9%B4%E4%BD%8D%E5%B1%85%E5%9B%BD%E5%86%85% ...