-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
149 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package main | ||
|
||
func solveNQueens(n int) (ans [][]string) { | ||
col := make([]int, n) | ||
dg := make([]int, n<<1) | ||
udg := make([]int, n<<1) | ||
t := make([][]byte, n) | ||
for i := range t { | ||
t[i] = make([]byte, n) | ||
for j := range t[i] { | ||
t[i][j] = '.' | ||
} | ||
} | ||
var dfs func(int) | ||
dfs = func(i int) { | ||
if i == n { | ||
tmp := make([]string, n) | ||
for i := range tmp { | ||
tmp[i] = string(t[i]) | ||
} | ||
ans = append(ans, tmp) | ||
return | ||
} | ||
for j := 0; j < n; j++ { | ||
if col[j]+dg[i+j]+udg[n-i+j] == 0 { | ||
col[j], dg[i+j], udg[n-i+j] = 1, 1, 1 | ||
t[i][j] = 'Q' | ||
dfs(i + 1) | ||
t[i][j] = '.' | ||
col[j], dg[i+j], udg[n-i+j] = 0, 0, 0 | ||
} | ||
} | ||
} | ||
dfs(0) | ||
return | ||
} | ||
|
||
/* | ||
* with 15x15 board | ||
* ./Solution 5.51s user 0.16s system 106% cpu 5.312 total, | ||
* compare to javac Solution.java 0.76s user 0.04s system 238% cpu 0.334 total | ||
*/ | ||
func main() { | ||
solveNQueens(15) | ||
} |
104 changes: 104 additions & 0 deletions
104
interview_prep/algorithm/java/ide_handicapped/nqueens/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import java.util.AbstractList; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
class Solution { | ||
List<List<String>> result; | ||
public List<List<String>> solveNQueens(int n) { | ||
return new AbstractList<List<String>>(){ | ||
@Override | ||
public int size(){ | ||
init(); | ||
return result.size(); | ||
} | ||
|
||
@Override | ||
public List<String> get(int index){ | ||
init(); | ||
return result.get(index); | ||
} | ||
|
||
private void init(){ | ||
if(result != null) return; | ||
int[][] grid = newEmptyBoard(n); | ||
getQueenCombinations(grid, n, 0, 0); | ||
} | ||
}; | ||
} | ||
private void getQueenCombinations( | ||
int[][] grid, | ||
int n, | ||
int row, | ||
int col | ||
){ | ||
if(row >= n){ | ||
result.add(build(grid, n)); | ||
return; | ||
} | ||
|
||
for(int i = 0; i < n; i++){ | ||
if(isValidCombination(grid, n, row, i)){ | ||
grid[row][i] = 1; | ||
getQueenCombinations(grid, n, row + 1, col); | ||
grid[row][i] = 0; | ||
} | ||
} | ||
} | ||
|
||
private boolean isValidCombination( | ||
int[][] grid, | ||
int n, | ||
int row, | ||
int col | ||
){ | ||
for(int i = 0; i < n; i++){ | ||
for(int j = 0; j < n; j++){ | ||
if(grid[i][j] == 1 && (row + col == i + j || row + j == col + i || j == col)){ | ||
return false; | ||
} | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
private List<String> build(int[][] grid, int n){ | ||
final List<String> solution = new ArrayList<>(); | ||
for(int i = 0; i < n; i++){ | ||
StringBuilder sb = new StringBuilder(); | ||
for(int j = 0; j < n; j++){ | ||
if(grid[i][j] == 0){ | ||
sb.append('.'); | ||
} | ||
else{ | ||
sb.append('Q'); | ||
} | ||
} | ||
solution.add(sb.toString()); | ||
} | ||
return solution; | ||
} | ||
|
||
public static void main(String[] args) { | ||
//https://leetcode.com/problems/n-queens/ | ||
int boardSize = 15; | ||
final Solution solution = new Solution(); | ||
final List<List<String>> solutions = solution.solveNQueens(boardSize); | ||
|
||
// for (List<String> solutionBoard : solutions) { | ||
// for (String row : solutionBoard) { | ||
// System.out.println(row); | ||
// } | ||
// System.out.println(); | ||
// } | ||
} | ||
|
||
private int[][] newEmptyBoard(int size){ | ||
int[][] grid = new int[size][size]; | ||
result = new ArrayList<>(); | ||
for(int[] row : grid){ | ||
Arrays.fill(row, 0); | ||
} | ||
return grid; | ||
} | ||
|
||
} |