HDU 4801 Pocket Cube(模拟题——转魔方)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4801

题面:

Pocket Cube

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 882    Accepted Submission(s): 271

Problem Description

Pocket Cube is a 3-D combination puzzle. It is a 2 × 2 × 2 cube, which means it is constructed by 8 mini-cubes. For a combination of 2 × 2 mini-cubes which sharing a whole cube face, you can twist it 90 degrees in clockwise or counterclockwise
direction, this twist operation is called one twist step.

Considering all faces of mini-cubes, there will be totally 24 faces painted in 6 different colors (Indexed from 0), and there will be exactly 4 faces painted in each kind of color. If 4 mini-cubes‘ faces of same color rely on same large cube face, we can call
the large cube face as a completed face.

Now giving you an color arrangement of all 24 faces from a scrambled Pocket Cube, please tell us the maximum possible number of completed faces in no more than N twist steps.

Index of each face is shown as below:

Input

There will be several test cases. In each test case, there will be 2 lines. One integer N (1 ≤ N ≤ 7) in the first line, then 24 integers Ci separated by a single space in the second line. For index 0 ≤ i < 24, Ci is color of the
corresponding face. We guarantee that the color arrangement is a valid state which can be achieved by doing a finite number of twist steps from an initial cube whose all 6 large cube faces are completed faces.

Output

For each test case, please output the maximum number of completed faces during no more than N twist step(s).

Sample Input

1
0 0 0 0 1 1 2 2 3 3 1 1 2 2 3 3 4 4 4 4 5 5 5 5
1
0 4 0 4 1 1 2 5 3 3 1 1 2 5 3 3 4 0 4 0 5 2 5 2

Sample Output

6
2

Source

2013 Asia Changsha Regional Contest

解题:

转魔方,注意对应位置别弄错就好,深搜,广搜都可以。就是比较难调。

总结:

一些重复性代码,最好写成函数调用,减少代码的冗余性,同时提高代码的准确性。

代码:

#include <iostream>
#include <cstdio>
using namespace std;
int cube[24],tmp,a,b,c,ans,n;
int check()
{
	int res=0;
	tmp=cube[0];
	if(tmp==cube[1]&&tmp==cube[2]&&tmp==cube[3])res++;
	tmp=cube[4];
	if(tmp==cube[5]&&tmp==cube[10]&&tmp==cube[11])res++;
	tmp=cube[6];
	if(tmp==cube[7]&&tmp==cube[12]&&tmp==cube[13])res++;
    tmp=cube[8];
	if(tmp==cube[9]&&tmp==cube[14]&&tmp==cube[15])res++;
    tmp=cube[16];
	if(tmp==cube[17]&&tmp==cube[18]&&tmp==cube[19])res++;
    tmp=cube[20];
	if(tmp==cube[21]&&tmp==cube[22]&&tmp==cube[23])res++;
    return res;
}
void rotate(int oper)
{
    if(oper==1)
	{
		a=cube[1];
		b=cube[3];
		c=cube[9];
		cube[1]=cube[7];
		cube[3]=cube[13];
		cube[7]=cube[17];
		cube[13]=cube[19];
		cube[17]=cube[21];
		cube[19]=cube[23];
		cube[21]=a;
		cube[23]=b;
		cube[9]=cube[8];
		cube[8]=cube[14];
        cube[14]=cube[15];
		cube[15]=c;
	}
	else if(oper==6)
    {
		a=cube[7];
		b=cube[13];
		c=cube[14];
		cube[7]=cube[1];
		cube[13]=cube[3];
		cube[1]=cube[21];
		cube[3]=cube[23];
		cube[21]=cube[17];
		cube[23]=cube[19];
		cube[17]=a;
		cube[19]=b;
		cube[14]=cube[8];
		cube[8]=cube[9];
        cube[9]=cube[15];
		cube[15]=c;
	}
    else if(oper==2)
    {
		a=cube[8];
		b=cube[14];
		c=cube[7];
		cube[8]=cube[17];
		cube[14]=cube[16];
		cube[17]=cube[11];
		cube[16]=cube[5];
		cube[11]=cube[2];
		cube[5]=cube[3];
		cube[2]=a;
		cube[3]=b;
		cube[7]=cube[13];
		cube[13]=cube[12];
        cube[12]=cube[6];
		cube[6]=c;
	}
    else if(oper==5)
    {
		a=cube[16];
		b=cube[17];
		c=cube[12];
		cube[17]=cube[8];
		cube[16]=cube[14];
		cube[8]=cube[2];
		cube[14]=cube[3];
		cube[2]=cube[11];
		cube[3]=cube[5];
		cube[11]=b;
		cube[5]=a;
		cube[12]=cube[13];
		cube[13]=cube[7];
        cube[7]=cube[6];
		cube[6]=c;
	}
    else if(oper==3)
    {
		a=cube[12];
		b=cube[13];
		c=cube[16];
		cube[12]=cube[14];
		cube[13]=cube[15];
		cube[14]=cube[21];
		cube[15]=cube[20];
		cube[21]=cube[10];
		cube[20]=cube[11];
		cube[10]=a;
		cube[11]=b;
		cube[16]=cube[17];
		cube[17]=cube[19];
        cube[19]=cube[18];
		cube[18]=c;
	}
	else
    {
		a=cube[12];
		b=cube[13];
		c=cube[16];
		cube[12]=cube[10];
		cube[13]=cube[11];
		cube[10]=cube[21];
		cube[11]=cube[20];
		cube[20]=cube[15];
		cube[21]=cube[14];
		cube[14]=a;
		cube[15]=b;
		cube[16]=cube[18];
		cube[18]=cube[19];
        cube[19]=cube[17];
		cube[17]=c;
	}
}
void search(int oper,int step)
{
	rotate(oper);
	tmp=check();
	if(tmp>ans)ans=tmp;
    if(step==n)
	{
		rotate(7-oper);
		return;
	}
	for(int i=1;i<=6;i++)
	{
		if(i!=(7-oper))
		{
			search(i,step+1);
		}
	}
    rotate(7-oper);
}
int main()
{
	while(~scanf("%d",&n))
	{
		for(int i=0;i<24;i++)
			scanf("%d",&cube[i]);
        ans=check();
		for(int i=1;i<=6;i++)
			search(i,1);
		printf("%d\n",ans);
	}
	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-29 10:48:13

HDU 4801 Pocket Cube(模拟题——转魔方)的相关文章

HDU 4801 Pocket Cube BFS

链接:http://vjudge.net/contest/view.action?cid=51289#problem/K 题意:给一个小魔方(每面只有四个小块),每个小块有自己的颜色(一共六种颜色,每种颜色四块),问经过最多N次操作(N<=7),每次操作是将魔方某一面转动90度,最多能使多少个面的四个小块颜色都相同. 思路:每次操作产生6种状态,(向前,向后,向左,向右,向上,向下,左半魔方向前和右半魔方向后的操作产生的状态本质是一样的)直接BFS搜索,可能产生的状态一共有6^7种,中间比较麻烦

HDU 4801 Pocket Cube

题目链接 去年现场,虎哥1Y的,现在刷刷题,找找状态... 一共6种转法,把3个面放到顶部,左旋和右旋,感觉写的还不错....都写成常数了. #include <stdio.h> #include <math.h> #include <string.h> #include <queue> #include <algorithm> #define LL long long using namespace std; struct node { int

HDU 4022 Bombing STL 模拟题

手动模拟.. #include<stdio.h> #include<iostream> #include<algorithm> #include<vector> #include<cmath> #include<queue> #include<set> #include<map> using namespace std; #define N 10100 #define inf 1000000010 map<

HDU 5983 Pocket Cube

Pocket Cube Problem Description The Pocket Cube, also known as the Mini Cube or the Ice Cube, is the 2 × 2 × 2 equivalence of a Rubik's Cube.The cube consists of 8 pieces, all corners.Each piece is labeled by a three dimensional coordinate (h, k, l)

2016ACM/ICPC亚洲区青岛站 B Hdu-5983 Pocket Cube 模拟

题面 题意:给你一个2*2的魔方,给你每个面每个小块的颜色,一共24个,然后问你能否在一步之内还原. 题解:手动在纸上画,推出每种变化对应的置换,显然,一共有6种,而且可以当成3种,(具体哪3种,就是绕x,y,z轴转一次的),另外一个方向,就是转三次就行了 而且你也不需要考虑什么上面转了下面转,相对关系是一样的 写的时候犯了个错,手写的u,v,r分不清楚了..... 转一次会有12个小面发生变化,写的时候可以只写8个面,因为有一个面的位置变了, 但是我们只问一步之内能不能还原,那一面的都没有到其

HDU 4028 The time of a day STL 模拟题

暴力出奇迹.. #include<stdio.h> #include<iostream> #include<algorithm> #include<vector> #include<cmath> #include<queue> #include<set> #include<map> using namespace std; #define ll __int64 #define N 42 ll n,m,ans;

hdu 5641 King&#39;s Phone(暴力模拟题)

Problem Description In a military parade, the King sees lots of new things, including an Andriod Phone. He becomes interested in the pattern lock screen. The pattern interface is a 3×3 square lattice, the three points in the first line are labeled as

HDU 4930 Fighting the Landlords(扯淡模拟题)

Fighting the Landlords 大意: 斗地主....   分别给出两把手牌,肯定都合法.每张牌大小顺序是Y (i.e. colored Joker) > X (i.e. Black & White Joker) > 2 > A (Ace) > K (King) > Q (Queen) > J (Jack) > T (10) > 9 > 8 > 7 > 6 > 5 > 4 > 3. 给你8种组合:1.

HDU 4925 Apple Tree(模拟题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4925 解题报告:给你n*m的土地,现在对每一块土地有两种操作,最多只能在每块土地上进行两种操作,第一种是种苹果树操作,第二种是施肥操作,种苹果树操作可以使得该块地 长出一个苹果,施肥操作可以使得与这块土地相邻的土地的苹果产量变为原来的两倍,问可以得到的最多的苹果数量是多少? 例如一个4*4的土地,用1表示在该土地上做第一种操作,0表示在该土地上做第二种操作,可以得到最多苹果的操作如下: 0 1 0