1. Description of the problem:
In chess, the horse goes on the day, the user enters the starting position of the chessboard from 1-8, the output starts from this point, the horse goes through the whole chessboard of the various schemes, and outputs the number of programs
2. Enter the style:
Please enter the starting position of the Checkerboard Horse:
1 1
3. Output style:
1 20 11 14 3 6 9 16
12 23 2 19 10 15 4 7
21 30 13 24 5 8 17 26
32 35 22 29 18 25 54 45
39 48 31 34 55 46 27 60
36 33 38 47 28 59 44 53
49 40 63 56 51 42 61 58
64 37 50 41 62 57 52 43
1 20 11 14 3 6 9 16
12 23 2 19 10 15 4 7
21 30 13 24 5 8 17 26
32 35 22 29 18 25 57 45
39 63 31 34 56 46 27 51
36 33 38 47 28 52 44 58
62 40 64 55 60 42 50 53
64 37 61 41 49 54 59 43
.......
4. Problem-Solving ideas:
We use a two-dimensional array to simulate the direction of the horse walking, through the function move (x, y) to achieve the horse walk. If the Board X<0 | | x>7 | | y<0 | | Y>7 said that x, Y is not inside the board, then return directly. If the chessboard Qipan[x][y]! = 0 indicates that it has gone through, and also directly
Return. Another step, step, records the number of steps of the board, from 11 to 64, and if step is 64, the checkerboard is output and count++, and not to 64 recursively calls the move (x, y) function. Finally go through a scheme and then backtrack.
5. code example:
Import Java.util.Scanner;
public class chess{
static int weizhi[][] = {{ -2,1},{-2,-1},{-1,2},{-1,-2},{1,2},{1,-2},{2,1},{2,-1}};
static int step = 1;
static int qipan[][] = new INT[8][8];
static int count = 0;//All the way through the board
public static void Main (string[] args) {
Initialize the board first
for (int i=0;i<8;i++) {
for (int j=0;j<8;j++) {
QIPAN[I][J] = 0;
}
}
System.out.println ("Please enter the starting position of the Checkerboard Horse:");
Scanner SCN = new Scanner (system.in);
int x = Scn.nextint ();
int y = Scn.nextint ();
x--; The checkerboard input starts at 1, 1, and the starting position of the array starts from 0, 0
y--;
Move (x, y);
}
public static void Move (int x,int y) {
int next_x;
int next_y;
X, y crossed out.
if (x<0 | | x>7 | | y<0 | | y>7) {
Return
}
It means there's already a horse on the board.
if (qipan[x][y]! = 0) {
Return
}
Qipan[x][y] = step;
step++;
If step is greater than 64, output and count directly
if (step>64) {
for (int i=0;i<8;i++) {
for (int j=0;j<8;j++) {
System.out.printf ("%5d", Qipan[i][j]);
}
System.out.println ();
}
System.out.println ("===============");
count++;
Return
}else{
for (int i=0;i<8;i++) {//Mark to walk in 8 directions
next_x = x + weizhi[i][0];
next_y = y + weizhi[i][1];
Move (next_x,next_y);
}
}
Qipan[x][y] = 0;
Step--; Backtracking
}
}
Java implements the horse-riding board problem