Flip Game

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 44   Accepted Submission(s) : 17
Problem 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
PKU
 
 
通过这个题我对搜索的应用和利用位运算对状态进行记录的方法有了更深的认识。
将每种棋盘状态用一个二进制数来记录,通过异或运算模拟每次翻面操作。先用1表示黑色棋子,0表示白色棋子,将棋盘表示成一个16位的二进制整数(0到65535之间),创建一个队列,将当前棋盘的状态(用一个0到65355之间的整数表示)进队,然后将对队头的16种可能的操作(用异或运算和移位运算完成)产生的情况(用一个0到65355之间的整数表示)进队,然后队头元素出队,再对此时的对头元素重复进行该操作......注意每次进队操作都要用bool数组对该种情况进行标记,如果产生相同的情况就说明有能达到该种状态更快捷的方法,就不入队,用一个整型的step数组记录每次操作所需的步数......循环直至队头指针等于队尾指针或者出现全黑或全白的状态(即65535和0),输出impossible或者步数。
 
//#include<iostream>
//using namespace std;
//int binary(int a[])
//{
// int i,t=1,res=0;
// for(i=1;i<=16;i++)
// {
// res+=a[i]*t;
// t*=2;
// }
// return res;
//}
//void copy(int a[],int b[])
//{
// int i;
// for(i=1;i<=16;i++)
// b[i]=a[i];
//}
//int main()
//{
//
// int i,j,a[17],b[17],t=0,front=0,rear=0,step[65536]={0};
// int **queue=new int*[65536*2];
// for(i=0;i<65536;i++)
// queue[i]=new int[17];
// bool f[65536]={false};
// char sq[4][5];
// step[0]=0;
// for(i=0;i<4;i++)
// {
// cin>>sq[i];
// for(j=0;j<4;j++)
// {
// if(sq[i][j]=='w')
// a[t+j+1]=0;
// else
// a[t+j+1]=1;
// }
// t+=4;
// }
// int tm=binary(a);
// /*for(i=1;i<=16;i++)
// cout<<a[i]<<' ';
// cout<<endl;
// cout<<binary(a)<<endl;*/
// /*cout<<binary(a)<<endl;*/
// if(tm==0||tm==65535)
// {
// cout<<0<<endl;
// return 0;
// }
// f[tm]=true;
// copy(a,queue[rear++]);
// while(front<rear)
// {
// int t=binary(queue[front]);
// copy(queue[front],b);
// for(i=1;i<=16;i++)
// {
// b[i]=1-queue[front][i];
// if(i>4)
// b[i-4]=1-queue[front][i-4];
// if(i<13)
// b[i+4]=1-queue[front][i+4];
// if(i%4!=1)
// b[i-1]=1-queue[front][i-1];
// if(i%4!=0)
// b[i+1]=1-queue[front][i+1];
// int temp=binary(b);
// if(temp==0||temp==65535)
// {
// cout<<step[t]+1<<endl;
// return 0;
// }
// if(!f[temp])
// {
// step[temp]=step[t]+1;
// f[temp]=true;
// copy(b,queue[rear++]);
// }
// }
// front++;
// }
// cout<<"Impossible"<<endl;
// for(i=0;i<65536;i++)
// delete queue[i];
// delete queue;
//} #include<iostream>
using namespace std;
int main()
{
int i,j,a=0,b,t=1,front=0,rear=1,step[65536]={0},queue[65536*2];
bool f[65536]={false};
char sq[5];
for(i=0;i<4;i++)
{
cin>>sq;
for(j=0;j<4;j++)
{
if(sq[j]=='b')
a+=t;
t<<=1;
}
}
if(a==0||a==65535)
{
cout<<0<<endl;
return 0;
}
queue[0]=a;
f[a]=true;
while(front<rear)
{
int t=queue[front];
for(i=0;i<16;i++)
{
b=queue[front];
b^=1<<i;
if(i+1>4)
b^=1<<(i-4);
if(i+1<13)
b^=1<<(i+4);
if((i+1)%4!=1)
b^=1<<(i-1);
if((i+1)%4!=0)
b^=1<<(i+1);
if(b==0||b==65535)
{
cout<<step[t]+1<<endl;
return 0;
}
if(!f[b])
{
step[b]=step[t]+1;
f[b]=true;
queue[rear++]=b;
}
}
front++;
}
cout<<"Impossible"<<endl;
}

HDOJ-三部曲-1001-Flip Game的更多相关文章

  1. HDOJ三部曲-DP-1017-pearls

    Pearls Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 20000/10000K (Java/Other) Total Submis ...

  2. HDOJ(1001) Sum Problem

    这一套题做错了几次,按理说直接用等差数列求和公式就行了,主要是要考虑一些运算符的结核性问题: 四则运算符(+.-.*./)和求余运算符(%)结合性都是从左到右. 于是,我自己写了一个版本,主要是考虑( ...

  3. poj1753 Flip Game

    题意:4*4的正方形,每个格子有黑白两面,翻转格子使得4*4个格子显示全黑或全白,翻转要求:选中的那个格子,以及其上下左右相邻的格子(如果存在)要同时翻转.输出最小的达到要求的翻转次数或者Imposs ...

  4. 杭电hdoj题目分类

    HDOJ 题目分类 //分类不是绝对的 //"*" 表示好题,需要多次回味 //"?"表示结论是正确的,但还停留在模块阶 段,需要理解,证明. //简单题看到就 ...

  5. 贪心 赛码 1001 Movie

    题目传送门 /* 贪心:官方题解: 首先我们考虑如何选择最左边的一个区间 假设最左边的区间标号是i, 那选择的另外两个区间的左端点必定要大于Ri 若存在i之外的j, 满足Rj<Ri, 那么另外两 ...

  6. uva10327 - Flip Sort

    Flip Sort Sorting in computer science is an important part. Almost every problem can be solved effec ...

  7. HDOJ 题目分类

    HDOJ 题目分类 /* * 一:简单题 */ 1000:    入门用:1001:    用高斯求和公式要防溢出1004:1012:1013:    对9取余好了1017:1021:1027:   ...

  8. 算法——A*——HDOJ:1813

    Escape from Tetris Time Limit: 12000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  9. HDOJ(HDU).1003 Max Sum (DP)

    HDOJ(HDU).1003 Max Sum (DP) 点我挑战题目 算法学习-–动态规划初探 题意分析 给出一段数字序列,求出最大连续子段和.典型的动态规划问题. 用数组a表示存储的数字序列,sum ...

  10. HDOJ.1009 FatMouse' Trade (贪心)

    FatMouse' Trade 点我挑战题目 题意分析 每组数据,给出有的猫粮m与房间数n,接着有n行,分别是这个房间存放的食物和所需要的猫粮.求这组数据能保证的最大的食物是多少? (可以不完全保证这 ...

随机推荐

  1. Scrum团队成立,阅读《构建之法》第6~7章,并参考以下链接,发布读后感、提出问题、并简要说明你对Scrum的理解

    Scrum团队成立:  团队名称:神的孩子 团队目标:短期目标,完成O2O模式的第一个平台 团队口号:我们都不是神的孩子 团队照: 角色分配 产品负责人: 许佳仪.决定开发内容和优先级排序,最大化产品 ...

  2. sleep函数

    Linux下: #include <unistd.h> sleep(1); // 睡眠1秒 usleep(1); // 睡眠1微妙

  3. PHP函数——is_subclass_of() 函数 判断一个对象是否为一个类的子类

    说明: bool is_subclass_of ( object $object , string $class_name ) 如果对象 object所属类是类 class_name的子类,则返回 T ...

  4. Linux Mysql 1130错误解决

      今天在win32下通过navicat 远程登录Mysql时出现如下错误:     想都不用想,肯定是Mysql的访问权限问题.   首先,通过终端(我用的是SSH)远程登录到Linux服务器,为了 ...

  5. Java中的String与常量池[转帖]

    string是java中的字符串.String类是不可变的,对String类的任何改变,都是返回一个新的String类对象.下面介绍java中的String与常量池. 1. 首先String不属于8种 ...

  6. HTML 中 META的作用

    说明: meta是用来在HTML文档中模拟HTTP协议的响应头报文.meta 标签用于网页的<head>与</head>中,meta 标签的用处很多.meta 的属性有两种:n ...

  7. 转载——PLSQL developer 连接不上64位Oracle 解决办法

    前两天刚下载了oracle 11g 64位的最新版本,安装成功之后,再安装PLSQL.结果使用PLSQL访问数据库时,死活连接不上.报错如下: Could not load "……\bin\ ...

  8. S1:动态方法调用:call & apply

    js中函数执行的两种方式:一是通过调用运算符’()’,二是通过调用call或apply来动态执行. 一.动态方法调用中指定this对象 开发中我们往往需要在对象B中调用对象A的方法,这个时候就用到了a ...

  9. S1 :闭包

    闭包是指有权访问另一个函数作用域中的变量的函数.创建闭包的常见方式,就是在一个函数内部创建另一个函数,以createComparisonFunction()函数为例 function createCo ...

  10. 一个QQ木马的逆向分析浅谈(附带源码)

    程序流程:首先注册自己程序的窗口以及类等一系列窗口操作,安装了一个定时器,间隔为100ms,功能搜索QQ的类名,如果找到就利用FindWindow("5B3838F5-0C81-46D9-A ...