N骑士的问题 [英] The N-knights problem

查看:117
本文介绍了N骑士的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi Sir,
 I am Java Learner, I would like to seek for help on my coding.
 The requirement is as below,
 The knights must visits the maximum number of board positions without attacking each other. The Knight can move in the shape of the letter, 'L', over two in one direction and then over one in a perpendicular direction. If the Knight rests at the square marked X.
 Please assist, thanks.





我尝试过:



我的编码,



What I have tried:

My coding,

Package knightstour;
 import java.util.*;
 public class KnightsTour {

 private static int board[][] = new int[8][8];
 private static int stepCounter = 1;

 public KnightsTour() {
 initBoard(board);
 tour(0,0);
 printSol(board);
 }
 public static void printSol(int[][] a) {
 for (int i = 0; i < a.length; i++) {
 for (int j = 0; j < a[i].length; j++) {
 if(a[i][j]>9){
 System.out.print(a[i][j] + " ");
 }else{
 System.out.print(a[i][j] + " ");
 }
 }
 System.out.println();
 }
 System.out.println();
 }
 public static void initBoard(int[][] a) {
 for (int i = 0; i < a.length; i++) {
 for (int j = 0; j < a[i].length; j++) {
 a[i][j] = -1;
 }
 }
 }
 public void tour(int x, int y) {
 if (((x < 0) || (x >= 8) || (y < 0) || (y >= 8)) || (board[x][y] != -1)) {
 return;
 } else {
 stepCounter++;
 board[x][y] = stepCounter;
 tour(x+2, y+1);
 tour(x+1, y-2);
 tour(x+1, y+2);
 tour(x-1, y+2);
 tour(x-2, y-1);
 tour(x-2, y+1);
 tour(x-1, y-2);
 tour(x+2, y-1); 
 }
 }
 public boolean spaceAvailable(int X, int Y) {
 }
 Public static void main(String[] args){
 new KnightsTour();
 }
 }

推荐答案

几件事:

1)如果你希望其他人阅读你的代码,写它以便于阅读。

这意味着缩进它:

Couple of things:
1) if you want other people to read your code, write it so it is easy to read.
That means indent it:
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
if(a[i][j]>9){
System.out.print(a[i][j] + " ");
}else{
System.out.print(a[i][j] + " ");
}
}
System.out.println();
}

不容易阅读。这是:

Is not easy to read. This is:

for (int i = 0; i < a.length; i++) {
    for (int j = 0; j < a[i].length; j++) {
        if(a[i][j]>9){
            System.out.print(a[i][j] + " ");
        }else{
            System.out.print(a[i][j] + " ");
        }
    }
    System.out.println();
}

因为缩进可以帮助你跟随什么去哪里以及括号匹配什么。

这也意味着不使用单个字符变量,因为它们更快为了您输入:例如上面代码中的a - 您可以使用 i j ,但是它们会更好,因为 y x 因为这使得它们正在做的更加明显:循环遍历行( y)和列(x)。



2)我们不做你的作业:这是有原因的。它就是为了让你思考你被告知的事情,并试着理解它。它也在那里,以便您的导师可以识别您身体虚弱的区域,并将更多的注意力集中在补救措施上。

并且该代码不会开始解决您给出的问题。它设置了板子,但问题的肉甚至没有开始!



自己动手,你可能会发现它并不像你那么困难想想!



如果您遇到特定问题,请询问相关问题,我们会尽力提供帮助。但是我们不打算为你做这一切!

Because the indentation helps you to "follow" what goes where and what bracket matches what.
That also means not using single character variables because they are quicker for your to type: "a" in the above code for example - you can get away with i and j, but they would be better as y and x because that makes it more obvious what they are doing: looping through rows (y) and columns (x).

2) We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.
And that code doesn't start to address the question you have been given. It sets up teh board, but the "meat" of the question isn't even started!

Try it yourself, you may find it is not as difficult as you think!

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!


如果你在这个网站上搜索了Q& A,你会找到一些解决方案,包括:编码挑战:棋子上的骑士 [ ^ ]
If you had search the Q&A on this site you would have found a few solutions including this: Coding challenge: a knight.on a chessboard[^]


这篇关于N骑士的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆