Flip Game 分类: POJ 2015-06-15 14:59 22人阅读 评论(0) 收藏
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 33519 | Accepted: 14642 |
Description
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:
- 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
goal, then write the word "Impossible" (without quotes).
Sample Input
bwwb
bbwb
bwwb
bwww
Sample Output
4
/*
枚举第一行的情况(用二进制),进行反转,然后反转后面的行以保正前面的行符合要求
最后看最后一行是不是符合要求
*/
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <cctype>
#include <queue>
#include <stack>
#include <algorithm>
using namespace std;
int a[5][5],b[5][5];
int Arr[5];
char s[5][5]; void Trans()//转化成数字
{
for(int i=0; i<4; i++)
{
for(int j=0; j<4; j++)
{
a[i][j]=s[i][j]=='b'?1:0;
}
}
}
void tran(int n)//二进制转化
{
memset(Arr,0,sizeof(Arr));
int top=3;
while(n)
{
Arr[top--]=n&1;
n=n>>1;
}
}
void Creat()
{
for(int i=0; i<4; i++)
{
for(int j=0; j<4; j++)
{
b[i][j]=a[i][j];
}
}
}
void change(int i,int j)//反转
{
b[i][j]=b[i][j]==0?1:0;
if(j>0)
{
b[i][j-1]=b[i][j-1]==0?1:0;
}
if(j<3)
{
b[i][j+1]=b[i][j+1]==0?1:0;
}
if(i<3)
{
b[i+1][j]=b[i+1][j]==0?1:0;
}
if(i>0)
{
b[i-1][j]=b[i-1][j]==0?1:0;
}
}
int main()
{
int sum;
int Max=1000;
for(int i=0; i<4; i++)
{
scanf("%s",s[i]);
}
Trans();
for(int i=0; i<=15; i++)
{
tran(i);
for(int j=0; j<2; j++)
{
Creat();
sum=0;
for(int k=0; k<4; k++)
{
if(Arr[k])
{
change(0,k);
sum++;
}
}
for(int k=1; k<4; k++)
{
for(int kk=0; kk<4; kk++)
{
if(b[k-1][kk]!=j)
{
change(k,kk);
sum++;
}
}
}
bool flag=true;
for(int k=0; k<4; k++)
{
if(b[3][k]!=j)
{
flag=false;
break;
}
}
if(flag)
{
if(Max>sum)
{
Max=sum;
}
if(Max==0)
{
break;
}
}
else//如果不能反转成则就不可能反转成功
{
break;
}
}
}
if(Max!=1000)
{
printf("%d\n",Max);
}
else
{
printf("Impossible\n");
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
Flip Game 分类: POJ 2015-06-15 14:59 22人阅读 评论(0) 收藏的更多相关文章
- 【solr专题之一】Solr快速入门 分类: H4_SOLR/LUCENCE 2014-07-02 14:59 2403人阅读 评论(0) 收藏
一.Solr学习相关资料 1.官方材料 (1)快速入门:http://lucene.apache.org/solr/4_9_0/tutorial.html,以自带的example项目快速介绍发Solr ...
- A Plug for UNIX 分类: POJ 图论 函数 2015-08-10 14:18 2人阅读 评论(0) 收藏
A Plug for UNIX Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14786 Accepted: 4994 Desc ...
- iOS正则表达式 分类: ios技术 2015-07-14 14:00 35人阅读 评论(0) 收藏
一.什么是正则表达式 正则表达式,又称正规表示法,是对字符串操作的一种逻辑公式.正则表达式可以检测给定的字符串是否符合我们定义的逻辑,也可以从字符串中获取我们想要的特定部分.它可以迅速地用极简单的方式 ...
- iOS中UITextField 使用全面解析 分类: ios技术 2015-04-10 14:37 153人阅读 评论(0) 收藏
//初始化textfield并设置位置及大小 UITextField *text = [[UITextField alloc]initWithFrame:CGRectMake(20, 20, 13 ...
- Hdu 1009 FatMouse' Trade 分类: Translation Mode 2014-08-04 14:07 74人阅读 评论(0) 收藏
FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- printf "%.*s" 分类: 小细节 2015-07-04 14:36 2人阅读 评论(0) 收藏
ref : http://www.cnblogs.com/yuaqua/archive/2011/10/21/2219856.html 小数点.后"*"表示输出位数,具体的数据来自 ...
- 读取Webpage表中的内容 分类: H3_NUTCH 2015-02-10 14:59 418人阅读 评论(0) 收藏
nutch将从网页中抓取到的信息放入hbase数据库中,默认情况下表名为$crawlId_webpage,但表中的内容以16进制进行表示,直接scan或者通过Java API进行读取均只能读取到16进 ...
- IIS上虚拟站点的web.config与主站点的web.config冲突解决方法 分类: ASP.NET 2015-06-15 14:07 60人阅读 评论(0) 收藏
IIS上在主站点下搭建虚拟目录后,子站点中的<system.web>节点与主站点的<system.web>冲突解决方法: 在主站点的<system.web>上一级添 ...
- C++实现不能被继承的类——终结类 分类: C/C++ 2015-04-06 14:48 64人阅读 评论(0) 收藏
1. 问题 C++如何实现不能被继承的类,即终结类.Java中有final关键字修饰,C#中有sealed关键字修饰,而C++目前还没有类似的关键字来修饰类实现终结类,需编程人员手动实现. ...
随机推荐
- 数据库调优过程(一):SqlServer批量复制(bcp)[C#SqlBulkCopy]性能极低问题
背景 最近一段给xx做项目,这边最头疼的事情就是数据库入库瓶颈问题. 环境 服务器环境:虚拟机,分配32CPU,磁盘1.4T,4T,5T,6T几台服务器不等同(转速都是7200r),内存64G. 排查 ...
- PostgreSQL9.2.4内核源码结构介绍
PostgreSQL的源代码可以随意获得,其开源协议也允许研究者任意修改,这里介绍一下PostgreSQL的源码结构以及部分实现机制.下载PostgreSQL源代码并减压后,其一级目录结构如下图: P ...
- UVa 10088 - Trees on My Island (pick定理)
样例: 输入:123 16 39 28 49 69 98 96 55 84 43 51 3121000 10002000 10004000 20006000 10008000 30008000 800 ...
- 2-sat按照最小字典序输出可行解(hdu1814)
Peaceful Commission Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- csuoj 1022: 菜鸟和大牛
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1022 1022: 菜鸟和大牛 Time Limit: 1 Sec Memory Limit: 1 ...
- 每天一个shell知识--数组
1.shell中数组的定义: 数组名=(value value1 value2 ) 也可以单独的设定数组的分量: arrayL[0]=value arrayL[1]=value1 2.${arrayL ...
- java中时间的比较
进入要比较从库中取出的时间,期初使用比较date.getTime()的值,但是当时间的年月日都相同时,时分秒较早的getTime()值比时分秒较晚的getTime()的值要大,至今笔者还不知这是为什么 ...
- CSS_03_03_ul图片替换
ul图片替换 第01步:编写css样式:url.css @charset "utf-8"; /*ul用图片替换*/ ul.u_01{/*图片*/ list-style:circle ...
- 夺命雷公狗---node.js---15之加密
node其实也给我们留下了密码的加密发送,不过一般都是用cmd5加密其实也是够了,但是sha1加密也要提下: /** * Created by leigood on 2016/8/31. */ var ...
- 《REWORK》启示录 发出你的心声——程序员与身体
Sound Like You 所谓的标题在这里并不是为了吸引眼球,不过也是为了吸引眼球,只是出发点已经不一样了.这是一篇适合给程序员看的关于健康的文章,也许你认识李开复也可以给他看看,上过养生过,觉得 ...