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 ...
随机推荐
- ElasticSearch - 信息聚合系列之聚合过滤
摘要 聚合范围限定还有一个自然的扩展就是过滤.因为聚合是在查询结果范围内操作的,任何可以适用于查询的过滤器也可以应用在聚合上. 版本 elasticsearch版本: elasticsearch-2. ...
- python读取数据库并把数据写入本地文件
一,介绍 上周用jmeter做性能测试时,接口B传入的参数需要依赖接口A生成的借贷申请ID,接口A运行完需要把生成的借贷申请ID导出来到一个文件,作为参数传给接口B,刚开始的时候,手动去数据库倒, 倒 ...
- VS Code mac版全局搜索失效最简单解法
网上百度到的一些说法,说是添加以下命令行 "search.exclude": { "system/": true, "!/system/**/*.ps ...
- gitbook build/serve 失败,Error: ENOENT: no such file or directory, stat ...
我使用的 gitbook 版本 CLI version: 2.3.2 GitBook version: 3.2.3 在使用 gitbook 生成文档时,发现编译偶尔不规律性地出现错误 d:\Mine\ ...
- Sqlite3入门简记
一,安装Sqlite3 1.入门时看http://www.runoob.com/sqlite/sqlite-intro.html,说的简单,但是适合入门 2.在终端输入sqlite3,没有返回信息,表 ...
- C++ 赋值构造函数的返回值到底有什么用?且返回值是否为引用类型有什么区别吗?
首先定义类Person class Person{ public: string name; Person()=default; //默认构造函数 Person(string nam):name(na ...
- CentOS安装PHP7.*
安装 如之前安装过PHP,则先卸载 $ yum -y remove php* 由于linux的yum源不存在php7.x,所以我们要更改yum源 $ rpm -Uvh https://dl.fedor ...
- [python 练习] 计算个税
题目:利用python计算个税 说明:python有序字典的使用 代码: # -*- coding: utf-8 -*- from collections import OrderedDict # 税 ...
- centos上发布部署python的tornado网站项目完整流程
先说下大体上的做法,开发环境上要新弄一个 virtualenv的环境,在这个里面放你的开发调试,当然这个其实也不是必须的,但是这样会方便管理一些. 再在centos上也弄一个 virtualenv虚拟 ...
- jpa 使用笔记
1,嵌套类查询 List<Person> findByAddress_ZipCode(ZipCode zipCode); ZipCode是Address属性的内部属性. 2,jpa in ...