骑士游历:

定义了向量的数组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 -

最新文章

  1. [No00007D]2016-面经[上]
  2. PHP的 Mysqli扩展库的多语句执行
  3. docker学习笔记一:基本安装和设置容器静态ip
  4. hdu - 4608 - I-number
  5. 获得sql对应的binary
  6. js 只能输入数字和小数点
  7. 数据结构学习——shell排序的C语言实现
  8. CentOS允许/禁止ping的方法
  9. 原生JS添加节点方法与jQuery添加节点方法的比较及总结
  10. Rx RxJava【Operators】操作符
  11. 【转】java读写二进制文件的解决方法
  12. VPN工作原理
  13. eclipse远程调试Linux环境下的web项目
  14. 【转】GAMITBLOBK中固定解、浮点解、约束解、松弛解等解类型解释
  15. LoadRunner 11 error:Cannot initialize driver dll
  16. 黑盒测试实践——day04
  17. vue学习笔记——组件的优化
  18. flutter学习地址
  19. (转)获取 request 中用POST方式&quot;Content-type&quot;是&quot;application/x-www-form-urlencoded;charset=utf-8&quot;发送的 json 数据
  20. 使用Python制作第一个爬虫程序

热门文章

  1. 第一个Struts2程序
  2. ISO 8601
  3. 甲骨文推出MySQL Fabric,简化MySQL的高可用性与可扩展性
  4. 设置Sublime Text2 中代码提示
  5. hdu1828(线段树+扫描线)
  6. HDU 2512 一卡通大冒险
  7. css各浏览器的兼容性写法
  8. (转)ios获取设备系统信息
  9. yaf框架流程三
  10. 【英语】Bingo口语笔记(40) - [aʊ]的发音规则