【DS】骑士周游问题
简述:最早由印度棋手在公元前200年的时候直观的提出。该问题用现代语言来描述的话,仍是在一个具体的图中寻找哈密尔顿路径的问题。图G中的哈密尔顿路径指的是经过图G中的每一个顶点,且只经过一次的一条轨迹。如果这条轨迹是一条闭合路径,即从起点出发不重复地遍历所有的点后仍能回到起始点,那么这条路径就成为就称为哈密尔顿路径。本问题是研究马在棋盘上的跳动,能否在每个棋盘的格子里只经过一次而遍历整个棋盘?
骑士周游问题的相关定理:
1)定理1:在8x8的棋盘里面,令Aij表示第i行第j列公共的棋格(1≤i,j≤8),Akl表示~~。由移动法可知,从Aij到Akl的移动是合法的,当且仅当|i-k|=1,|j-l|=2或者相反;
2)定理2:当|(i+j)-(k+l)|为偶数的时候,从Aij到Akl的移动是绝对不合法的,也就是说,只有当(i+j)和(k+l)的奇偶性不同的时候,从Aij到Akl的移动才可能是合法的;
3)定理3:将骑士周游问题扩展到nxn的棋盘上,当n≥5的时候,以任意点作为初始点骑士巡游问题都有解;
解法:单纯的使用回溯法而不加优化,效率是非常低下的。于是有人提出启发式的遍历规则,即向前看两部,当没准备跳一步的时候,设准备跳到(x,y)计算(x,y)这一点可能往几个方向跳,将这个数目设为(x,y)的权值,并将所有可能的(x,y)按权值排序,从最小的开始,循环遍历所有可能的(x,y),回溯求出结果,该算法可以求出所有可能的骑士周游路径,算出一个可行的结果很快,其不足之处在于时间复杂度上并没有本质的变化,要求出所有的路径,仍然很慢。
下面是代码,根据启发式搜索思想进行,首先是两个辅助类:
第一个类:ChessPosition:这个类主要用于记录棋子所在的位置,是用于记录其走过的路径所用的。这里面有一个参数粗腰解释,那就是count,这个参数的作用如下:当棋子第一次走过某个棋格的时候,它是0,这个0有两个作用,首先是表明它是第一次走过这个棋格;其次,也是最主要的作用,我们在上面提到启发式搜索需要计算从这个棋格能够跳到其他哪些棋格(记为路径path);这些其他的棋格又能跳到哪些另外的棋格,记录其数目排序并且选取一个路径,第一次我们是选择最小的,是index(0),但是当我们回溯的时候,我们不能再次进入index(0),否则程序会陷入死循环,我们必须跳过index(0),继而进入index(1),第二次回溯到此则只能进入index(2),依此类推。所以count在每次回溯之前都自增一次,以指定到底选择那条可以走的路径
public class ChessPosition {
private int xCor;
private int yCor;
private int count;//记录回溯到这个点的次数
public ChessPosition(int x, int y){
this.xCor = x;
this.yCor = y;
count = 0;
}
public int getxCor() {
return xCor;
}
public void setxCor(int xCor) {
this.xCor = xCor;
}
public int getyCor() {
return yCor;
}
public void setyCor(int yCor) {
this.yCor = yCor;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}
第二个辅助类:Path:这个类比较简单,记录了从(xOrgin,yOrgin)点可以跳跃到其他哪些点,这些点记录在ridePositions中,可以把他看作点(xOrgin,yOrgin)的路径
import java.util.ArrayList;
/**
* 记录可以由一个点跳到其他哪几个点
* */
public class Path {
private int xOrgin;
private int yOrgin;
private ArrayList<ChessPosition> ridePositions;
public Path(int xOrgin, int yOrgin) {
this.xOrgin = xOrgin;
this.yOrgin = yOrgin;
ridePositions = new ArrayList<ChessPosition>();
}
public int getxOrgin() {
return xOrgin;
}
public void setxOrgin(int xOrgin) {
this.xOrgin = xOrgin;
}
public int getyOrgin() {
return yOrgin;
}
public void setyOrgin(int yOrgin) {
this.yOrgin = yOrgin;
}
public void addPosition(ChessPosition position){
ridePositions.add(position);
}
public int getSize(){
return ridePositions.size();
}
public ChessPosition getIndex(int index){
return ridePositions.get(index);
}
}
主类:Rider类:
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
/**
* 骑士周游问题
* */
public class Rider {
static int chessBoard[][] = {{-1,-1,-1,-1,-1,-1,-1,-1},
{-1,-1,-1,-1,-1,-1,-1,-1},
{-1,-1,-1,-1,-1,-1,-1,-1},
{-1,-1,-1,-1,-1,-1,-1,-1},
{-1,-1,-1,-1,-1,-1,-1,-1},
{-1,-1,-1,-1,-1,-1,-1,-1},
{-1,-1,-1,-1,-1,-1,-1,-1},
{-1,-1,-1,-1,-1,-1,-1,-1}};//棋盘,初始化为-1,棋子走过的路径根据步数修改数字,第一步记为0;
static ArrayList<ChessPosition> ridePath = new ArrayList<ChessPosition>();//记录走过的路径
static int countSteps = 0;//记录步数
static int countSolutions = 1;//记录第几种解法
public static void main(String[] args) {
// TODO Auto-generated method stub
ChessPosition orginPosition = new ChessPosition(0,0);
chessBoard[0][0] = 0;
ridePath.add(orginPosition);
Ride(orginPosition);
}
//函数:检测在一个点可以跳动到其他位置的个数
public static Path getRideNumber(int x, int y){
Path path = new Path(x, y);
if((x>1) &&(y>0) && chessBoard[x-2][y-1] == -1)
path.addPosition(new ChessPosition(x-2, y-1));
if((x>0) &&(y>1) && chessBoard[x-1][y-2] == -1)
path.addPosition(new ChessPosition(x-1, y-2));
if((x>1) &&(y<7) && chessBoard[x-2][y+1] == -1)
path.addPosition(new ChessPosition(x-2, y+1));
if((x>0) &&(y<6) && chessBoard[x-1][y+2] == -1)
path.addPosition(new ChessPosition(x-1, y+2));
if((x<7) &&(y<6) && chessBoard[x+1][y+2] == -1)
path.addPosition(new ChessPosition(x+1, y+2));
if((x<6) &&(y<7) && chessBoard[x+2][y+1] == -1)
path.addPosition(new ChessPosition(x+2, y+1));
if((x<6) &&(y>0) && chessBoard[x+2][y-1] == -1)
path.addPosition(new ChessPosition(x+2, y-1));
if((x<7) &&(y>1) && chessBoard[x+1][y-2] == -1)
path.addPosition(new ChessPosition(x+1, y-2));
return path;
}
/**
* 现在假设从一个给定的初始点进行遍历,那么首先看看它能跳到哪些点,纪录之后在paths中,如果path是0,而此时步数不是63,那么很明显,马已经
* 跳到了死路,因为既没有跳完,也不能再继续往下跳,很显然,需要回溯,也就是将倒数一次的初始点删去,恢复原貌,取到倒数第二个结点,先讲遍历次数
* 加1,然后从此点开始进行回溯;如果等于63,那么说明已经遍历完成,打印路径即可——————(还要回溯!)
* 倘若以上都不发生,也就是说很完好的取得了paths,此时也就得到了从初始点可以跳跃的路径,现在就是要看这些路径能够跳往哪些结点,记录进入
* secondRidePoints,按照可以跳往的结点数量进行排序,此时到底要取哪一个,需要使用遍历次数,倘若是第一次遍历,则为0,
* 否则回溯之前都是加1的;
* 此时如果遍历次数小于初始结点的路径数,则可以放入路径
* 倘若不遍历次数已经大于初始结点的路径数,则说明poisition也已经出问题了,必须重新回溯,也就是对poisition的上一步进行回溯,换句话说,
* poisition得删掉!
*/
public static void Ride(ChessPosition poisition){
//首先看看它能跳到哪些点
Path paths = getRideNumber(poisition.getxCor(), poisition.getyCor());
if(paths.getSize() == 0 && countSteps != 63){
//绝路上了,需要回溯(此时回溯只要告诉前一个节点,选择另外一个格子进行跳跃即可)
//刚刚已经跳跃一步了,现在跳跃下一步,发现无处可跳,并且还没有完成遍历,此时应该把xOrgin和yOrgin这一步删除!
countSteps --;
chessBoard[poisition.getxCor()][poisition.getyCor()] = -1;
int pathLength = ridePath.size();
ridePath.remove(pathLength-1);
ChessPosition lastPosition = ridePath.get(pathLength-2);
int recallCount = lastPosition.getCount();
lastPosition.setCount(recallCount+1);
Ride(lastPosition);
}else if(countSteps == 63){
System.out.print("Congratulations!第" + countSolutions +"种解法:");
countSolutions++;
printPath();
}
//这些点又能跳到其它哪些点
ArrayList<Path> secondRidePoints = new ArrayList<Path>();
for(int index = 0; index < paths.getSize(); index++){
ChessPosition position = paths.getIndex(index);
secondRidePoints.add(getRideNumber(position.getxCor(), position.getyCor()));
}
//排序
sortList(secondRidePoints);
int pathOrder = poisition.getCount();
if(pathOrder < secondRidePoints.size()){
Path nextPath = secondRidePoints.get(pathOrder);//取第N个,可能需要回溯
int secondX = nextPath.getxOrgin();
int secondY = nextPath.getyOrgin();//跳到这个点
ChessPosition newPosition = new ChessPosition(secondX, secondY);
//将这个点设置为步数
chessBoard[newPosition.getxCor()][newPosition.getyCor()] = countSteps;
countSteps ++;
ridePath.add(newPosition);
Ride(newPosition);
}else{
//如果不对,此时更加麻烦,说明上一个点也已经到了最后
countSteps --;
chessBoard[poisition.getxCor()][poisition.getyCor()] = -1;
int pathLength = ridePath.size();
ridePath.remove(pathLength-1);
ChessPosition lastPosition = ridePath.get(pathLength-2);
int recallCount = lastPosition.getCount();
lastPosition.setCount(recallCount+1);
Ride(lastPosition);
}
}
public static void sortList(ArrayList<Path> Points){
Collections.sort(Points, new SortByCount());
}
public static void printPath(){
ChessPosition position = null;
for(int index = 0; index < ridePath.size(); index++){
position = ridePath.get(index);
System.out.print("(" + position.getxCor() + "," + position.getyCor()+")-");
}
System.out.println(ridePath.size());
}
}
class SortByCount implements Comparator {
public int compare(Object o1, Object o2) {
Path listA = (Path) o1;
Path listB = (Path) o2;
if (listA.getSize() > listB.getSize())
return 1;
else
return 0;
}
}
以上就是整个代码,下面几个答案:
Congratulations!第1种解法:(0,0)-(1,2)-(0,4)-(1,6)-(3,7)-(5,6)-(7,7)-(6,5)-(5,7)-(7,6)-(6,4)-(7,2)-(6,0)-(4,1)-(2,0)-(0,1)-(1,3)-(0,5)-(1,7)-(3,6)-(2,4)-(0,3)-(1,1)-(3,0)-(2,2)-(1,0)-(0,2)-(2,1)-(4,0)-(3,2)-(5,1)-(7,0)-(6,2)-(5,0)-(7,1)-(5,2)-(3,1)-(4,3)-(3,5)-(1,4)-(0,6)-(2,7)-(1,5)-(0,7)-(2,6)-(4,7)-(6,6)-(4,5)-(3,3)-(2,5)-(4,4)-(2,3)-(4,2)-(6,3)-(7,5)-(5,4)-(7,3)-(6,1)-(5,3)-(7,4)-(5,5)-(3,4)-(4,6)-(6,7)-64
Congratulations!第2种解法:(0,0)-(1,2)-(0,4)-(1,6)-(3,7)-(5,6)-(7,7)-(6,5)-(5,7)-(7,6)-(6,4)-(7,2)-(6,0)-(4,1)-(2,0)-(0,1)-(1,3)-(0,5)-(1,7)-(3,6)-(2,4)-(0,3)-(1,1)-(3,0)-(2,2)-(1,0)-(0,2)-(2,1)-(4,0)-(3,2)-(5,1)-(7,0)-(6,2)-(5,0)-(7,1)-(5,2)-(3,1)-(4,3)-(3,5)-(1,4)-(0,6)-(2,7)-(1,5)-(0,7)-(2,6)-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,6)-(4,5)-(3,3)-(2,5)-(4,4)-(6,3)-(5,5)-(7,4)-(5,3)-(3,4)-(4,6)-(6,7)-(7,5)-(5,4)-(7,3)-(6,1)-(4,2)-(2,3)-64
Congratulations!第39种解法:(0,0)-(1,2)-(0,4)-(1,6)-(3,7)-(5,6)-(7,7)-(6,5)-(5,7)-(7,6)-(6,4)-(7,2)-(6,0)-(4,1)-(2,0)-(0,1)-(1,3)-(0,5)-(1,7)-(3,6)-(2,4)-(0,3)-(1,1)-(3,0)-(2,2)-(1,0)-(0,2)-(2,1)-(4,0)-(3,2)-(5,1)-(7,0)-(6,2)-(5,0)-(7,1)-(5,2)-(3,1)-(4,3)-(3,5)-(1,4)-(0,6)-(2,7)-(1,5)-(0,7)-(2,6)-(4,7)-(6,6)-(4,5)-(3,3)-(2,5)-(4,4)-(6,3)-(5,5)-(7,4)-(5,3)-(6,1)-(7,3)-(5,4)-(7,5)-(6,7)-(4,6)-(3,4)-(4,2)-(2,3)-64
可以选取一种在棋盘上画画看。
备注:代码有好几处不太完善,例如:
1)在寻找到一个解的时候没有进行回溯(会导致丢失解);
2)在回溯的过程中,没有判断路径中是否有结点可以回溯(这会导致异常);
3)如果在Main函数里面加入一个循环,让棋盘的每一格都成为初始棋格,那么可以看到很多不一样的答案
猜测:这个程序在运行的时候,由于函数嵌套调用太多(本题的解非常之多),会出现StackOverflowError异常,估计如果此处不抛出栈溢出异常,后面就会出现IndexOutBounds异常,因为pathLength-2 < 0!
算法虽能得出答案,但仍有待改进,欢迎交流~