最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java實(shí)現(xiàn)馬踏棋盤游戲

 更新時間:2022年02月14日 11:38:32   作者:會釣貓的魚  
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)馬踏棋盤游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

用java實(shí)現(xiàn)馬踏棋盤游戲算法,供大家參考,具體內(nèi)容如下

在4399小游戲中有這樣一個游戲

這是代碼實(shí)現(xiàn)

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;
? ? ? ? }
? ? }
? ? //判斷當(dāng)前棋子下一個可以走的所有位置數(shù)組
? ? 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;
? ? }

? ? //使用貪心算法提高算法運(yùn)行速度
? ? 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;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? });
? ? }
}

然后照著步驟一步一步下就可以了

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

嵊州市| 苍梧县| 满洲里市| 都兰县| 江华| 额尔古纳市| 武川县| 左云县| 巩留县| 重庆市| 德令哈市| 西林县| 霍邱县| 长阳| 巩义市| 沭阳县| 南京市| 房产| 分宜县| 木里| 西平县| 望江县| 鹤庆县| 松溪县| 泸定县| 襄垣县| 镶黄旗| 竹山县| 安仁县| 柯坪县| 无锡市| 海阳市| 新宁县| 芒康县| 荃湾区| 广德县| 原阳县| 茶陵县| 贵州省| 资中县| 察雅县|