I am trying to know a bit more about the recursive but in some way I can not solve nightclub And I hope someone can tell my logic error.
class main {static int fsize = 5; // Board size (5 * 5) Static Intel board [] [] = New Int [FSICA] [FSIS]; Stable int [] move_x = {1, 2, 2, 1, -1, -2, -2, -1}; // possible moves (x-axis) static int [] move_y = {-2, -1, 1, 2, 2, 1, -1, -2}; // possible moves (y-axis) // x = current x coordinate / y = current y coordinate static zero solution (int move_number, int x, int y) {board [x] [y] = move_number; // Check whether the knight is on all the fields, if (move_number == ((fsize * fsize) - 1)) {for (int i = 0; i = 0 & amp; amp; (x + move_x [i]) & lt; fsize & (y + move_y [c] )> = 0 & amp; amp; (Y + move_y [c]) & lt; fsize) // Check whether the knight is on this field or not (-1 = is not here) if (board [x + move_x [I]] [y + move_y [c]] == -1) {System.out.println ("move:" + move_number + "\ n"); // Resolve next field (move_number + 1, (x + _x [i]), (y + move_y [c])); }}}} // A valid conduction board could not be found [x] [y] = -1; {} (Constant = 0; c & lt; fsize; c ++) {for board [i] [c] (constant =); For {inti i = 0; I & lt; Fsize; I ++} = -1; }} Solution (0, 0, 0); }}
Edit: Hope it's okay. I tried to run this program but could not get more than 22 valid tricks.
Fix your code by doing two things I was able to:
- Single-level loop 8 possibilities for the next level check
- You only have two nested loops here for
(int i = 0; i & lt; 8; i ++)
Why are you - At each level there are only 8 moves, not 64
- You only have two nested loops here for
-
create board
<>[y] = -1; Last statement if
terms - out of this level in
solution
- Before returning
you is to return
board [x] [y] = invoice number;
> After fixing those people, your homework is done, right, ends, congratulations!
in pseudocode
Right now, you have it:
Resolve constant void (int move_number, int x, int y) {Board [x] [y] = move_number; If (done) {print; } Else {for (int i = 0; i <8; i ++) {for (int c = 0; c & lt; 8; c ++) {ATTEMPT_MOVE (i, c); // can not understand !!! }} Board [x] [y] = -1; // it is not here !!! }}
To fix this code, you have to:
Resolve the static void (int lev_number, int x, int y ) {Board [x] [y] = move_number; If (done) {print; } Else {for (int i = 0; i & lt; 8; i ++) {ATTEMPT_MOVE (i); // now more understandable! }} Board [x] [y] = -1; // The assignment in the first row must be undone! }
Additional suggestion
- Divide the locker into auxiliary methods, i.e. board printing, and valid coordinate check
Comments
Post a Comment