-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathL4Q1.java
52 lines (48 loc) · 1.22 KB
/
L4Q1.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
public class L4Q1{
static boolean isAttacked(int[] cols, int tryrow) {
int row, col = cols[tryrow];
for (int prev = 1; prev <= tryrow; prev++) {
row = cols[tryrow - prev];
if (row==col || row==col-prev || row==col+prev) return true;
}
return false;
}
public static int getThisSolution(int[][] table, int[] cols) {
int max=0;
for (int i=0; i<8; ++i){
for (int j=0; j<8; ++j) {
if ((cols[i] == j))max+=table[i][j];
}
}
return max;
}
public static int getOptimal(int[] cols, int col, int aux, int[][] table, int max){
cols[0] = -1;
while (col >= 0) {
do cols[col]++; while ((cols[col] < 8) && isAttacked(cols, col));
if (cols[col] < 8) {
if (col < 7) cols[++col] = -1;
else {
aux = getThisSolution(table, cols);
if (aux>max) max=aux;
}
} else col--;
}
return max;
}
public static void main(String[] args) {
Arquivo arq = new Arquivo("L4Q1E2.in", "L4Q1.out");
int[] cols = new int[8];
int[][] table = new int[8][8];
int col, max, aux;
while (!arq.isEndOfFile()){
for (int i=0; i<8; ++i){
for (int j=0; j<8; ++j){
table[i][j] = arq.readInt();
}
}
col=0; max=0; aux=0; max=getOptimal(cols, col, aux, table, max);
arq.println(max);
}
}
}