HDU 5983 Pocket Cube 模拟

HDU 5983 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) where h, k, l ∈ {0, 1}. Each of the six faces owns four small faces filled with a positive integer.
For each step, you can choose a certain face and turn the face ninety degrees clockwise or counterclockwise.
You should judge that if one can restore the pocket cube in one step. We say a pocket cube has been restored if each face owns four same integers.

Input

The first line of input contains one integer N(N ≤ 30) which is the number of test cases.
For each test case, the first line describes the top face of the pocket cube, which is the common 2 × 2 face of pieces
labelled by (0, 0, 1),(0, 1, 1),(1, 0, 1),(1, 1, 1). Four integers are given corresponding to the above pieces.
The second line describes the front face, the common face of (1, 0, 1),(1, 1, 1),(1, 0, 0),(1, 1, 0). Four integers are
given corresponding to the above pieces.
The third line describes the bottom face, the common face of (1, 0, 0),(1, 1, 0),(0, 0, 0),(0, 1, 0). Four integers are
given corresponding to the above pieces.
The fourth line describes the back face, the common face of (0, 0, 0),(0, 1, 0),(0, 0, 1),(0, 1, 1). Four integers are
given corresponding to the above pieces.
The fifth line describes the left face, the common face of (0, 0, 0),(0, 0, 1),(1, 0, 0),(1, 0, 1). Four integers are given
corresponding to the above pieces.
The six line describes the right face, the common face of (0, 1, 1),(0, 1, 0),(1, 1, 1),(1, 1, 0). Four integers are given
corresponding to the above pieces.
In other words, each test case contains 24 integers a, b, c to x. You can flat the surface to get the surface development
as follows.

+ - + - + - + - + - + - +
| q | r | a | b | u | v |
+ - + - + - + - + - + - +
| s | t | c | d | w | x |
+ - + - + - + - + - + - +
        | e | f |
        + - + - +
        | g | h |
        + - + - +
        | i | j |
        + - + - +
        | k | l |
        + - + - +
        | m | n |
        + - + - +
        | o | p |
        + - + - +

Output

For each test case, output YES if can be restored in one step, otherwise output NO.

Sample Input

4
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
5 5 5 5
6 6 6 6
6 6 6 6
1 1 1 1
2 2 2 2
3 3 3 3
5 5 5 5
4 4 4 4
1 4 1 4
2 1 2 1
3 2 3 2
4 3 4 3
5 5 5 5
6 6 6 6
1 3 1 3
2 4 2 4
3 1 3 1
4 2 4 2
5 5 5 5
6 6 6 6

Sample Output

YES
YES
YES
NO

Source

2016ACM/ICPC亚洲区青岛站-重现赛(感谢中国石油大学)

Recommend

jiangzijing2015

题意

给一个二阶魔方(如图所示)要求旋转一次之内(转一次或一次不转), 问是否能将魔方复原

分析

拿个魔方在纸上模拟一下, 一共有6种旋转方式, 可以在按照上图给出的abcd…画一个小矩形, 将矩形的每一个面分离开, 可以比较直观地看出魔方是如何旋转的, 以及旋转后的情况. 将其对应的代码写出即可.

代码

#include<stdio.h>

int top[5], front[5], bottom[5], back[5], left[5], right[5];

int check()
{
	int flag = 1, i;
	for( i=1; i<4; i++)
	{
		if( top[i] != top[i+1])
		{
			flag = 0;
			break;
		}
		if( front[i] != front[i+1])
		{
			flag = 0;
			break;
		}
		if( bottom[i] != bottom[i+1])
		{
			flag = 0;
			break;
		}
		if( back[i] != back[i+1])
		{
			flag = 0;
			break;
		}
		if( left[i] != left[i+1])
		{
			flag = 0;
			break;
		}
		if( right[i] != right [i+1])
		{
			flag = 0;
			break;
		}
	}
	return flag;
}

void ope_1()
{
	int t, u;
	t = top[2];
	u = top[4];
	
	top[2] = front[2];
	top[4] = front[4];
	front[2] = bottom[2];
	front[4] = bottom[4];
	bottom[2] = back[2];
	bottom[4] = back[4];
	back[2] = t;
	back[4] = u;	
}

void ope_2()
{
	int t, u;
	t = top[2];
	u = top[4];
	
	top[2] = back[2];
	top[4] = back[4];
	back[2] = bottom[2];
	back[4] = bottom[4];
	bottom[2] = front[2];
	bottom[4] = front[4];
	front[2] = t;
	front[4] = u;	
}

void ope_3()
{
	int t, u;
	t = top[3];
	u = top[4];
	
	top[3] = left[3];
	top[4] = left[4];
	left[3] = bottom[2];
	left[4] = bottom[1];
	bottom[1] = right[4];
	bottom[2] = right[3];
	right[3] = t;
	right[4] = u;	
}

void ope_4()
{
	int t, u;
	t = top[3];
	u = top[4];
	
	top[3] = right[3];
	top[4] = right[4];
	right[3] = bottom[2];
	right[4] = bottom[1];
	bottom[2] = left[3];
	bottom[1] = left[4];
	left[3] = t;
	left[4] = u;	
}

void ope_5()
{
	int t, u;
	t = front[1];
	u = front[2];
	
	front[1] = right[3];
	front[2] = right[1];
	right[3] = back[4];
	right[1] = back[3];
	back[4] = left[2];
	back[3] = left[4];
	left[2] = t;
	left[4] = u;	
}

void ope_6()
{
	int t, u;
	t = front[1];
	u = front[2];
	
	front[1] = left[2];
	front[2] = left[4];
	left[2] = back[4];
	left[4] = back[3];
	back[4] = right[3];
	back[3] = right[1];
	right[3] = t;
	right[1] = u;	
}

int main(void)
{	
	int n;
	scanf("%d", &n);
	while(n--)
	{
		int i;
		for( i=1; i<=4; i++)	scanf("%d", &top[i]);
		for( i=1; i<=4; i++)	scanf("%d", &front[i]);
		for( i=1; i<=4; i++)	scanf("%d", &bottom[i]);
		for( i=1; i<=4; i++)	scanf("%d", &back[i]);
		for( i=1; i<=4; i++)	scanf("%d", &left[i]);
		for( i=1; i<=4; i++)	scanf("%d", &right[i]);
		
		if( check()==1)
		{
			printf("YES\n");
			continue;
		}
		
		ope_1();
		if( check()==1)
		{
			printf("YES\n");
			continue;
		}
		ope_2();
		
		ope_2();
		if( check()==1)
		{
			printf("YES\n");
			continue;
		}
		ope_1();
		
		ope_3();
		if( check()==1)
		{
			printf("YES\n");
			continue;
		}
		ope_4();
		
		ope_4();
		if( check()==1)
		{
			printf("YES\n");
			continue;
		}
		ope_3();
		
		ope_5();
		if( check()==1)
		{
			printf("YES\n");
			continue;
		}
		ope_6();
		
		ope_6();
		if( check()==1)
		{
			printf("YES\n");
			continue;
		}
		ope_5();
		
		printf("NO\n");
	}
	
	return 0;
}

收获与反思

  1. 第一次知道这也算是个算法, 叫"模拟"算法, 即按照题目要求, 把汉字翻译成计算机语言即可, 难度不大但需要把步骤考虑清晰
  2. 代码中12, 34, 56为逆操作, 同时check()检查是否已经复原的函数写的可能较为繁琐, 日后有机会可以考虑用其他方法简化程序 (不过看来这种一步一步的操作占用时间不是很大, 并不会超时)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值