【马踏棋盘问题解决方案】


package algothrim;

import org.jetbrains.annotations.NotNull;

import java.awt.*;
import java.util.ArrayList;

/**
 * 马踏棋盘类
 */
public class HorseChessBoard {

    Piece[][] chessBoard;//位置集合构成的棋盘
    Piece currentPiece;//当前所在位置
    int row;//棋盘的行数
    int list;//棋盘的列数

    /**构造方法
     * @param row         棋盘的行数
     * @param list        棋盘的列数
     * @param currentRow  当前所在的行
     * @param currentList 当前所在的列
     */
    public HorseChessBoard(int row, int list, int currentRow, int currentList) {
        this.row = row;
        this.list = list;
        chessBoard = new Piece[row][list];
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < list; j++) {
                chessBoard[i][j] = new Piece(i + 1, j + 1);
            }
        }
        currentPiece = new Piece(currentRow, currentList);
    }

    /**
     * @param piece 当前所在的位置
     * @return 可到达位置的集合
     */
    public ArrayList<Piece> reachable(Piece piece) {
        ArrayList<Piece> reachablePieces = new ArrayList<>();
        for (int i = 1; i <= row; i++) {
            for (int j = 1; j <= list; j++) {
                if ( Math.abs(i - piece.point.x) == 2 && Math.abs(j - piece.point.y) == 1 ) {
                    reachablePieces.add(new Piece(i, j));
                }
                if ( Math.abs(i - piece.point.x) == 1 && Math.abs(j - piece.point.y) == 2 ) {
                    reachablePieces.add(new Piece(i, j));
                }
            }
        }
        return reachablePieces;
    }

    /**
     * @return 判断棋盘是否走完
     */
    public Boolean isFinished() {
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < list; j++) {
                if ( !chessBoard[i][j].isVisited ) {
                    return false;
                }
            }
        }
        return true;
    }

    /**
     * @param pieceOfReachablePieces 可到达集合中的某个位置
     * @return 对应的在棋盘中的位置
     */
    public Piece getPieceFromChessBoard(@NotNull Piece pieceOfReachablePieces) {
        return chessBoard[pieceOfReachablePieces.point.x - 1][pieceOfReachablePieces.point.y - 1];
    }

    /**
     * @param currentPiece 当前所在位置
     * @param step         当前已走步数
     */
    public void horseChessBoard(@NotNull Piece currentPiece, int step) {

        getPieceFromChessBoard(currentPiece).step = step + 1;
        getPieceFromChessBoard(currentPiece).isVisited = true;
        if ( isFinished() ) {
            return;
        } else {
            ArrayList<Piece> reachablepieces = reachable(currentPiece);
            for (Piece piece : reachablepieces) {
                if ( !getPieceFromChessBoard(piece).isVisited ) {
                    horseChessBoard(getPieceFromChessBoard(piece), getPieceFromChessBoard(currentPiece).step);
                }
            }
        }
        if ( !isFinished() ) {
            getPieceFromChessBoard(currentPiece).step = 0;
            getPieceFromChessBoard(currentPiece).isVisited = false;
        }
    }

    /**
     * 如果能走完,打印每个位置分别是第几步
     * 如果不能走完,输出全0
     */
    public void show() {
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < list; j++) {
                System.out.print(chessBoard[i][j].step + "\t");
            }
            System.out.println();
        }
    }

    /**
     * 启动游戏
     */
    public void start() {
        horseChessBoard(currentPiece, 0);
    }

    /**
     * 位置类
     */
    public static class Piece {
        Point point = new Point();//位置
        Boolean isVisited = false;//是否已经走过
        int step;//初始值为0,如果走过是第几步

        public Piece(int x, int y) {
            point.x = x;
            point.y = y;
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值