问题描述:
在一个8x8(或者nxn)的棋盘上,一个骑兵(马)走日(对角)能否遍历整个棋盘。
http://en.wikipedia.org/wiki/Knight%27s_tour
Warnsdorff's algorithm: Heruistic 剪枝,排除不需要的回溯路
规则如下两条:
1)我们可以从棋盘上任意一处开始移动
2)我们每次移动到最近,最狭隘(周围没遍历过点最少)的点(be more greedy!)
算法的基本结构:
1. 任意选取点P为棋盘上起点
2. 标记P为1
3. 重复以下动作从2-64:
1. S 作为P能移动到的点的set
2. 标记P 为S里最小accesibility
3. 标记P 为现在的移动次数
4. 返回标记的数组
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
public class knight_tour {
public static final int N = 8;
//cx,cy记录可以移动的位置 8 种
public static final int cx[] = {1, 1, 2, 2, -1, -1, -2, -2};
public static final int cy[] = {2, -2, 1