You have four colored cubes. Each side of each cube is a single color, and there are four colors: blue (B), red (R), green (G) and yellow (Y) Describing the six faces as front, back, left, right, top, bottom, the cube colors are:Cube Front Back Left Right Top Bottom 
1 R B G Y B Y 
2 R G G Y B B 
3 Y B R G Y R 
4 Y G B R R R The objective is to find ways to stack the four cubes as a vertical column so that each side of the column is showing all four colors.
In a compiled language of your choice, write a program to find all successful permutations.

解决方案 »

  1.   

    我未完成的代码  大家参考一下 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 swap(arr){
    var result = [];
    var tmp = deepcopy(arr)
    for (var i = 0; i < arr.length; i++) { for (var j = 0; j < 2; j++) {

    tmp[j][0] = arr[j][1];
    tmp[j][1] = arr[j][0];
    };
    result.push(tmp)
    };
    console.log(result)
    return result
    }swap([[1,2],[3,4]])
    function redo(){}var  permute_fn = function(input) {
    var permArr = [],
    usedChars = []; function permute() {
        var i, ch;
        for (i = 0; i < input.length; i++) {
            ch = input.splice(i, 1)[0];
            usedChars.push(ch);
            if (input.length == 0) {
             var tmp = usedChars.slice()
                permArr.push(tmp);
            }
            permute(input);
            input.splice(i, 0, ch);
            usedChars.pop();
        }
        return permArr
    };
    return permute();
    }//假设blue=1,red=10,green=100,yellow=1000/*
    function init() {
    var a = [10, 1, 100, 1000, 1, 1000],
    b = [10, 100, 100, 1000, 1, 1],
    c = [1000, 1, 10, 100, 1000, 10],
    d = [1000, 100, 1, 10, 10, 10]
    var box = [a, b, c, d]
    return box;
    }
    */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;
    }
    //检测有几个位置不一样颜色/*
    function check(a, b, tmp) {
    if (!tmp) {
    var tmp = [0, 0, 0, 0, 0, 0];
    for (var i = 0; i < 6; i++) {
    if (a[i] != b[i]) tmp[i] = [a[i], b[i]];
    else swap(a,b,1)
    };
    } else {
    for (var i = 0; i < 6; i++) {
    if (tmp[i] != 0) {
    if (!tmp[i].in_array(b[i])) tmp[i].push(b[i]);
    }
    };
    }
    return tmp;
    }
    */function check_result (arr){
    var array = deepcopy(arr)
    var num = 0; length = array.length;
    for (i = 0; i <= length - 2; i++) {
    for (j = length - 1; j >= 1; j--) {    
    if (array[j] < array[j - 1]) {
    temp = array[j];
    array[j] = array[j - 1];
    array[j - 1] = temp;
    }
    }
    }}
    //一共有多少种排序可能
    function box_list () {
    var box = init();
    var result = []
    for (var i = 0; i < box.length; i++) {
    var tmp = permute_fn(box[i]) result.push(tmp)
    }; check_result(result)
    }
    //box_list()