如果您正在开发软件来求解魔方,您将如何表示魔方?
发布于 2009-02-01 05:33:08
这个ACM Paper描述了它用来表示魔方的几种替代方法,并对它们进行了比较。遗憾的是,我没有获取全文的帐户,但描述中写道:
提出并比较了魔方的七种可选表示:3位整数的3x3x3数组;6x3x3文字数组;5x12文字矩阵;11x11稀疏文字矩阵;54元素向量;4维数组;以及3x3x3嵌套数组。APL函数用于方向移动和四分之一转弯,以及几个用于求解立方体的有用工具。
此外,这个RubiksCube.java文件包含一个非常干净的表示,以及用于旋转部分的相关代码(如果您正在寻找实际的代码)。它使用一个单元格和一个面数组。
发布于 2009-02-01 05:11:24
一种方法是专注于视觉外观。
一个立方体有六个面,每个面是一个三乘三的正方形阵列。所以
Color[][][] rubik = new Color[6][3][3];然后,每一步都是一种方法,它会排列一组特定的彩色方块。
发布于 2010-08-26 23:00:55
避免优化;使其面向对象。我使用的伪代码类大纲是:
class Square
+ name : string
+ accronym : string
class Row
+ left_square : square
+ center_square : square
+ right_square : square
class Face
+ top_row : list of 3 square
+ center_row : list of 3 square
+ bottom_row : list of 3 square
+ rotate(counter_clockwise : boolean) : nothing
class Cube
+ back_face : face
+ left_face : face
+ top_face : face
+ right_face : face
+ front_face : face
+ bottom_face : face
- rotate_face(cube_face : face, counter_clockwise : boolean) : nothing内存使用量如此之小,处理如此之少,以至于优化是完全不必要的,特别是当你牺牲代码可用性的时候。
https://stackoverflow.com/questions/500221
复制相似问题