Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write the code of Java to realize the algorithm of horse pedal board game?

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how to write the code of the horse pedal board game algorithm realized by Java". In the daily operation, I believe that many people have doubts about how to write the code of the horse pedal board game algorithm in Java. The editor has consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for everyone to answer the doubt that "how to write the code of the horse pedal board game algorithm in Java". Next, please follow the editor to study!

You can make the chessboard smaller for the test. 8x8 is really slow.

Import java.util.Arrays;/** * Knight Tour problem * @ author LM_Code * @ create 2019-03-17-18:57 * / public class KnightProblem {static final int SIZE = 8 stroke / set the number of rows and columns of the chessboard > = 5 static final int [] [] A = new int [SIZE] [SIZE]; / / initialize the chessboard. All values in the array default to 0 static final int [] NEXT= new int [] {1,2}. / / to set the next step of the horse, use an array with a space of 2 to replace the xMagol y coordinate public static void main (String [] args) {/ / to determine whether this point can walk the entire chessboard if (method (NEXT, 1) {/ / can, then output the chessboard trajectory for (int I = 0; I)

< A.length; i++) { System.out.println(Arrays.toString(A[i])); } }else{//不能,提示无解 System.out.println("此起点无解"); } } //传入下一步NEXT,和并表明下一步是第几步tag,返回此点是否能走完棋盘(有解) public static boolean method(int[] NEXT, int tag){ int[] current = new int[]{NEXT[0], NEXT[1]};//将当前步存入本次方法调用的局部变量 A[current[0]][current[1]] = tag;//把马跳到当前位置,并标记为是第几步 // 如果是最后一步,递归结束 if(tag == SIZE*SIZE){ return true; } //如果不是最后一步,下一步有8中可能 for (int i = 0; i < 8; i++) { //下一步的第i种情况是否可走 if(canGo(current, i)){//如果可以走,继续递归 //判断此时的下一步,是否能走完棋盘 if(method(NEXT, tag+1)){//能,返回true,递归结束 return true; } //此时的下一步不能走完棋盘,则继续寻找第i+1种情况的下一步是否有解 } //此时的下一步无解,则寻找第i+1种情况是否有解 } //如果当前步无法走完棋盘(无解) A[current[0]][current[1]] = 0;//回溯:撤销当前步,当前步赋值为0 return false;//返回false,回到上一步,表明此步无解 } //判断下一步是否能走,下一步有8中情况0-7,传入当前步arr,判断是否有第count种情况的下一步 public static boolean canGo(int[] arr,int count){ switch (count){ case 0 : if(arr[0]-1>

= 0&&arr [1] + 2=0&&arr [1] + 1

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report