java如何实现马踏棋盘

发布时间:2022-02-14 14:37:13 作者:小新
来源:亿速云 阅读:138

这篇文章给大家分享的是有关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);      
    }
}

这里给出运算结果:

java如何实现马踏棋盘

感谢各位的阅读!关于“java如何实现马踏棋盘”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

推荐阅读:
  1. JAVA自己实现LinkedList
  2. java怎么实现分页

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java

上一篇:vue3.0响应式函数原理是什么

下一篇:js事件流、事件委托与事件阶段的示例分析

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》