HDOJ-三部曲-1001-Flip Game
Flip Game
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 44 Accepted Submission(s) : 17
- 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.
bbwb
bwwb
bwww
//#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的更多相关文章
- HDOJ三部曲-DP-1017-pearls
Pearls Time Limit : 2000/1000ms (Java/Other) Memory Limit : 20000/10000K (Java/Other) Total Submis ...
- HDOJ(1001) Sum Problem
这一套题做错了几次,按理说直接用等差数列求和公式就行了,主要是要考虑一些运算符的结核性问题: 四则运算符(+.-.*./)和求余运算符(%)结合性都是从左到右. 于是,我自己写了一个版本,主要是考虑( ...
- poj1753 Flip Game
题意:4*4的正方形,每个格子有黑白两面,翻转格子使得4*4个格子显示全黑或全白,翻转要求:选中的那个格子,以及其上下左右相邻的格子(如果存在)要同时翻转.输出最小的达到要求的翻转次数或者Imposs ...
- 杭电hdoj题目分类
HDOJ 题目分类 //分类不是绝对的 //"*" 表示好题,需要多次回味 //"?"表示结论是正确的,但还停留在模块阶 段,需要理解,证明. //简单题看到就 ...
- 贪心 赛码 1001 Movie
题目传送门 /* 贪心:官方题解: 首先我们考虑如何选择最左边的一个区间 假设最左边的区间标号是i, 那选择的另外两个区间的左端点必定要大于Ri 若存在i之外的j, 满足Rj<Ri, 那么另外两 ...
- uva10327 - Flip Sort
Flip Sort Sorting in computer science is an important part. Almost every problem can be solved effec ...
- HDOJ 题目分类
HDOJ 题目分类 /* * 一:简单题 */ 1000: 入门用:1001: 用高斯求和公式要防溢出1004:1012:1013: 对9取余好了1017:1021:1027: ...
- 算法——A*——HDOJ:1813
Escape from Tetris Time Limit: 12000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- HDOJ(HDU).1003 Max Sum (DP)
HDOJ(HDU).1003 Max Sum (DP) 点我挑战题目 算法学习-–动态规划初探 题意分析 给出一段数字序列,求出最大连续子段和.典型的动态规划问题. 用数组a表示存储的数字序列,sum ...
- HDOJ.1009 FatMouse' Trade (贪心)
FatMouse' Trade 点我挑战题目 题意分析 每组数据,给出有的猫粮m与房间数n,接着有n行,分别是这个房间存放的食物和所需要的猫粮.求这组数据能保证的最大的食物是多少? (可以不完全保证这 ...
随机推荐
- Java 之 I/O 系列 01 ——基础
Java 之 I/O 系列 目录 Java 之 I/O 系列 01 ——基础 Java 之 I/O 系列 02 ——序列化(一) Java 之 I/O 系列 02 ——序列化(二) 整理<疯狂j ...
- mongodb的安装及注意事项
环境:win8 32bit 1:安装 去mongodb的官网http://www.mongodb.org/downloads下载32bit的包 解压后会出现以下文件 在安装的盘C:下建立mongodb ...
- 关于for循环条件性能问题
昨天看一博客写到一条 尽量使用 for(int i=0,ct=list.Count();i<ct;i++){} 这样的格式,因为我平时一般都是用for(int i=0;i<list.Cou ...
- 《Pro AngularJS》学习小结-01
<Pro AngularJS>该书以一个SportsStore案例为主线铺开. 一.开发环境设置 该书中所用的数据库data server开发环境是Deployed,从来没听说过,而且作者 ...
- C#基础学习文章导航
第一部分:入个门 C#入门篇-1:HelloWorld的类 C#入门篇-2:什么是变量 C#入门篇-3:数据类型及转换 C#入门篇-4:使用运算符 第二部分:流程控制语句 C#入门篇5-1:流程控制语 ...
- 学习记录012-NFS
1.Network file System 主要是通过网络让不同的主机进行通信,构建于ip协议之上的现代文件系统,用来存储共享视频,图片,文件等 2.并发大的时候会有点问题(维护不好会丢数据) 3.N ...
- MySQL数据库百万级高并发网站实战
在一开始接触PHP接触MYSQL的时候就听不少人说:“MySQL就跑跑一天几十万IP的小站还可以,要是几百万IP就不行了”,原话不记得了,大体 就是这个意思.一直也没有好的机会去验证这个说法,一是从没 ...
- 使用反射来编写实体类的XML
前言: 开发过程中经常需要返回某实体类的列表,公司通常用的都是XML格式的接口,小猪借鉴了公司前辈留下的代码一直是类似这么写的: public static string GetXMLList(ILi ...
- linux常用的重要的命令: netstat
Netstat 命令用于显示各种网络相关信息,如网络连接,路由表,接口状态 (Interface Statistics),masquerade 连接,多播成员 (Multicast Membershi ...
- InLineHookSSDT
//当Ring3调用OpenProcess //1从自己的模块(.exe)的导入表中取值 //2Ntdll.dll模块的导出表中执行ZwOpenProcess(取索引 进入Ring0层) //3进入R ...