Skip to content

Commit d2603c0

Browse files
committed
2 parents 9bb3f40 + 6a480f2 commit d2603c0

File tree

1 file changed

+114
-0
lines changed

1 file changed

+114
-0
lines changed
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package com.hackerrank.dynamic;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
7+
/*
8+
9+
stream of consciousness is key
10+
ok what can i do
11+
12+
i have to recuserively search and check
13+
14+
but what i don't understand is
15+
- is do i see the column? why?
16+
- why am i iterating..
17+
*/
18+
19+
public class EightQueens {
20+
21+
private static final Integer SIZE = 8;
22+
23+
public static void main(String...args){
24+
var e = new EightQueens();
25+
var result = e.SolveEightQueens();
26+
System.out.println(result.size());
27+
}
28+
29+
List<int[]> SolveEightQueens(){
30+
int[] columns = new int[]{0,0,0,0,0,0,0,0};
31+
int row = 0;
32+
List<int[]> solutions = new ArrayList<int[]>();
33+
return solveEightQueens(columns,row,solutions);
34+
}
35+
36+
private List<int[]> solveEightQueens(int[]columns, int row, List<int[]> solutions) {
37+
// Start with bottom up base case
38+
if(row == SIZE){
39+
solutions.add(columns.clone());
40+
} else {
41+
// more rows to go
42+
// each col has a unique queen that queen will have a number
43+
for(int queen = 0; queen < SIZE; queen++) {
44+
if(checkQueen(columns,row,queen)){
45+
// made it through check - place queen
46+
columns[row] = queen;
47+
solveEightQueens(columns,row+1,solutions);
48+
}
49+
}
50+
}
51+
52+
return solutions;
53+
}
54+
55+
/*
56+
how do i check the row?
57+
how do i check the col
58+
how do i check the diagonal
59+
keep thinking!!
60+
your not thinking your just stalling
61+
62+
but I don't know what i am doing
63+
what am i doing?
64+
checking for unique values in a matrix
65+
the x axis doesn't matter
66+
67+
only the position within the columns
68+
69+
what is the column value!??!?
70+
71+
why do i have to loop through the column
72+
73+
how do i critical think and problem solve?
74+
75+
Why!?
76+
why is the key
77+
you have a matrix of a board 8x8
78+
queens must have unique row and column and also occupy a unique diagonal.
79+
so it only matter one side
80+
[1] 0
81+
[2] 0
82+
[3] 0
83+
[4] 0
84+
[5] 0
85+
[6] 0
86+
[7] 1
87+
[8] 1
88+
*/
89+
90+
private boolean checkQueen(int[] columns, int proposedRowIndex, int proposedColIndex) {
91+
92+
// Bottom Up
93+
for(int currentRow = 0; currentRow < proposedRowIndex; currentRow++){
94+
int currentCol = columns[currentRow];
95+
// Check Row
96+
// there is no check because we go bottom up and so only consider empty rows.
97+
98+
// Check Column
99+
if(currentCol == proposedColIndex){
100+
return false;
101+
}
102+
103+
// Check Diagonal
104+
int columnDistance = Math.abs(proposedColIndex-currentCol);
105+
int rowDistnace = currentRow-proposedRowIndex;
106+
if(columnDistance == rowDistnace){
107+
return false;
108+
}
109+
110+
}
111+
// if we get this far we made it
112+
return true;
113+
}
114+
}

0 commit comments

Comments
 (0)