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 ...
随机推荐
- CF1093F Vasya and Array
题目链接:洛谷 以后还是要多打CF,不然就会错过这些很好的思维题了.我dp学得还是太烂,要多总结. 首先$len=1$就直接输出0. 我们考虑$dp[i][j]$表示前$i$个数的答案,而且第$i$个 ...
- Python模块安装与读取Excel
今天.想用Python读取一下Excel中的数据,从网上查找了一个样例,是要安装相关的模块: 1:到python官网下载http://pypi.python.org/pypi/xlrd模 ...
- 60道Python面试题&答案精选!找工作前必看
需要Word/ PDF版本的同学可以在实验楼微信公众号回复关键词"面试题"获取. 1. Python 的特点和优点是什么? 答案:略. 2. 什么是lambda函数?它有什么好处? ...
- charles-抓包Andriod 手机的设置
长按弹出 修改后: charles如果不配置SSL通用证书: 会导致HPPTS协议的域名抓取失败/乱码的现象: 现在SSL越来越多,很多博客都上了SSL,支付相关的行业更是基础配置: charles配 ...
- memcached----------linux下安装memcached,以及php的memcached扩展。
1.通过wget http://www.memcached.org/files/memcached-1.4.24.tar.gz下载最新源码2.解压tar -xf memcached-1.4.24.ta ...
- logback 指定每隔一段时间创建一个日志文件
我使用的logback版本是1.2.3 目前logback支持根据时间来配置产生日志文件,但是只支持每周,每天,每个小时,每分钟等创建一个文件,配置如下: <appender name=&quo ...
- Angular4 构建,部署,多环境
- java 命令查字节码文件, 查.class文件内容
1. 需要用javac,javap命令,所以先配下环境变量 2.配置环境变量 单击“计算机-属性-高级系统设置”,单击“环境变量”.在“系统变量”栏下单击“新建”,创建新的系统环境变量. 3.写需要用 ...
- Selenium WebDriver的工作原理
先通过一个简单的类比说个好理解的,这个比喻是我从美版知乎Quora上看到的,觉得比较形象.好理解拿来用用. 我们可以把WebDriver驱动浏览器类比成出租车司机开出租车. 在开出租车时有三个角色: ...
- react context跨组件传递信息
从腾讯课堂看到的一则跨组件传递数据的方法,贴代码: 使用步骤: 1.在产生参数的最顶级组建中,使用childContextTypes静态属性来定义需要放入全局参数的类型 2.在父组件中,提供状态,管理 ...