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;
                }
            }
        });
    }
}

在这里插入图片描述
然后照着步骤一步一步下就可以了

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值