|
1 | 1 | package Challenges_VIII.Little_Bishops;
|
2 | 2 |
|
| 3 | +import java.util.ArrayList; |
3 | 4 | import java.util.Scanner;
|
4 | 5 |
|
5 |
| -public class Main { |
| 6 | +import javax.swing.plaf.metal.MetalIconFactory.PaletteCloseIcon; |
6 | 7 |
|
7 |
| - public static int numSolutions = 0; |
| 8 | +public class Main { |
| 9 | + static ArrayList<String> placedBishops; |
| 10 | + static int numBishops; |
| 11 | + static int numPlacements; |
8 | 12 |
|
9 | 13 | public static void main(String[] args) {
|
10 | 14 | Scanner s = new Scanner(System.in);
|
| 15 | + int sizeOfBoard = 0; |
| 16 | + numBishops = 0; |
11 | 17 |
|
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); |
19 | 23 |
|
20 |
| - System.out.println(numSolutions); |
21 | 24 | }
|
22 | 25 |
|
23 | 26 | }
|
24 | 27 |
|
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; |
35 | 41 | }
|
| 42 | + } |
36 | 43 |
|
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); |
46 | 54 | }
|
| 55 | + } else { |
| 56 | + System.out.println(row + "," + i + " is not safe."); |
47 | 57 | }
|
48 | 58 | }
|
49 |
| - } else { |
50 |
| - numSolutions++; |
51 | 59 |
|
| 60 | + if (placedBishops.size() > 0) |
| 61 | + placedBishops.remove(placedBishops.size() - 1); |
52 | 62 | }
|
53 | 63 |
|
54 | 64 | }
|
|
0 commit comments