骑士周游问题 --- 递归解法 --- java代码

骑士游历:

定义了向量的数组M,行数组X,列数组Y, 棋盘plane,计数器count,走动步数step

需要注意的是,递归函数的进入前的验证,原先的想法是传入来时的方向参数,可是这样的想法被实践否定了,

从理论上看,一个棋子走向哪里是不受它的过去制约的,最近的过去也不例外,

详情请见:http://en.wikipedia.org/wiki/Knights_tour

代码如下:

/**
 * Created by kodoyang on 2014/5/3.
 */
public class KnightTour {

    private static final int[][] M= {
            {2, 1}, {1, 2}, {-1, 2}, {-2, 1}, {-2, -1}, {-1, -2}, {1, -2}, {2, -1}
    };

    private static final int[] X = {0, 1, 2, 3, 4, 5, 6, 7}, Y = {0, 1, 2, 3, 4, 5, 6, 7};

    private static final int[][] plane = new int[X.length][Y.length];

    public final static void main(String[] args){

        int[] O = {0, 0};
        next(X[0], Y[0], 1);
    }

    private static int count = 0;
    private final static void next(final int x, final int y, final int step){

        plane[x][y] = step;
        if(step == 64)
            print( ++count );
        else {
            for(int i = 0; i < M.length; i++){
                int[] v = M[i];
                int _x = x + v[0], _y = y + v[1];

                if (valid(_x, _y))
                    next(_x, _y, step + 1);

            }
        }

        plane[x][y] = 0;
    }

    private static final boolean valid(final int x, final int y){
        boolean result = false;

        if(x >= X[0] && x <= X[7] && y >= Y[0] && y <= Y[7]){
            result = plane[x][y] == 0;
        }

        return result;
    }

    private static final void print(int step){
        System.out.println("----------------------------------   " + step + "   ------");
        for(int i = X[0]; i <= X[7]; ++i) {
            for (int j = Y[0]; j <= Y[7]; ++j) {
                System.out.print("\t" + plane[i][j]);
            }
            System.out.println();
        }
        System.out.println("---------------------------------------------------");
    }

}

这里一共有26,534,728,821,064种结果,程序被我提前停掉了,运行结果如下:

----------------------------------      ------

---------------------------------------------------
----------------------------------      ------

---------------------------------------------------
----------------------------------      ------

---------------------------------------------------
----------------------------------      ------

---------------------------------------------------
----------------------------------      ------

---------------------------------------------------
----------------------------------      ------

---------------------------------------------------
----------------------------------      ------

---------------------------------------------------
----------------------------------      ------

---------------------------------------------------
----------------------------------      ------

---------------------------------------------------
----------------------------------      ------

---------------------------------------------------
----------------------------------      ------

---------------------------------------------------
----------------------------------      ------

---------------------------------------------------
----------------------------------      ------

---------------------------------------------------
----------------------------------      ------

---------------------------------------------------
----------------------------------      ------

---------------------------------------------------
----------------------------------      ------

---------------------------------------------------
----------------------------------      ------

---------------------------------------------------
----------------------------------      ------

---------------------------------------------------
----------------------------------      ------

---------------------------------------------------
----------------------------------      ------

---------------------------------------------------
----------------------------------      ------

---------------------------------------------------
----------------------------------      ------

---------------------------------------------------
----------------------------------      ------

---------------------------------------------------
----------------------------------      ------

---------------------------------------------------
----------------------------------      ------

---------------------------------------------------
----------------------------------      ------

---------------------------------------------------
----------------------------------      ------

---------------------------------------------------
----------------------------------      ------

---------------------------------------------------
----------------------------------      ------

---------------------------------------------------
----------------------------------      ------

---------------------------------------------------
----------------------------------      ------

---------------------------------------------------
----------------------------------      ------

---------------------------------------------------
----------------------------------      ------

---------------------------------------------------
----------------------------------      ------

---------------------------------------------------

Process finished with exit code -
上一篇:Spring @Value注入值失败,错误信息提示:Could not resolve placeholder


下一篇:2018.12.31 bzoj4001: [TJOI2015]概率论(生成函数)