你有四个彩色立方体。每个立方体的每一面都是单一颜色,有四种颜色:蓝色(B),红色(R),绿色(G)和黄色(Y)将六个面描述为前,后,左,右,顶,底部,立方体颜色是:
目标是找到将四个立方体堆叠为垂直列的方法,以便列的每一侧都显示所有四种颜色。
我能做什么,
感谢
PS:
function init(){
var a = [['R','B'],['G','Y'],['B','Y']],
b = [['R','G'],['G','Y'],['B','B']],
c = [['Y','B'],['R','G'],['Y','R']],
d = [['Y','G'],['B','R'],['R','R']]
var box = [a,b,c,d]
return box;
}
答案 0 :(得分:0)
搜索所有可能的状态:没有那么多(每个立方体24次旋转,其中大多数可以过滤掉)。伪代码:
for cubeLevel0 in rotations(cubes[0]):
var sidesMatch = function(cube) {
return cube.N==cubeLevel0.N and cube.S==cubeLevel0.S and ...
}
for cubeLevel1 in rotations(cube[1]).filter(sidesMatch):
for cubeLevel2 in rotations(cube[2]).filter(sidesMatch):
for cubeLevel3 in rotations(cube[3]).filter(sidesMatch):
console.log(cubeLevel0, cubeLevel1, cubeLevel2, cubeLevel3)
嵌套的for循环是丑陋的,您可以通过将其实现为递归函数来删除它们。为了清楚起见,我把它留在了这里。
要实现rotations(cube)
功能,您有几个选择。最简单的方法是在你面前采用一个物理立方体,并以任意顺序标记每一面{0,1,2,3,4,5}
。然后在物理上执行24次旋转中的一次,并记下置换顺序,然后撤消旋转,这样您就可以回到起点。然后像这样编写函数:
// You HAVE to manually discover these with the physical method described above
var rotationPermutations = [[0,1,2,3,4,5,6], [3,4,2,1,5,0], ...(24 of these)...];
function rotations(cube) { // representing a cube as a 6-tuple
return rotationPermutations.map(function(permutation) {
// return [cube[i] for i in permutation]
return permutation.map(function(x){return cube[x]})
});
}
如果您能想出一种检测排列无效的方法,您可以更聪明地做事情;那么你可以生成轴的所有48个排列(使用[[axis0top, axis0bottom], [axis1top, axis1bottom], [axis2top, axis2bottom]]
表示,通过置换轴3!方式,并且每个轴以2种方式翻转,因此2 ^ 3 = 8个翻转可能性,3!* 2 ^ 3 = 48)并用错误的手性过滤掉其中的一半。然而,考虑到这一点比使用上述手动方法写下排列要花费更长的时间。
答案 1 :(得分:0)
Array.prototype.in_array = function(e) {
for (i = 0; i < this.length; i++) {
if (this[i] == e) return true;
}
return false;
}
Array.prototype.clone = function () {
return this.slice();
}
function deepcopy(obj) {
var out = [],i = 0,len = obj.length;
for (; i < len; i++) {
if (obj[i] instanceof Array){
out[i] = deepcopy(obj[i]);
}
else out[i] = obj[i];
}
return out;
}
function Cube (id, front, back, left, right, top, bottom) {
var self = this;
self.id=id;
self.front = front;
self.back = back;
self.left = left;
self.right = right;
self.top = top;
self.bottom = bottom;
self.turnMethod1 = function(){//left-front-right-back
var tmp=self.left;
self.left=self.front;
self.front=self.right;
self.right=self.back;
self.back=tmp;
}
self.turnMethod2 = function(){//left-top-right-bottom
var tmp=self.left;
self.left=self.top;
self.top=self.right;
self.right=self.bottom;
self.bottom=tmp;
}
self.turnMethod3 = function(){//front-top-back-bottom
var tmp=self.front;
self.front=self.top;
self.top=self.back;
self.back=self.bottom;
self.bottom=tmp;
}
self.fn = [
self.turnMethod1,self.turnMethod2,self.turnMethod3
]
}
Cube.prototype.toString = function(){
return this.id+"\t"+this.front+"\t"+this.back+"\t"+this.left+"\t"+this.right+"\t"+this.top+"\t"+this.bottom+"\n";
}
var cube1=new Cube(1,"R","B","G","Y","B","Y");
var cube2=new Cube(2,"R","G","G","Y","B","B");
var cube3=new Cube(3,"Y","B","R","G","Y","R");
var cube4=new Cube(4,"Y","G","B","R","R","R");
function count() {
for (var i = 0; i < 3; i++) {
for (var n = 0; n < 4; n++) {
cube1.fn[i]()
for (var a = 0; a < 3; a++) {
for (var b = 0; b < 4; b++) {
cube2.fn[a]()
for (var c = 0; c < 3; c++) {
for (var d = 0; d < 4; d++) {
cube3.fn[c]()
for (var e = 0; e < 3; e++) {
for (var f = 0; f < 4; f++) {
if(done()){
printPermutation();
}
cube4.fn[e]()
};
};
};
};
};
};
};
};
}
function done() {
var arr = []; /*front validation*/
arr.push(cube1.front);
if (arr.in_array(cube2.front)) {
return false;
} else {
arr.push(cube2.front);
}
if (arr.in_array(cube3.front)) {
return false;
} else {
arr.push(cube3.front);
}
if (arr.in_array(cube4.front)) {
return false;
} else {
arr.push(cube4.front);
} /*back validation*/
var arr = [];
arr.push(cube1.back);
if (arr.in_array(cube2.back)) {
return false;
} else {
arr.push(cube2.back);
}
if (arr.in_array(cube3.back)) {
return false;
} else {
arr.push(cube3.back);
}
if (arr.in_array(cube4.back)) {
return false;
} else {
arr.push(cube4.back);
}
/*left validation*/
var arr = [];
arr.push(cube1.left);
if (arr.in_array(cube2.left)) {
return false;
} else {
arr.push(cube2.left);
}
if (arr.in_array(cube3.left)) {
return false;
} else {
arr.push(cube3.left);
}
if (arr.in_array(cube4.left)) {
return false;
} else {
arr.push(cube4.left);
} /*right validation*/
var arr = [];
arr.push(cube1.right);
if (arr.in_array(cube2.right)) {
return false;
} else {
arr.push(cube2.right);
}
if (arr.in_array(cube3.right)) {
return false;
} else {
arr.push(cube3.right);
}
if (arr.in_array(cube4.right)) {
return false;
} else {
arr.push(cube4.right);
}
return true;
}
function printPermutation(){
console.log("----------------------------");
var arr = []
arr.push("Cube\tFront\tBack\tLeft\tRight\tTop\tBottom\n");
arr.push(cube1.toString());
arr.push(cube2.toString());
arr.push(cube3.toString());
arr.push(cube4.toString());
console.log(arr.join(""))
}
function main(){
count()
}
main()