Skip to content

Commit 54b4f1a

Browse files
committed
Getting there....
1 parent 1c8ccae commit 54b4f1a

File tree

1 file changed

+41
-31
lines changed

1 file changed

+41
-31
lines changed

Challenges_VIII/Little_Bishops/Main.java

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,64 @@
11
package Challenges_VIII.Little_Bishops;
22

3+
import java.util.ArrayList;
34
import java.util.Scanner;
45

5-
public class Main {
6+
import javax.swing.plaf.metal.MetalIconFactory.PaletteCloseIcon;
67

7-
public static int numSolutions = 0;
8+
public class Main {
9+
static ArrayList<String> placedBishops;
10+
static int numBishops;
11+
static int numPlacements;
812

913
public static void main(String[] args) {
1014
Scanner s = new Scanner(System.in);
15+
int sizeOfBoard = 0;
16+
numBishops = 0;
1117

12-
int boardSize = 0;
13-
int maxBishops = 0;
14-
15-
while ((boardSize = s.nextInt()) != 0 && (maxBishops = s.nextInt()) != 0) {
16-
numSolutions = 0;
17-
int[] equations = new int[maxBishops];
18-
backtrack(equations, 1, maxBishops, boardSize);
18+
while ((sizeOfBoard = s.nextInt()) != 0 && (numBishops = s.nextInt()) != 0) {
19+
numPlacements = 0;
20+
placedBishops = new ArrayList<String>();
21+
backtrack(1, sizeOfBoard);
22+
System.out.println(numPlacements);
1923

20-
System.out.println(numSolutions);
2124
}
2225

2326
}
2427

25-
public static void backtrack(int[] equations, int numBishops, int maxBishops, int boardSize) {
26-
if (numBishops < maxBishops) {
27-
for (int x = 0; x < boardSize; x++) {
28-
for (int y = 0; y < boardSize; y++) {
29-
boolean blocked = false;
30-
for (int e : equations) {
31-
if (((-1 * x) + e) == y || (x - e) == y) {
32-
blocked = true;
33-
break;
34-
}
28+
public static void backtrack(int row, int n) {
29+
// n is the number of columns on the board
30+
// i is the current column
31+
if (row <= n) {
32+
for (int i = 1; i <= n; i++) {
33+
boolean isSafe = true;
34+
for (String b : placedBishops) {
35+
String[] point = b.split(",");
36+
int[] coord = { Integer.parseInt(point[0]), Integer.parseInt(point[1]) };
37+
38+
if (Math.abs(row - coord[0]) == Math.abs(i - coord[1])) {
39+
isSafe = false;
40+
break;
3541
}
42+
}
3643

37-
if (!blocked) {
38-
if (numBishops < maxBishops) {
39-
int[] temp = equations;
40-
temp[numBishops] = y - x;
41-
backtrack(temp, numBishops + 1, maxBishops, boardSize);
42-
}
43-
// if (numBishops == maxBishops) {
44-
// numSolutions++;
45-
// }
44+
if (isSafe) {
45+
placedBishops.add(String.valueOf(row) + "," + String.valueOf(i));
46+
System.out.println("*** " + row + "," + i + " is safe. ***");
47+
if (placedBishops.size() == numBishops) {
48+
numPlacements++;
49+
System.out.println("New Solution! Number of sol: " + numPlacements);
50+
placedBishops.remove(placedBishops.size() - 1);
51+
// break;
52+
} else {
53+
backtrack(row + 1, n);
4654
}
55+
} else {
56+
System.out.println(row + "," + i + " is not safe.");
4757
}
4858
}
49-
} else {
50-
numSolutions++;
5159

60+
if (placedBishops.size() > 0)
61+
placedBishops.remove(placedBishops.size() - 1);
5262
}
5363

5464
}

0 commit comments

Comments
 (0)