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

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

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

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

在4399小游戲中有這樣一個(gè)游戲

這是代碼實(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("請(qǐng)輸入行:");
? ? ? ? X = sc.nextInt();
? ? ? ? System.out.println("請(qǐng)輸入列:");
? ? ? ? Y = sc.nextInt();
? ? ? ? System.out.println("請(qǐng)輸入棋子所在行:");
? ? ? ? int row = sc.nextInt();
? ? ? ? System.out.println("請(qǐng)輸入棋子所在列:");
? ? ? ? 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)前棋子下一個(gè)可以走的所有位置數(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;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? });
? ? }
}

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

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

相關(guān)文章

  • 學(xué)習(xí)SpringBoot容器功能及注解原理

    學(xué)習(xí)SpringBoot容器功能及注解原理

    這篇文章主要介紹了學(xué)習(xí)SpringBoot容器功能及注解原理,文中通過(guò)詳細(xì)的代碼示例對(duì)SpringBoot容器功能及注解原理進(jìn)行了解析,有需要的朋友可以借鑒參考下
    2021-09-09
  • SpringBoot使用Thymeleaf自定義標(biāo)簽的實(shí)例代碼

    SpringBoot使用Thymeleaf自定義標(biāo)簽的實(shí)例代碼

    這篇文章主要介紹了SpringBoot使用Thymeleaf自定義標(biāo)簽的實(shí)例代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-09-09
  • Springboot使用jxls實(shí)現(xiàn)excel模板導(dǎo)出excel方式

    Springboot使用jxls實(shí)現(xiàn)excel模板導(dǎo)出excel方式

    這篇文章主要介紹了Springboot使用jxls實(shí)現(xiàn)excel模板導(dǎo)出excel方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • Java中如何對(duì)字符串進(jìn)行utf-8編碼

    Java中如何對(duì)字符串進(jìn)行utf-8編碼

    這篇文章主要介紹了Java中如何對(duì)字符串進(jìn)行utf-8編碼問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • Spring中HandlerMapping接口源碼詳解

    Spring中HandlerMapping接口源碼詳解

    這篇文章主要介紹了Spring中HandlerMapping接口源碼詳解,RequestMappingHandlerMapping類(lèi)就是實(shí)現(xiàn)此接口并將容器中所有的控制器的RequestMappingInfo請(qǐng)求和HandlerMethod注冊(cè)到內(nèi)存之中,需要的朋友可以參考下
    2023-11-11
  • springboot中的dockerfile使用

    springboot中的dockerfile使用

    這篇文章主要介紹了springboot中的dockerfile使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • 基于kafka實(shí)現(xiàn)Spring Cloud Bus消息總線(xiàn)

    基于kafka實(shí)現(xiàn)Spring Cloud Bus消息總線(xiàn)

    消息總線(xiàn)是一種通信工具,可以在機(jī)器之間互相傳輸消息、文件等,這篇文章主要介紹了如何利用kafka實(shí)現(xiàn)SpringCloud Bus消息總線(xiàn),感興趣的可以學(xué)習(xí)一下
    2022-04-04
  • Java使用正則表達(dá)式截取重復(fù)出現(xiàn)的XML字符串功能示例

    Java使用正則表達(dá)式截取重復(fù)出現(xiàn)的XML字符串功能示例

    這篇文章主要介紹了Java使用正則表達(dá)式截取重復(fù)出現(xiàn)的XML字符串功能,涉及java針對(duì)xml字符串及指定格式字符串的正則匹配相關(guān)操作技巧,需要的朋友可以參考下
    2017-08-08
  • 基于spring+springmvc+hibernate 整合深入剖析

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

    這篇文章主要介紹了于spring+springmvc+hibernate整合實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • Mybatis執(zhí)行流程、緩存原理及相關(guān)面試題匯總

    Mybatis執(zhí)行流程、緩存原理及相關(guān)面試題匯總

    最近剛學(xué)完MyBatis,趁著大好機(jī)會(huì),總結(jié)一下它的執(zhí)行流程,面試也愛(ài)問(wèn)這個(gè),下面這篇文章主要給大家介紹了關(guān)于Mybatis執(zhí)行流程、緩存原理及相關(guān)面試題的相關(guān)資料,需要的朋友可以參考下
    2022-02-02

最新評(píng)論

客服| 那坡县| 河西区| 台南市| 呼和浩特市| 于都县| 长泰县| 蕉岭县| 卓尼县| 屏东市| 澜沧| 静海县| 濉溪县| 微山县| 南皮县| 平安县| 高安市| 长海县| 富蕴县| 闽清县| 夏河县| 逊克县| 康定县| 五原县| 嘉黎县| 崇明县| 淅川县| 台南县| 濉溪县| 桐梓县| 红桥区| 吕梁市| 麻城市| 正安县| 扶风县| 彰化县| 绍兴县| 东至县| 南平市| 建湖县| 准格尔旗|