java实现马踏棋盘游戏

 更新时间:2022年02月14日 11:38:32   作者:会钓猫的鱼  
这篇文章主要为大家详细介绍了java实现马踏棋盘游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

用java实现马踏棋盘游戏算法,供大家参考,具体内容如下

在4399小游戏中有这样一个游戏

这是代码实现

package com.HorseChess;

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

public class HorseChess {
    private static int X;
    private static int Y;
    private static boolean visited[];
    private static boolean finished;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入行:");
        X = sc.nextInt();
        System.out.println("请输入列:");
        Y = sc.nextInt();
        System.out.println("请输入棋子所在行:");
        int row = sc.nextInt();
        System.out.println("请输入棋子所在列:");
        int column = sc.nextInt();
        int [][] chessboard = new int[X][Y];
        visited = new boolean[X*Y];
        traverchess(chessboard,row-1,column-1,1);
        for(int[] rows : chessboard){
            for (int step : rows){
                System.out.print(step + "\t");
            }
            System.out.println();
        }
    }

    public static void traverchess(int[][] chessboard,int row,int column,int step){
        chessboard[row][column] = step;
        visited[row * X+column] = true;
        ArrayList<Point> ps = next(new Point(column,row));
        sort(ps);
        while (!ps.isEmpty()){
            Point p = ps.remove(0);
            if(!visited[p.y*X+p.x]){
                traverchess(chessboard,p.y,p.x,step+1);
            }
        }

        if(step<X*Y&&!finished){
            chessboard[row][column] = 0;
            visited[row * X + column] = false;
        }
        else {
            finished = true;
        }
    }
    //判断当前棋子下一个可以走的所有位置数组
    public static ArrayList<Point> next(Point curpoint){
        ArrayList<Point> ps = new ArrayList<Point>();
        Point p1 = new Point();
        if((p1.x = curpoint.x - 2)>=0&&(p1.y = curpoint.y - 1)>=0){
            ps.add(new Point(p1));
        }
        if((p1.x = curpoint.x - 1)>=0&&(p1.y = curpoint.y - 2)>=0){
            ps.add(new Point(p1));
        }
        if((p1.x = curpoint.x + 1)< X && (p1.y = curpoint.y - 2)>=0){
            ps.add(new Point(p1));
        }
        if((p1.x = curpoint.x + 2)< X && (p1.y = curpoint.y - 1)>=0){
            ps.add(new Point(p1));
        }
        if((p1.x = curpoint.x + 2)<X&&(p1.y = curpoint.y + 1)<Y){
            ps.add(new Point(p1));
        }
        if((p1.x = curpoint.x + 1)<X&&(p1.y = curpoint.y + 2)<Y){
            ps.add(new Point(p1));
        }
        if((p1.x = curpoint.x - 1)>=0&&(p1.y = curpoint.y + 2)<Y){
            ps.add(new Point(p1));
        }
        if((p1.x = curpoint.x - 2)>=0&&(p1.y = curpoint.y + 1)<Y){
            ps.add(new Point(p1));
        }
        return ps;
    }

    //使用贪心算法提高算法运行速度
    public static void sort(ArrayList<Point> ps){
        ps.sort(new Comparator<Point>() {
            @Override
            public int compare(Point o1, Point o2) {
                int count1 = next(o1).size();
                int count2 = next(o2).size();
                if(count1<count2){
                    return  -1;
                }else if (count1 == count2){
                    return 0;
                }
                else {
                    return  1;
                }
            }
        });
    }
}

然后照着步骤一步一步下就可以了

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • 学习SpringBoot容器功能及注解原理

    学习SpringBoot容器功能及注解原理

    这篇文章主要介绍了学习SpringBoot容器功能及注解原理,文中通过详细的代码示例对SpringBoot容器功能及注解原理进行了解析,有需要的朋友可以借鉴参考下
    2021-09-09
  • SpringBoot使用Thymeleaf自定义标签的实例代码

    SpringBoot使用Thymeleaf自定义标签的实例代码

    这篇文章主要介绍了SpringBoot使用Thymeleaf自定义标签的实例代码,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-09-09
  • Springboot使用jxls实现excel模板导出excel方式

    Springboot使用jxls实现excel模板导出excel方式

    这篇文章主要介绍了Springboot使用jxls实现excel模板导出excel方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-08-08
  • Java中如何对字符串进行utf-8编码

    Java中如何对字符串进行utf-8编码

    这篇文章主要介绍了Java中如何对字符串进行utf-8编码问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-04-04
  • Spring中HandlerMapping接口源码详解

    Spring中HandlerMapping接口源码详解

    这篇文章主要介绍了Spring中HandlerMapping接口源码详解,RequestMappingHandlerMapping类就是实现此接口并将容器中所有的控制器的RequestMappingInfo请求和HandlerMethod注册到内存之中,需要的朋友可以参考下
    2023-11-11
  • springboot中的dockerfile使用

    springboot中的dockerfile使用

    这篇文章主要介绍了springboot中的dockerfile使用,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-12-12
  • 基于kafka实现Spring Cloud Bus消息总线

    基于kafka实现Spring Cloud Bus消息总线

    消息总线是一种通信工具,可以在机器之间互相传输消息、文件等,这篇文章主要介绍了如何利用kafka实现SpringCloud Bus消息总线,感兴趣的可以学习一下
    2022-04-04
  • Java使用正则表达式截取重复出现的XML字符串功能示例

    Java使用正则表达式截取重复出现的XML字符串功能示例

    这篇文章主要介绍了Java使用正则表达式截取重复出现的XML字符串功能,涉及java针对xml字符串及指定格式字符串的正则匹配相关操作技巧,需要的朋友可以参考下
    2017-08-08
  • 基于spring+springmvc+hibernate 整合深入剖析

    基于spring+springmvc+hibernate 整合深入剖析

    这篇文章主要介绍了于spring+springmvc+hibernate整合实例,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-10-10
  • Mybatis执行流程、缓存原理及相关面试题汇总

    Mybatis执行流程、缓存原理及相关面试题汇总

    最近刚学完MyBatis,趁着大好机会,总结一下它的执行流程,面试也爱问这个,下面这篇文章主要给大家介绍了关于Mybatis执行流程、缓存原理及相关面试题的相关资料,需要的朋友可以参考下
    2022-02-02

最新评论