-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChess.java
More file actions
82 lines (74 loc) · 2.27 KB
/
Copy pathChess.java
File metadata and controls
82 lines (74 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import java.lang.Math;
import java.util.Scanner;
public class Chess {
private int[][] board;
private int boardSize;
private int dr, dc;
private int tile;
public Chess() {
board = new int[1][1];
dr = 0; dc = 0; boardSize = 0;
}
public Chess(int tr, int tc, int s) {
int n;
n = (int) Math.pow(2, s);
if (n <= tr || n <= tc) {
System.out.println("初始化参数错误!");
} else {
board = new int[n][n];
dr = tr; dc = tc;
boardSize = s;
}
}
public void Print() {
for (int i = 0; i < Math.pow(2, this.boardSize); i++) {
for (int j = 0; j < Math.pow(2, this.boardSize); j++) {
System.out.print(String.format("%3d|", this.board[i][j]));
}
System.out.println();
}
}
public static void main(String[] args) {
/*
Chess c1 = new Chess(5, 5, 3);
*/
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
int y = sc.nextInt();
int k = sc.nextInt();
Chess c1 = new Chess(x, y, k);
c1.chessBoard(0, 0, c1.dc, c1.dr,
(int)Math.pow(2, c1.boardSize));
c1.Print();
}
public void chessBoard(int tr, int tc, int dr, int dc, int size) {
if (size == 1) {
return ;
}
int t = tile++, s = size / 2;
if (dr < tr + s && dc < tc + s) {
chessBoard(tr, tc, dr, dc, s);
} else {
board[tr + s - 1][tc + s - 1] = t;
chessBoard(tr, tc, tr + s - 1, tc + s - 1, s);
}
if (dr < tr + s && dc >= tc + s) {
chessBoard(tr, tc + s, dr, dc, s);
} else {
board[tr + s - 1][tc + s] = t;
chessBoard(tr, tc + s, tr + s - 1, tc + s, s);
}
if (dr >= tr + s && dc < tc + s) {
chessBoard(tr + s, tc, dr, dc, s);
} else {
board[tr + s][tc + s - 1] = t;
chessBoard(tr + s, tc, tr + s, tc + s - 1, s);
}
if (dr >= tr + s && dc >= tc + s) {
chessBoard(tr + s, tc + s, dr, dc, s);
} else {
board[tr + s][tc + s] = t;
chessBoard(tr + s, tc + s, tr + s, tc + s, s);
}
}
}