-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainLesson5_3.java
More file actions
41 lines (31 loc) · 1.07 KB
/
MainLesson5_3.java
File metadata and controls
41 lines (31 loc) · 1.07 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
package ru.MylearnCh1J1L1;
public class MainLesson5_3 {
public static void main(String[] args) {
int[] queens = new int[8];
setPos(queens, 0);
for (int row = 0; row < queens.length; row++) {
for (int col = 0; col < queens.length; col++) {
if (queens[row] == col) System.out.print(" Ф ");
else System.out.print(" 0 ");
}
System.out.println();
}
}
public static boolean setPos(int[] array, int row) {
if (row == array.length) return true;
for (int i = 0; i < array.length; i++) {
boolean isValid = true;
for (int j = 0; j < row; j++) {
if (array[j] == i || array[j] == i - row + j || array[j] == i + row - j) {
isValid = false;
break;
}
}
if (isValid) {
array[row] = i;
if (setPos(array, row + 1)) return true;
}
}
return false;
}
}