/
DivideAndConquer.java
113 lines (96 loc) · 2.99 KB
/
DivideAndConquer.java
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
* Copyright (c) 2018. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
* Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
* Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
* Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
* Vestibulum commodo. Ut rhoncus gravida arcu.
*/
package com.example.god.androidmore.algorithm.other;
public class DivideAndConquer {
/**
* 球队比赛排列问题
* @param num
*/
public static void treamQuestion(int num) {
int[][] datas = new int[num][num];
eightTream(datas, datas.length);
printResult(datas);
}
public static void eightTream(int[][] datas, int num) {
if (num == 1) {
datas[0][0] = 1;
} else {
int m = num / 2;
eightTream(datas, m);
for (int i = 0; i < m; i++) {
for (int j = m; j < num; j++) {
datas[i][j] = datas[i][j - m] + m;
}
}
for (int i = m; i < num; i++) {
for (int j = 0; j < m; j++) {
datas[i][j] = datas[i - m][j] + m;
}
}
for (int i = m; i < num; i++) {
for (int j = m; j < num; j++) {
datas[i][j] = datas[i - m][j - m];
}
}
}
}
// L型骨牌问题
static int type = 0;
public static void chessBoardQuestion(int num) {
int[][] datas = new int[num][num];
chessBoard(1, 0, 0, 0, datas.length, datas);
printResult(datas);
}
public static void chessBoard(int spcR, int spcC, int starR, int starC, int num, int[][] data) {
if (num != 1) {
int m = num / 2;
type = type % 9 + 1;
int n = type;
//左上角
if (spcR < starR + m && spcC < starC + m) {
chessBoard(spcR, spcC, starR, starC, m, data);
} else {
data[starR + m - 1][starC + m - 1] = n;
chessBoard(starR + m - 1, starC + m - 1, starR, starC, m, data);
}
//左下角
if (spcR < starR + m && spcC >= starC + m) {
chessBoard(spcR, spcC, starR, starC + m, m, data);
} else {
data[starR + m - 1][starC + m] = n;
chessBoard(starR + m - 1, starC + m, starR, starC + m, m, data);
}
//右上角
if (spcR >= starR + m && spcC < starC + m) {
chessBoard(spcR, spcC, starR + m, starC, m, data);
} else {
data[starR + m][starC + m - 1] = n;
chessBoard(starR + m, starC + m - 1, starR + m, starC, m, data);
}
//右下角
if (spcR >= starR + m && spcC >= starC + m) {
chessBoard(spcR, spcC, starR + m, starC + m, m, data);
} else {
data[starR + m][starC + m] = n;
chessBoard(starR + m, starC + m, starR + m, starC + m, m, data);
}
}
}
/**
* 打印结果
* @param datas
*/
public static void printResult(int datas[][]) {
for (int i = 0; i < datas.length; i++) {
for (int j = 0; j < datas.length; j++) {
System.out.print(datas[i][j]);
}
System.out.print("\n");
}
}
}