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++目前还没有类似的关键字来修饰类实现终结类,需编程人员手动实现. ...
随机推荐
- weka特征选择(IG、chi-square)
一.说明 IG是information gain 的缩写,中文名称是信息增益,是选择特征的一个很有效的方法(特别是在使用svm分类时).这里不做详细介绍,有兴趣的可以googling一下. chi-s ...
- c++ template怎么使用及注意事项
c++ 中的template和c#的什么有点相似? 先看下定义方式: template <typename|class T> T myFunction(T param1,T param2. ...
- Android -- 自定义View小Demo(一)
1,现在要实现下图的简单效果,很简单 ,就是使用paint在canvas上绘制5中不同颜色的圆圈,效果图如下: 这是绘制基本图形一种最简单的方法,下面是它的代码 ,注释写的很详细,也就不去讲解了 M ...
- Android -- 初探MVP模式
1,相信大家对mvp模式都很熟悉了,M-Model-模型.V-View-视图.C-Controller-控制器.MVP作为MVC的版本演化,与MVC的意义类似:M-Model-模型.V-View-视图 ...
- Fresco源码解析 - DataSource怎样存储数据
Fresco源码解析 - DataSource怎样存储数据 datasource是一个独立的 package,与FB导入的guava包都在同一个工程内 - fbcore. datasource的类关系 ...
- 杭电oj 1016 Prime Ring Problem
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- Install the 64bit library in Ubuntu13.10
After installed Ubuntu13.10, and i want to run a 32bit software, in the pass, you just run sudo apt- ...
- Server2003系统上的内置服务器设置某类IP无法访问问题
最近测试过程中遇到了一个很奇怪的现象,把服务器(测试产品)部署在Server2003系统的外网A上,把客户端(测试产品)部署在内网B,网络A,B用路由器相连,设置网络A为200.1.1.255,发现客 ...
- C++笔试题(部分)
1.简述C++11和Boost 2.struct和union与class的区别 3.为什么C++中调用被C编译器编译后的函数要加extern C声明? 4.以下代码哪里不对? #pragma regi ...
- scala特质
package com.ming.test /** * scala 特质,类似与java接口,但是比java接口强大,可以有实现方法,定义字段之类的 */ /** * 定义一个日志的特质 */ tra ...