马踏棋盘算法 Java实现

马在某个点最多可能有8种走法,用递归和回溯实现。

注:代码中,查找下一个可走坐标是从右下第一个开始的,也就是图中的4。可以通过修改a,b...h的值来改变顺序。


/**
 * 马踏棋盘算法 
 * 递归和回溯
 *
 */
public class HorseStep {
	
	public static int X = 8;
	public static int Y = 8;
	
	public static int returnCount = 0;
	
	/**
	 * 棋盘
	 */
	public static int chess[][] = new int[X][Y];
	
	
	/**
	 * 找到基于(x,y)位置的下一个可走位置
	 * @param x
	 * @param y
	 * @param count
	 * @return
	 */
	public static int nextxy(XY xy,int count){
		
		final int a=0,
				b=1,
				c=2,
				d=3,
				e=4,
				f=5,
				g=6,
				h=7;
		
		int x = xy.getX();
		int y = xy.getY();
		
		int returnInt = 0;
		
		switch (count) {
		
//		从以x,y为轴心的 右下 开始
		
		case a:
			if( x+2<=X-1 && y+1<=Y-1 && chess[y+1][x+2]==0){
				x +=2;
				y +=1;
				returnInt = 1;
			}
			
			break;
			
		case b:
			if( x+1<=X-1 && y+2<=Y-1 && chess[y+2][x+1]==0){
				x +=1;
				y +=2;
				returnInt = 1;
			}
			
			break;
			
		case c:
			if( x-1>=0 && y+2<=Y-1 && chess[y+2][x-1]==0){
				x -=1;
				y +=2;
				returnInt = 1;
			}
			
			break;
			
		case d:
			if( x-2>=0 && y+1<=Y-1 && chess[y+1][x-2]==0){
				x -=2;
				y +=1;
				returnInt = 1;
			}
			
			break;
		
		case e:
			if( x-2>=0 && y-1>=0 && chess[y-1][x-2]==0){
				x -=2;
				y -=1;
				returnInt = 1;
			}
			
			break;
			
		case f:
			if( x-1>=0 && y-2>=0 && chess[y-2][x-1]==0){
				x -=1;
				y -=2;
				returnInt = 1;
			}
			
			break;
			
		case g:
			if( x+1<=X-1 && y-2>=0 && chess[y-2][x+1]==0){
				x +=1;
				y -=2;
				returnInt = 1;
			}
			
			break;
			
		case h:
			if( x+2<=X-1 && y-1>=0 && chess[y-1][x+2]==0){
				x +=2;
				y -=1;
				
				returnInt = 1;
			}
			break;
			
		default:
			break;
		}
		
		if(returnInt == 1){
			xy.setX(x);
			xy.setY(y);
			
			return 1;
		}

		return 0;
	}
	
	/**
	 * 打印棋盘
	 */
	public static void print(){
		for(int i=0;i<X;i++){
			for(int j=0;j<Y;j++){
				
				if(chess[i][j]<10)
					System.out.print(chess[i][j]+"  ");
				else
					System.out.print(chess[i][j]+" ");
				
			}
			System.out.println();
		}
		
	}
	
	/**
	 * 深度优先遍历棋盘
	 * @param x
	 * @param y
	 * @param tag
	 * @return
	 * (x,y)为位置坐标
	 * tag是标记变量,每走一步 tag+1。
	 */
	public static int TravelChessBoard(XY xy,int tag){
		
//		马在某个点有八种可能的方向,用来约束查找小于八种的变量
		Integer count = 0;
		
//		马所在位置是否可以再跳向下一个位置,0有,1无(条件:1,不出边界,2.没有走过)
		int haveNextXy = 0;
		
		int x = xy.getX();
		int y = xy.getY();
		
//		x是横轴,y是竖轴,左上角为0,0点,往右和往下递增
		chess[y][x] = tag;
		
//		最后一步,递归的终止条件
		if(X*Y == tag){
//			打印棋盘
			print();
			return 1;
		}
		
//		找到马的下一个可走坐标(x1,y1),如果找到为1,否则为0.
		haveNextXy = nextxy(xy, count);
		
		while( 0==haveNextXy && count<7){
			count ++;
			haveNextXy = nextxy(xy, count);
		}
		
		while(haveNextXy==1){
			if(TravelChessBoard(xy, tag+1)==1){
				return 1;
			}
			
//			回退后,把当前点也设置为回退后的位置
			xy.setX(x);
			xy.setY(y);
			
			count++;
			
//			找到马的下一个可走坐标(x1,y1),如果找到flag=1,否则为0.
			haveNextXy = nextxy(xy, count);
			
			while( 0==haveNextXy && count<7){
				count ++;
				haveNextXy = nextxy(xy, count);
			}
		}
		
//		回退
		if(haveNextXy==0){
			chess[y][x]=0;
			returnCount++;
		}
		
		return 0 ;
	}
	
	public static void main(String[] args) {
		long begin = System.currentTimeMillis();
		
//		马所在位置的坐标,x是横轴,y是竖轴,左上角为0,0点,往右和往下递增
		XY xy = new XY();
		xy.setX(1);
		xy.setY(0);
		
		if(TravelChessBoard(xy, 1)==0){
			System.out.println("马踏棋盘失败");
		}
		
		long time = System.currentTimeMillis()-begin;
		
		System.out.println("耗时"+time+"毫秒");
		System.out.println(returnCount);
	}
	
}


class XY{
	private int x;
	private int y;
	public int getX() {
		return x;
	}
	public void setX(int x) {
		this.x = x;
	}
	public int getY() {
		return y;
	}
	public void setY(int y) {
		this.y = y;
	}
	
	
}



结果:

如果从(0,0)开始的话



  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一段马踏棋盘算法Java 代码: ``` public class HorseChessboard { private int[][] chessboard; private int startX; private int startY; private int stepCount; private boolean finished; public HorseChessboard(int n) { this.chessboard = new int[n][n]; } public void printChessboard() { for (int[] row : chessboard) { for (int num : row) { System.out.print(num + "\t"); } System.out.println(); } } public void horseTravel(int x, int y) { chessboard[x][y] = ++stepCount; if (stepCount == chessboard.length * chessboard.length) { finished = true; return; } int[][] nextMoves = getNextMoves(x, y); for (int[] nextMove : nextMoves) { int nextX = nextMove[0]; int nextY = nextMove[1]; if (chessboard[nextX][nextY] == 0) { horseTravel(nextX, nextY); } } if (!finished) { chessboard[x][y] = 0; stepCount--; } } private int[][] getNextMoves(int x, int y) { int[][] nextMoves = new int[8][2]; int count = 0; if (canMove(x - 2, y - 1)) { nextMoves[count++] = new int[]{x - 2, y - 1}; } if (canMove(x - 1, y - 2)) { nextMoves[count++] = new int[]{x - 1, y - 2}; } if (canMove(x + 1, y - 2)) { nextMoves[count++] = new int[]{x + 1, y - 2}; } if (canMove(x + 2, y - 1)) { nextMoves[count++] = new int[]{x + 2, y - 1}; } if (canMove(x + 2, y + 1)) { nextMoves[count++] = new int[]{x + 2, y + 1}; } if (canMove(x + 1, y + 2)) { nextMoves[count++] = new int[]{x + 1, y + 2}; } if (canMove(x - 1, y + 2)) { nextMoves[count++] = new int[]{x - 1, y + 2}; } if (canMove(x - 2, y + 1)) { nextMoves[count++] = new int[]{x - 2, y + 1}; } return Arrays.copyOfRange(nextMoves, 0, count); } private boolean canMove(int x, int y) { return x >= 0 && x < chessboard.length && y >= 0 && y < chessboard.length; } public static void main(String[] args) { HorseChessboard horseChessboard = new HorseChessboard(8); horseChessboard.horseTravel(0, 0); horseChessboard.printChessboard(); } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值