马踏棋盘java算法完整版

马踏棋盘很好实现,但有时运行起来特别慢,还可能出不来结果,在这里要用到贪心算法来优化,
即找出最难走的路径,也就是下下步可下棋的位置最少。
        下面给出该算法完整代码:
/*
     * 马踏棋盘问题:(贪婪法求解)
     * 棋盘有64个位置,“日”字走法,刚好走满整个棋盘
     */

    //下一个走法的方向类
    class Direction{
        int x;
        int y;
        int wayOutNum;
    }

    public class Hores_chessboard_1 {
        static final int[] dx = { -2, -1, 1, 2, 2, 1, -1, -2 }; // x方向的增量  
        static final int[] dy = { 1, 2, 2, 1, -1, -2, -2, -1 }; // y方向的增量  
        static final int N = 8;     
        static int[][] chessboard = new int[N][N]; // 棋盘 

    /**
     * 
     * @param nami
     * @param x,y为棋子的位置
     * @return 如果棋子的位置不合法,则返回一个大于8的数。
     * 否则返回棋子的下个出路的个数
     */
    static int wayOut(int x, int y){        
        int count = 0;
        int tx, ty, i;
        //判断是否超出棋盘边界,该位置是否已经下过
        if(x<0 || x>7 || y<0 || y>7 || chessboard[x][y]!=0){
            return 9;
        }
        for(i=0; i<N; i++){
            tx = x+dx[i];
            ty = y+dy[i];
            //如果棋子的下个出路可行,则出路数自加一次
            if(tx>-1 && tx<8 && ty>-1 && ty<8 && chessboard[tx][ty]==0)
                count++;
        }
        return count;
    }

    /**
     * 按照棋子的下个出路的个数从低到高排序
     * @param next 棋子的八个位置的数组
     */
    static void sort(Direction[] next){
        int i, j, index;
        Direction temp = null;
        //这里用的选择排序
        for(i=0; i<N; i++){
            index = i;
            for(j=i+1; j<N; j++){
                if(next[index].wayOutNum > next[j].wayOutNum)
                    index = j;
            }
            if(i != index){
                temp = next[i];
                next[i] = next[index];
                next[index] = temp;
            }
        }
    }

    static void Move(int x, int y, int step){
        int i, j;
        int tx, ty;
        //如果step==64,则说明每个棋格都走到了,现在只需要打印结果就完了
        if(step == N*N){
            for(i=0; i<N; i++){
                for(j=0; j<N; j++){
                    System.out.printf("%3d", chessboard[i][j]);
                }
                System.out.println();
            }
            System.exit(0);
        }

        //下一个棋子的N个位置的数组
        Direction[] next = new Direction[N];

        for(i=0; i<N; i++){
            Direction temp = new Direction();
            temp.x = x+dx[i];
            temp.y = y+dy[i];
            next[i] = temp;
            //循环得到下个棋子N处位置的下个出路的个数
            next[i].wayOutNum = wayOut(temp.x, temp.y);
        }

        //配合贪婪算法,按下个棋子的下个出路数排序后,next[0]就是下个出路数最少的那个
        sort(next);

        for(i=0; i<N; i++){
            tx = next[i].x;
            ty = next[i].y;
            chessboard[tx][ty] = step;
            Move(tx, ty, step+1);
            /*如果上面Move()往下一步走不通,则回溯到这里
            重置chessboard[tx][ty]为0,接着i++,又循环...... */
            chessboard[tx][ty] = 0;
        }
    }

    public static void main(String[] args) {
        int i, j;
        //初始化棋盘
        for(i=0; i<8; i++){
            for(j=0; j<8; j++){
                chessboard[i][j] = 0;
            }
        }
        System.out.println("请输入棋子开始位置(0-7):");
        Scanner sc = new Scanner(System.in);
        int x = sc.nextInt();
        int y = sc.nextInt();
        //第一步不用比较,赋值第一步
        chessboard[x][y] = 1;
        Move(x, y, 2);      
    }
}

这里给出运算结果:
这里写图片描述

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
以下是一段棋盘算法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(); } } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值