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 ...
随机推荐
- SQLServer 大小写敏感配置
设置表内大小写敏感 ALTER TABLE 表名 ) COLLATE Chinese_PRC_CI_AS --不区分大小写 ALTER TABLE tb ) COLLATE Chinese_PRC_C ...
- vs2017 在win10下安装后开始运行asp.net core 项目时出错
vs2017 在win10下安装后开始运行asp.net core 项目时出错 报找不到什么 解决方法: 下载 asp.net 2.2 安装好,重新启动电脑问题解决.
- charles-Andriod 手机手机抓包乱码
然后重启进行进行抓包
- Oracle 12C 密码文件问题 ORA-01017: invalid username/password; logon denied
新安装的Oracle 12.1.0.2.0,NBU在测试备份的时候报ORA-01017 --alter user sys identified by "Wwjd!23";sqlpl ...
- namenode No valid image files
1,角色日志报错 Encountered exception loading fsimage java.io.FileNotFoundException: No valid image files f ...
- linux-git
- Vue与React的异同
众所周知,前端现在最火的两个框架是Vue和React了.通过一段时间的学习与项目上的实践,我想通过比较他们之间的异同点来发现以后在项目的技术选型中知道怎么抉择用哪个.有一点说明的是他们各自有自己的优势 ...
- IT题库6-同步和异步
同步就是许多线程同时共用一个资源,一个线程在用别的线程就要等待.异步相反,可以不用等待. 同步:发送一个请求,等待返回,然后才能再发送下一个请求:异步:发送一个请求,不等待返回,随时可以再发送下一个请 ...
- Instrumentation接口详解
Instrumentation接口位于jdk1.6包java.lang.instrument包下,Instrumentation指的是可以独立于应用程序之外的代理程序,可以用来监控和扩展JVM上运行的 ...
- 示例, linq分组
public class HIS_CLIREGISTER : BaseModel{ private String _FBCODE;[StringLength(8)]/// <summary> ...