欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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)文章

  • Jmeter JDBC請(qǐng)求常見(jiàn)問(wèn)題解決方案

    Jmeter JDBC請(qǐng)求常見(jiàn)問(wèn)題解決方案

    這篇文章主要介紹了Jmeter JDBC請(qǐng)求常見(jiàn)問(wèn)題解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-04-04
  • Matplotlib可視化之自定義顏色繪制精美統(tǒng)計(jì)圖

    Matplotlib可視化之自定義顏色繪制精美統(tǒng)計(jì)圖

    matplotlib提供的所有繪圖都帶有默認(rèn)樣式.雖然這可以進(jìn)行快速繪圖,但有時(shí)可能需要自定義繪圖的顏色和樣式,以對(duì)繪制更加精美、符合審美要求的圖像.matplotlib的設(shè)計(jì)考慮到了此需求靈活性,很容易調(diào)整matplotlib圖形的樣式,需要的朋友可以參考下
    2021-06-06
  • Spring整合Mybatis具體代碼實(shí)現(xiàn)流程

    Spring整合Mybatis具體代碼實(shí)現(xiàn)流程

    這篇文章主要介紹了Spring整合Mybatis實(shí)操分享,文章首先通過(guò)介紹Mybatis的工作原理展開(kāi)Spring整合Mybatis的詳細(xì)內(nèi)容,需要的小伙伴可以參考一下
    2022-05-05
  • 郵件收發(fā)原理你了解嗎? 郵件發(fā)送基本過(guò)程與概念詳解(一)

    郵件收發(fā)原理你了解嗎? 郵件發(fā)送基本過(guò)程與概念詳解(一)

    你真的了解郵件收發(fā)原理嗎?這篇文章主要為大家詳細(xì)介紹了郵件發(fā)送基本過(guò)程與概念,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-10-10
  • Java算法實(shí)現(xiàn)調(diào)整數(shù)組順序使奇數(shù)位于偶數(shù)之前的講解

    Java算法實(shí)現(xiàn)調(diào)整數(shù)組順序使奇數(shù)位于偶數(shù)之前的講解

    今天小編就為大家分享一篇關(guān)于Java算法實(shí)現(xiàn)調(diào)整數(shù)組順序使奇數(shù)位于偶數(shù)之前的講解,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2019-01-01
  • 一種類似JAVA線程池的C++線程池實(shí)現(xiàn)方法

    一種類似JAVA線程池的C++線程池實(shí)現(xiàn)方法

    線程池(thread pool)是一種線程使用模式。線程過(guò)多或者頻繁創(chuàng)建和銷(xiāo)毀線程會(huì)帶來(lái)調(diào)度開(kāi)銷(xiāo),進(jìn)而影響緩存局部性和整體性能。這篇文章主要介紹了一種類似JAVA線程池的C++線程池實(shí)現(xiàn)方法,需要的朋友可以參考下
    2019-07-07
  • java使用DOM4J對(duì)XML文件進(jìn)行增刪改查操作

    java使用DOM4J對(duì)XML文件進(jìn)行增刪改查操作

    這篇文章主要為大家詳細(xì)介紹了java使用DOM4J對(duì)XML文件進(jìn)行增刪改查操作,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-12-12
  • Java中的hashcode方法介紹

    Java中的hashcode方法介紹

    這篇文章主要介紹了Java中的hashcode方法介紹,還是比較不錯(cuò)的,這里分享給大家,供需要的朋友參考。
    2017-11-11
  • java?串口工具jSerialComm示例詳解

    java?串口工具jSerialComm示例詳解

    這篇文章主要介紹了java?串口工具jSerialComm,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-06-06
  • 詳解Spring的@Value作用與使用場(chǎng)景

    詳解Spring的@Value作用與使用場(chǎng)景

    這篇文章主要介紹了詳解Spring的@Value作用與使用場(chǎng)景,Spring為大家提供許多開(kāi)箱即用的功能,@Value就是一個(gè)極其常用的功能,它能將配置信息注入到bean中去,需要的朋友可以參考下
    2023-05-05

最新評(píng)論