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++目前还没有类似的关键字来修饰类实现终结类,需编程人员手动实现. ...
随机推荐
- 判断UpLoader是否安装了Flash
var flashVersion = (function() { var version; try { version ...
- JSP 使用框架frame
JSP使用框架放在<head>标签里面.如果放在<body>标签里面,用Tomcat打开,是不会显示的.
- android设置系统模式
android 静音与振动1,设置静音和振动静音和振动都属于来电后的动作.所以在设置静音和振动时都只是设置一些标识,并往数据库写入相应标识. 文件:packages/apps/settings/src ...
- Spring day01
1 实例化Spring容器 新建springday01项目1.F盘jar/Spring/first/五个jar包拷贝到lib下,复制xml文件到项目first包下2.First.java测试如何启动容 ...
- Open quote is expected for attribute "property" associated with an element type "result".错误
java Mybatis 框架下的项目 报 Open quote is expected for attribute "property" associated with a ...
- BETWEEN and
select * from ( select *,ROW_NUMBER() over (ORDER BY AddTime desc) RowNumber from Product where ID n ...
- 夺命雷公狗---node.js---19之项目的构建在node+express+mongo的博客项目4mongodb在项目中的基本引入
首先我们在命令行下先建立这个库: 然后我们在项目中引入mongodb的模块: var MongoClient = require('mongodb').MongoClient; var DB_STR ...
- 将服务费用DIY到底----走出软件作坊:三五个人十来条枪 如何成为开发正规军(十)[转]
前一段时间,讲了一系列开发经理.实施经理.服务经理的工具箱:开发经理的工具箱---走出软件作坊:三五个人十来条枪 如何成为开发正规军(三) ,实施经理的工具箱--走出软件作坊:三五个人十来条枪 如何成 ...
- andriod之应用内置浏览器 webview
参考:http://my.eoe.cn/694183/archive/10476.html http://blog.csdn.net/it_ladeng/article/details/8136534 ...
- PHPCMS V9 环境搭建
PHPCMS V9的学习总结分为以下几点: [1]PHPCMS 简介 PHP原始为Personal Home Page的缩写,(外文名:PHP: Hypertext Preprocessor,中文名: ...