ZOJ 2477 Magic Cube(魔方)
ZOJ 2477 Magic Cube(魔方)
Time Limit: 2 Seconds Memory Limit: 65536 KB
This is a very popular game for children. In this game, there's a cube, which consists of 3 * 3 * 3 small cubes. We can unwrap the cube, it will become like this:
这是个有名的儿童游戏。游戏中有个方块,由3**3的小方块组成。我们可以把方块展开如下:
CN
w w w
w w w
w w w
r r r g g g b b b o o o
r r r g g g b b b o o o
r r r g g g b b b o o o
y y y
y y y
y y y
The letters means the color on the small cubes. For example, 'r' means red, 'g' means green, 'y' means yellow....The goal for this game is to rotate the faces of the cube to make each of the faces contains only one color. Note there're exact 6 kind of colors on the cube and there're exact 9 small rectangles totally in any time in the game.
Do you know how to rotate the faces? I think most of you have known it. But I would like to show it again. When a face is rotated, the configuration of colors in all the adjacent faces changes. For the cube above, after we rotate the green face clock-wise, the last line of 'w' face will become the left column of 'b' face, the left column of 'b' face will become the top line of 'y' face, etc. As you may know, reaching the final position from a scrambled configuration can be quite challenging.
In this problem, you are given a configuration of the cube, and asked to give a way to reach the final position. To reduce the difficulty, the steps required will never be greater than 5.
这些小写字母表示小方块的颜色。比如,’r’为红,’g’为绿,’y’为黄...游戏目标为通过旋转某面方块使得每面只有一种颜色。注意,方块上仅有6种颜色,并且在游戏的任意时刻都有9个准确的矩形。 你知道如何旋转否?我想大部分人都懂,然而还是要讲一讲。某面旋转后,将改变所有相邻面的颜色。如上述立方体,顺时针旋转绿色面后,’w’面的最后一行将变成’b’面的左列,’b’面的左列将变成’y’面的顶行,以此类推。你或许知道从乱序变为最终状态是相当有挑战性的。 此问题中,给定一个方块,要求寻找一种到达最终状态的方法。为了降低难度,必须步骤数不超过5。
CN
Input - 输入
The input contains an integer in the first line, which indicates the number of the test cases. In each test case, there're exact 10 lines. The first line is an empty line. The next 9 lines contain a configuration. The format can be seen in the sample input. For simplicity, we give an index to each face as follows:
输入的第一行为一个整数,表示测试用例的数量。每个测试用例10行。
第一行为空行。随后9行为魔方配置。格式参照输入样例。简单起见,每面索引如下:
CN
/---\
| |
| 4 |
| |
/---+---+---+---\
| | | | |
| 0 | 1 | 2 | 3 |
| | | | |
\---+---+---+---/
| |
| 5 |
| |
\---/
Note that there's a space between two adjacent letters.
注意两个相邻小写字母间有一个空格。
CN
Output - 输出
For each test case, the first line of the output is the smallest count N of the steps to reach the winning position. If the winning position can't be reached in 5 steps, print -1 in this line.
Otherwise print each step in one line in the following N lines. A step contains two integers, the first one means the face index, and the second one means the direction. 1 means clock-wise and -1 means counter clock-wise.
If the given position is the winning position, print 0 for such test case simply. If there're multiple solutions, any one is acceptable.
对于每个测试用例,第一行输出到达成功位置的最小步骤数N。若5步内无法到达则输出-。
随后N行输出每个具体步骤。每个步骤包含两个整数,第一个表示面索引,第二个表示方向。1表示顺时针,且-1表示逆时针。
如果给定配置则为成功位置,输出0。若存多解,择一即可。
CN
Sample Input - 输入样例
2
w w w
w w w
w w w
r r r g g g b b b o o o
r r r g g g b b b o o o
r r r g g g b b b o o o
y y y
y y y
y y y w w w
w w w
b b b
r r w g g g y b b o o o
r r w g g g y b b o o o
r r w g g g y b b o o o
r r r
y y y
y y y
Sample Output - 输出样例
0
1
1 1
题解
IDA*,据说暴力也是可以的。
水平太渣没想到什么高明的代价估计方法,取每面不符合的颜色种类数为旋转代价,上限为3。
代码 C++
#include <cstdio>
#include <cstring>
#include <algorithm>
#define MX 54
int maxDeep, n, mp[], data[MX], opt[][],
chgs[][] = {
, , , , , , , , ,
, , , , , , , , ,
, , , , , , , , ,
, , , , , , , , ,
, , , , , , , , ,
, , , , , , , ,
}, chge[][] = {
, , , , , , , , , , , ,
, , , , , , , , , , , ,
, , , , , , , , , , , ,
, , , , , , , , , , , ,
, , , , , , , , , , , ,
, , , , , , , , , , ,
};
int vle() {
int rtn = , i, j, tmp[], siz;
for (i = ; i < ; ++i) {
memset(tmp, , sizeof tmp); siz = -;
for (j = ; j < ; ++j) ++tmp[data[chgs[i][j]]];
for (j = ; j < ; ++j) if (tmp[j]) ++siz;
rtn = std::max(rtn, siz> ? : siz);
}
return rtn;
}
void chg(int n, int drc) {
int i, tmpe[], tmps[];
if (~drc) {
memcpy(tmpe + , chge[n], sizeof chge[n]); memcpy(tmps + , chgs[n], sizeof chgs[n]);
memcpy(tmpe, tmpe + , sizeof(int)* ); memcpy(tmps, tmps + , sizeof(int)* );
}
else {
memcpy(tmpe, &chge[n][], sizeof(int)* ); memcpy(tmps, &chgs[n][], sizeof(int)* );
memcpy(tmpe + , chge[n], sizeof(int)* ); memcpy(tmps + , chgs[n], sizeof(int)* );
}
for (i = ; i < ; ++i) tmpe[i] = data[tmpe[i]];
for (i = ; i < ; ++i) data[chge[n][i]] = tmpe[i];
for (i = ; i < ; ++i) tmps[i] = data[tmps[i]];
for (i = ; i < ; ++i) data[chgs[n][i]] = tmps[i];
} bool DFS(int deep) {
int i = vle();
if (i + deep>maxDeep) return ;
if (!i) return ;
for (i = ; i < ; ++i) {
chg(opt[deep][] = i, opt[deep][] = );
if (DFS(deep + )) return ;
chg(i, -);
chg(opt[deep][] = i, opt[deep][] = -);
if (DFS(deep + )) return ;
chg(i, );
}
return ;
} int main() {
mp['w'] = ; mp['r'] = ; mp['g'] = ; mp['b'] = ; mp['o'] = ; mp['y'] = ;
int t, i;
char c;
scanf("%d", &t);
while (t--) {
for (i = ; i < MX; ++i) {
scanf(" %c", &c); data[i] = mp[c];
}
for (maxDeep = vle(); maxDeep < && !DFS(); ++maxDeep);
if (maxDeep < ) {
printf("%d\n", maxDeep);
for (i = ; i < maxDeep; ++i) printf("%d %d\n", opt[i][], opt[i][]);
}
else puts("-1");
}
return ;
}
ZOJ 2477 Magic Cube(魔方)的更多相关文章
- ZOJ 2477 Magic Cube 暴力,模拟 难度:0
		
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1477 用IDA*可能更好,但是既然时间宽裕数据简单,而且记录状态很麻烦,就直接 ...
 - ZOJ - 2477 dfs [kuangbin带你飞]专题二
		
注意输入的处理,旋转操作打表.递增枚举可能步数,作为限制方便找到最短路. AC代码:90ms #include<cstdio> #include<cstring> char m ...
 - ZOJ 3622 Magic Number 打表找规律
		
A - Magic Number Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu Subm ...
 - LA 6621 /ZOJ 3736 Pocket Cube 打表+暴力
		
这道题是长沙区域赛的一道简单题,当时题目在ZOJ重现的时候就做了一次,但是做的好复杂,用的BFS暴力,而且还没打表,最后还是莫名其妙的爆栈错误,所以就一直没弄出来,昨天做到大白书上例题05年东京区域赛 ...
 - 模拟 ZOJ 3736 Pocket Cube
		
题目传送门 题意:魔方最多旋转n次,问最多能使多少面颜色相同 分析:6种状态(3种旋转方式*顺逆方向,其他对称的!),首先先打个表,然后很愉快的DFS.自己写的时候费劲脑汁,代码很长,还TLE了... ...
 - [ZOJ 3622] Magic Number
		
Magic Number Time Limit: 2 Seconds Memory Limit: 32768 KB A positive number y is called magic n ...
 - ZOJ 3622 Magic Number(数)
		
题意 假设一个正整数y满足 将随意正整数x放到y的左边得到的数z满足 z%y==0 那么这个数就是个Magic Number 给你一个范围 求这个范围内Magic Number的个数 令 ...
 - zoj 2835 Magic Square(set)
		
Magic Square Time Limit: 2 Seconds Memory Limit: 65536 KB In recreational mathematics, a magic ...
 - ZOJ2477 Magic Cube
		
题目: This is a very popular game for children. In this game, there's a cube, which consists of 3 * 3 ...
 
随机推荐
- C#博客目录
			
基础加强 1.索引器 2.密闭类.静态类及扩展方法 3.值.引用类型及结构体 4.秒懂IL.CTS.CLS和CLR 5.装箱与拆箱 6.引用相等与运算符重载 7.ref与out 8.委托和事件 9.对 ...
 - 马永亮(马哥) linux视频2018年版
			
视频试看地址: 百度云链接: https://pan.baidu.com/s/1q4DV62DiFbBT8t63CapLJA 提取码: h5e4 资料收集于网络,如有侵权,告知后删除
 - 【LeetCode每天一题】Add Binary(二进制加法)
			
Given two binary strings, return their sum (also a binary string).The input strings are both non-emp ...
 - Netflix是怎样运行的(极度简化版)— 每次点击播放按钮背后的复杂东西
			
Netflix是怎样运行的(极度简化版)- 每次点击播放按钮背后的复杂东西 本文摘译自 How Netflix works: the (hugely simplified) complex stuff ...
 - Windows SFTP 的安装
			
用于Windows系统的免费SFTP服务器-Free SFTP Servers 前不久,有人问我:“怎么从 Linux 系统传文件到 Windows 服务器,不能用 FTP 协议.” 文件数量不大.用 ...
 - reduce函数
			
python中的reduce python中的reduce内建函数是一个二元操作函数,他用来将一个数据集合(链表,元组等)中的所有数据进行下列操作:用传给reduce中的函数 func()(必须是 ...
 - 文件中间修改内容遇到OSEerror
			
for i in f: 实际上是一直在调用 f.next() .(表明在交互模式下不能使用f.tell())从报错来看,是说 f.next() 方法被调用的时候,f.tell() 方法不可以被调用.
 - 接口自动化测试持续集成--Soapui接口功能测试断言
			
断言也就是判断实际结果与预期结果是否相等,如果相等测试通过,否则测试失败,自动化测试不管是UI,Services还有unit都需要做断言. 一.添加断言步骤的组件 二.设置断言 设置常用断言的三种方式 ...
 - 设置VS2017背景图片
			
设置方法很简单:安装扩展ClaudiaIDE 1.在这里下载扩展,https://visualstudiogallery.msdn.microsoft.com/9ba50f8d-f30c-4e33-a ...
 - Hadoop大数据学习视频教程  大数据hadoop运维之hadoop快速入门视频课程
			
Hadoop是一个能够对大量数据进行分布式处理的软件框架. Hadoop 以一种可靠.高效.可伸缩的方式进行数据处理适用人群有一定Java基础的学生或工作者课程简介 Hadoop是一个能够对大量数据进 ...