-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay31s1.java
More file actions
112 lines (94 loc) · 3.46 KB
/
Day31s1.java
File metadata and controls
112 lines (94 loc) · 3.46 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
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
import java.util.Scanner;
public class Day31s1 {
public static void main(String[] args) {
EightQueensSolver solver = new EightQueensSolver();
solver.solve();
}
}
class EightQueensSolver {
private static final int SIZE = 8;
private int[] queens; // queens[i] = j, i. satırdaki vezirin j. sütunda olduğunu gösterir
private int solutionCount;
private Scanner scanner;
public EightQueensSolver() {
this.queens = new int[SIZE];
this.solutionCount = 0;
this.scanner = new Scanner(System.in);
}
public void solve() {
System.out.println("8 Vezir Bulmacası Çözücüsüne Hoş Geldiniz!");
System.out.println("Bu program, 8 vezirin satranç tahtası üzerinde birbirini tehdit etmeden yerleştirilmesinin tüm çözümlerini bulur.");
System.out.println("Kaç çözüm görmek istersiniz? (1-92):");
int requestedSolutions = scanner.nextInt();
if (requestedSolutions < 1 || requestedSolutions > 92) {
System.out.println("Geçersiz sayı! Varsayılan olarak 1 çözüm gösterilecek.");
requestedSolutions = 1;
}
System.out.println("\nÇözümler aranıyor...");
findSolutions(0, requestedSolutions);
if (solutionCount == 0) {
System.out.println("Hiç çözüm bulunamadı!");
} else {
System.out.println("\nToplam " + solutionCount + " çözüm bulundu.");
}
}
private void findSolutions(int row, int requestedSolutions) {
if (row == SIZE) {
solutionCount++;
printSolution();
return;
}
for (int col = 0; col < SIZE; col++) {
if (isSafe(row, col)) {
queens[row] = col;
findSolutions(row + 1, requestedSolutions);
if (solutionCount >= requestedSolutions) {
return;
}
}
}
}
private boolean isSafe(int row, int col) {
// Aynı sütunda başka vezir var mı kontrol et
for (int i = 0; i < row; i++) {
if (queens[i] == col) {
return false;
}
}
// Sol üst çaprazda vezir var mı kontrol et
for (int i = row, j = col; i >= 0 && j >= 0; i--, j--) {
if (queens[i] == j) {
return false;
}
}
// Sağ üst çaprazda vezir var mı kontrol et
for (int i = row, j = col; i >= 0 && j < SIZE; i--, j++) {
if (queens[i] == j) {
return false;
}
}
return true;
}
private void printSolution() {
System.out.println("\nÇözüm #" + solutionCount + ":");
System.out.println("+-----------------+");
for (int i = 0; i < SIZE; i++) {
System.out.print("| ");
for (int j = 0; j < SIZE; j++) {
if (queens[i] == j) {
System.out.print("Q ");
} else {
System.out.print(". ");
}
}
System.out.println("|");
}
System.out.println("+-----------------+");
// Çözümün koordinatlarını göster
System.out.print("Koordinatlar: ");
for (int i = 0; i < SIZE; i++) {
System.out.print("(" + (i + 1) + "," + (queens[i] + 1) + ") ");
}
System.out.println("\n");
}
}