Four Cubes Puzzle (四色立方体问题) ——纪念已逝的找工作的那些日子

雪灾、地震、奥运、经济危机、...... 构成了二零零八,而我找工作的日子也掺杂在其中,同学都说是我的毕业带

来了经济危机,当然这只是玩笑话

找工作的道路崎岖又坎坷,战线也拉得很长,记得那是2007年冬天的第一块雪:),我就踏上了这条不归路,记得当

时在中科院做项目的同寝哥们尹健(此处化名 )给推荐了个地儿实习,不过递交简历的同没时要提交解决此题的

源码,当时在网上找了找,没找到源码,但找到了一些讨论此题的帖子,借鉴了一些网友提供的思路和想法,生搬硬套

地写下了如下代码,连同自己的简历一同发了去。后来,公司还真打来了电话,同意接受我去实习,经过再三斟酌,

考虑到之前师兄师姐瞒着导师出去实习最终都没有什么好果子吃,还是放弃了。

现在把这段代码保存下来,纪念我的那些找工作的日子和我那帮兄弟姐妹。

代码未考虑执行效率,应该有好多可以改进的地方,也希望大家多多批评指正,在此给所有光临寒舍的朋友拜个晚年!

Happy  牛 Year !

原题如下:

At ThumbJive we are proud of our record of finding and training the most effective software engineers. Our rigorous interview cycle will test your engineering mettle. If you join us you will be expected to code on multiple platforms, work to tight deadlines, show initiative, and above all communicate with, trust and support the rest of the team. Your reward will be to use and develop the latest technologies on web and wireless, and to work in a high functioning and creative environment. This is only for developers with a passion and talent for software engineering.

Watch this space for information about openings. And if you are interested, below is an engineering puzzle that some of our recruits had to complete as part of their interview. Enjoy!

 

Engineering Puzzle

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. Please submit the following:

1.  Initial estimate of time required for the task

2.  Actual time taken for the task

3.  Source code of solution

4.  DO NOT include any compiled binary code (.exe ) as it could be rejected by mail clients

代码如下:

package fourcubes;

/**
 * Initial estimate of time required for the task : half day ;
 *
 * Actual time taken for the task : one day ;
 */

/**
 * <p>Title: Source code of solution for Engineering Puzzle </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2007</p>
 *
 * <p>Company: HIT ICE</p>
 *
 * @author :Lee
 * @version 1.0
 */

public class FourCubes {

    public static void main(String[] args) {
        /**
         * If four colors are different, the sum of them is 15.
         * Or the sum isn't 15.
         */
        int Blue = 1;
        int Red = 2;
        int Green = 4;
        int Yellow = 8;

        // Loop variables;
        int i, x, y, m, n, u, v;
        int p, q, r, s;
        int k;

        //Temp variables , used to swap variables' values ;
        int temp;
        char temp_char;
        int temp_a2, temp_b2, temp_c2, temp_d2;
        int temp_a5, temp_b5, temp_c5, temp_d5;

        int count = 0; // Record the total of sulutions.

        //judge whither each side of the column is showing all four colors
        boolean differ_left, differ_right, differ_front, differ_back;
        boolean differ;

        /**
         *     unfold the cube, it is like this
         *
         *                           right
         *                    top    back
         *             front  left
         *             bottom
         *
         *                             6
         *                      4      5
         *               2      3
         *               1
         *
         *       put the four cubes into four arrays;
         *       each array's length is 11 ;
         *       {1,2,3,4,5,6,1,2,3,4,5}
         *
         *                                           4      5
         *                                    2      3
         *                             6      1
         *                      4      5
         *               2      3
         *               1
         */
        int A[] = {Yellow, Red, Green, Blue, Blue, Yellow, Yellow, Red, Green, Blue, Blue};
        int B[] = {Blue, Red, Green, Blue, Green, Yellow, Blue, Red, Green, Blue, Green};
        int C[] = {Red, Yellow, Red, Yellow, Blue, Green, Red, Yellow, Red, Yellow, Blue};
        int D[] = {Red, Yellow, Blue, Red, Green, Red, Red, Yellow, Blue, Red, Green};

        int a[] = new int[6]; // record the first cube
        int b[] = new int[6]; // record the second cube
        int c[] = new int[6]; // record the third cube
        int d[] = new int[6]; // record the forth cube
        char array_temp[] = new char[6]; // record informations of the correct cube

        for (i = 0; i < 6; i++) {
            for (p = 0; p < 6; p++) {//put in the first cube
                a[p] = A[p + i];
            }
            for (x = 0; x < 6; x++) {
                for (q = 0; q < 6; q++) {//put in the second cube
                    b[q] = B[q + x];
                }
                for (y = 1; y <= 4; y++) {// turn 90 degrees each time
                    temp = b[1];
                    b[1] = b[2];
                    b[2] = b[4];
                    b[4] = b[5];
                    b[5] = temp;
                    for (m = 0; m < 6; m++) {
                        for (r = 0; r < 6; r++) { // put in the third cube
                            c[r] = C[r + m];
                        }
                        for (n = 1; n <= 4; n++) { //turn 90 degrees each time
                            temp = c[1];
                            c[1] = c[2];
                            c[2] = c[4];
                            c[4] = c[5];
                            c[5] = temp;
                            for (u = 0; u < 6; u++) {
                                for (s = 0; s < 6; s++) { //put in the forth cube
                                    d[s] = D[s + u];
                                }
                                for (v = 1; v <= 4; v++) { //turn 90 degrees each time
                                    temp = d[1];
                                    d[1] = d[2];
                                    d[2] = d[4];
                                    d[4] = d[5];
                                    d[5] = temp;

                                    //jude whether the front side of the column is showing all four colors
                                    differ_front = (a[1] + b[1] + c[1] +
                                            d[1] == 15);
                                    //jude whether the back side of the column is showing all four colors
                                    differ_back = (a[4] + b[4] + c[4] + d[4] ==
                                            15);

                                    /**
                                     *     unfold the cube, it is like this
                                     *
                                     *                           right
                                     *                    top    back
                                     *             front  left
                                     *             bottom
                                     *
                                     *                             6
                                     *                      4      5
                                     *               2      3
                                     *               1
                                     *
                                     *       put the four cubes into four arrays;
                                     *       each array's length is 11 ;
                                     *       {1,2,3,4,5,6,1,2,3,4,5}
                                     *
                                     *                                           4      5
                                     *                                    2      3
                                     *                             6      1
                                     *                      4      5
                                     *               2      3
                                     *               1
                                     *
                                     *       if the the shape of unfold cube put in the array a[] or b[] or c[c] or d[]  is like this:
                                     *
                                     *                             6      1
                                     *                      4      5
                                     *               2      3
                                     *
                                     *        the third color and the sixth color are reverse
                                     *
                                     */
                                    if (i % 2 == 1) {
                                        temp_a2 = a[5];
                                        temp_a5 = a[2];
                                    } else {
                                        temp_a2 = a[2];
                                        temp_a5 = a[5];
                                    }
                                    if (x % 2 == 1) {
                                        temp_b2 = b[5];
                                        temp_b5 = b[2];
                                    } else {
                                        temp_b2 = b[2];
                                        temp_b5 = b[5];
                                    }
                                    if (m % 2 == 1) {
                                        temp_c2 = c[5];
                                        temp_c5 = c[2];
                                    } else {
                                        temp_c2 = c[2];
                                        temp_c5 = c[5];
                                    }
                                    if (u % 2 == 1) {
                                        temp_d2 = d[5];
                                        temp_d5 = d[2];
                                    } else {
                                        temp_d2 = d[2];
                                        temp_d5 = d[5];
                                    }
                                    //jude whether the left side of the column is showing all four colors
                                    differ_left = (temp_a2 + temp_b2 + temp_c2 +
                                            temp_d2 ==
                                            15);
                                    //jude whether the right side of the column is showing all four colors
                                    differ_right = (temp_a5 + temp_b5 + temp_c5 +
                                            temp_d5 ==
                                            15);

                                    //jude whether each side of the column is showing all four colors
                                    differ = differ_front && differ_back &&
                                             differ_left && differ_right;

                                    if (differ) {//output the slutions of the Engineering Puzzle
                                        count++;
                                        System.out.println(
                                                "********************************************************");
                                        System.out.println("Slotion Of Number " + count +
                                                ":");
                                        System.out.println(
                                                "********************************************************");
                                        System.out.println(
                                                "Cube        Front  Back   Left   Right  Top    Bottom  *");
                                        System.out.println(
                                                "********************************************************");

                                        //output the first cube
                                        System.out.print(" 1     ");
                                        for (k = 0; k < 6; k++) {
                                            if (a[k] == Blue) {
                                                array_temp[k] = 'B';
                                            } else if (a[k] == Red) {
                                                array_temp[k] = 'R';
                                            } else if (a[k] == Green) {
                                                array_temp[k] = 'G';
                                            } else if (a[k] == Yellow) {
                                                array_temp[k] = 'Y';
                                            }
                                        }
                                        if (i % 2 == 1) {
                                            temp_char = array_temp[2];
                                            array_temp[2] = array_temp[5];
                                            array_temp[5] = temp_char;
                                        }
                                        System.out.println(
                                                "       " + array_temp[1] +
                                                "     " + array_temp[4] +
                                                "      " + array_temp[2] +
                                                "      " + array_temp[5] +
                                                "      " + array_temp[3] +
                                                "       " + array_temp[0]+
                                                "     *"
                                                );

                                        //output the second cube
                                        System.out.print(" 2     ");
                                        for (k = 0; k < 6; k++) {
                                            if (b[k] == Blue) {
                                                array_temp[k] = 'B';
                                            } else if (b[k] == Red) {
                                                array_temp[k] = 'R';
                                            } else if (b[k] == Green) {
                                                array_temp[k] = 'G';
                                            } else if (b[k] == Yellow) {
                                                array_temp[k] = 'Y';
                                            }
                                        }
                                        if (x % 2 == 1) {
                                            temp_char = array_temp[2];
                                            array_temp[2] = array_temp[5];
                                            array_temp[5] = temp_char;
                                        }
                                        System.out.println(
                                                "       " + array_temp[1] +
                                                "     " + array_temp[4] +
                                                "      " + array_temp[2] +
                                                "      " + array_temp[5] +
                                                "      " + array_temp[3] +
                                                "       " + array_temp[0]+
                                                "     *"
                                                );

                                        //output the third cube
                                        System.out.print(" 3     ");
                                        for (k = 0; k < 6; k++) {
                                            if (c[k] == Blue) {
                                                array_temp[k] = 'B';
                                            } else if (c[k] == Red) {
                                                array_temp[k] = 'R';
                                            } else if (c[k] == Green) {
                                                array_temp[k] = 'G';
                                            } else if (c[k] == Yellow) {
                                                array_temp[k] = 'Y';
                                            }
                                        }
                                        if (m % 2 == 1) {
                                            temp_char = array_temp[2];
                                            array_temp[2] = array_temp[5];
                                            array_temp[5] = temp_char;
                                        }
                                        System.out.println(
                                                "       " + array_temp[1] +
                                                "     " + array_temp[4] +
                                                "      " + array_temp[2] +
                                                "      " + array_temp[5] +
                                                "      " + array_temp[3] +
                                                "       " + array_temp[0]+
                                                "     *"
                                                );

                                        //output the forth cube
                                        System.out.print(" 4     ");
                                        for (k = 0; k < 6; k++) {
                                            if (d[k] == Blue) {
                                                array_temp[k] = 'B';
                                            } else if (d[k] == Red) {
                                                array_temp[k] = 'R';
                                            } else if (d[k] == Green) {
                                                array_temp[k] = 'G';
                                            } else if (d[k] == Yellow) {
                                                array_temp[k] = 'Y';
                                            }
                                        }
                                        if (u % 2 == 1) {
                                            temp_char = array_temp[2];
                                            array_temp[2] = array_temp[5];
                                            array_temp[5] = temp_char;
                                        }
                                        System.out.println(
                                                "       " + array_temp[1] +
                                                "     " + array_temp[4] +
                                                "      " + array_temp[2] +
                                                "      " + array_temp[5] +
                                                "      " + array_temp[3] +
                                                "       " + array_temp[0]+
                                                "     *"
                                                );
                                        System.out.println(
                                                "********************************************************");
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        System.out.println("TOTAL : " + count + " Solutions.");
    }
}

 

你可能感兴趣的:(C++,c,工作,REST,C#)