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. 最小生成树(II)与Kruskal算法

    为防止网页加载过慢,故分两章.上接https://www.cnblogs.com/Uninstalllingyi/p/10479470.html Kruskal算法——将森林合并成树 玩过瘟疫公司吗… ...

  2. [转]Excel数据转化为sql脚本

    在实际项目开发中,有时会遇到客户让我们把大量Excel数据导入数据库的情况.这时我们就可以通过将Excel数据转化为sql脚本来批量导入数据库. 1 在数据前插入一列单元格,用来拼写sql语句. 具体 ...

  3. 地牢逃脱(BFS(广度优先搜索))

    题目描述 给定一个 n 行 m 列的地牢,其中 '.' 表示可以通行的位置,'X' 表示不可通行的障碍,牛牛从 (x0 , y0 ) 位置出发,遍历这个地牢,和一般的游戏所不同的是,他每一步只能按照一 ...

  4. jquery取radio单选按钮

    // var strMess = '<%=Exchange() %>';//            if (strMess == "兑换成功") {//         ...

  5. js实现滑动器效果

    最近公司在做一个项目,页面中要用到滑动器效果,我的第一反应是使用HTML5 input类型中的range类型,但马上我就否定了这个想法,因为range类型存在浏览器的兼容性问题(在主流浏览器中).但又 ...

  6. AndroidUI设计之 布局管理器 - 详细解析布局实现

    写完博客的总结 : 以前没有弄清楚的概念清晰化 父容器与本容器属性 : android_layout...属性是本容器的属性, 定义在这个布局管理器的LayoutParams内部类中, 每个布局管理器 ...

  7. UVALive - 6856 Circle of digits 后缀数组+二分

    题目链接: http://acm.hust.edu.cn/vjudge/problem/82135 Circle of digits Time Limit: 3000MS 题意 把循环串分割成k块,让 ...

  8. [core python programming]chapter 7 programming MS office

    excel.pyw会有问题,解决如下: 因为python3x中没有tkMessageBox模块,Tkinter改成了tkinter你可以查看你的py当前支持的模块.在交互式命令行下输入>> ...

  9. 项目UML设计(团队)

    [团队信息] 团队项目: 小葵日记--主打记录与分享模式的日记app 队名:日不落战队 队员信息及贡献分比例: 短学号 名 本次作业博客链接 此次作业任务 贡献分配 备注 501 安琪 http:// ...

  10. .net下将exe改造为既能双击独立运行又能注册为windows服务

    最近项目中需要将一些业务的处理程序改造为windows服务,但是考虑到实际需求,也需要能够直接双击运行这些处理程序.首先第一步想到的就是原来的项目不变,只需要在加一个windows服务的项目就行.但是 ...