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 ...
随机推荐
- 添加新网络模型后运行报错:未定义的参数:ps_roipooling param
现象描述:在新增了具有自定义的data层或者loss层的网路之后,工程运行会报错: 疑惑:并没有这样的参数新增,并且前向的deploy文件已经将自定义的loss以及data等都去掉了: 可能的原因:虽 ...
- 《图解HTTP》读书笔记(四:HTTP方法)
1.作用 告知服务器我的意图是什么使用以下方法下达命令. 2.方法 GET 方法用来请求访问已被 URI 识别的资源. 指定的资源经服务器端解析后返回响应内容. ---URI可以定位互联网上的资源 P ...
- 20190423 PowerDesigner 数据库模型快速建立
后面我在做一个视频的讲解记录吧! 那种讲解记录,只是为了演示按钮功能在什么地方,这个功能的作用是什么 这个软件相对比较简单的使用步骤,主要有三步 第一. 选择好你针对的数据库版本和类型创建数据库名称基 ...
- Adobe XD 介绍
Adobe XD 关于XD这个软件我也是经过别人介绍才知道的,刚出来每两年,之前是没有中文版的,最近才更新了中文版,使用起来更加方便了. 这就是主界面,界面十分简洁但又一目了然,同时主界面还会有链接, ...
- 使用Apache JMeter对SQL Server、Mysql、Oracle压力测试(一)
前段时间面试被问到了数据库方面的知识:比如选择什么样的数据库,如何优化,怎么加索引,于是想到了自己动手测试一下常用数据库的性能: 第一步,下载好JMeter之后打开运行.话说这个JMeter打开还真是 ...
- C++---使用VS在C++编程中出现 fatal error C1010: 在查找预编译头时遇到意外的文件结尾。是否忘记了向源中添加“#include "stdafx.h"”?
啦啦啦,好久没写博客啦... 对于C++初学者来说适应一个新的编译器还是需要蛮长一段时间的,现在我就给你们说说标题所说的这个问题吧... 第一步:菜单--〉项目--〉设置,出现“项目设置”对话框,左边 ...
- 克隆Rockey6加密狗复制资料
克隆Rockey6加密狗复制资料下载 描述:Rockey6加密狗复制克隆方法Rockey6加密狗复制案例解析! 一.用OD加载DLL,并分析: 10001320 >/$ B8 4C140000 ...
- 华为AR-111S路由器GRE协议设置
一.GRE的定义: gre(generic routing encapsulation,通用路由封装)协议是对某些网络层协议(如ip 和ipx)的数据报进行封装,使这些被封装的数据报能够在另一个网络层 ...
- day13 python迭代器与生成器
迭代器 字符串.列表.元组.字典.集合都可以被for循环,说明他们都是可迭代的 可迭代协议 : 就是内部实现了__iter__方法 可以被for循环的都是可迭代的,要想可迭代,内部必须有一个__ite ...
- 【数据结构】算法 LinkList (Merge Two Sorted Lists)
合并2个有序链表 list A, list B, Solution: 对A,B 表按序读取数据,比较大小后插入新链表C. 由于两个输入链表的长度可能不同,所以最终会有一个链表先完成插入所有元素,则直接 ...