Skip to content

Commit 303dad9

Browse files
committed
Add problem 51 - N Queens
1 parent c63d137 commit 303dad9

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed

problems/backtracking/n_queens.go

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package backtracking
2+
3+
func SolveNQueens(n int) [][]string {
4+
// List to store combinations
5+
var combinations [][]string
6+
// Special case
7+
if n == 0 {
8+
return combinations
9+
}
10+
board := make([][]rune, n)
11+
for i := range board {
12+
board[i] = make([]rune, n)
13+
for j := range board[i] {
14+
board[i][j] = '.'
15+
}
16+
}
17+
// Perform backtracking
18+
backtrackForNQueens(board, 0, &combinations)
19+
return combinations
20+
}
21+
22+
func backtrackForNQueens(board [][]rune, index int, combinations *[][]string) {
23+
if index == len(board) {
24+
*combinations = append(*combinations, build(board))
25+
return
26+
}
27+
for i := 0; i < len(board); i++ {
28+
if check(board, i, index) {
29+
board[i][index] = 'Q'
30+
backtrackForNQueens(board, index+1, combinations)
31+
board[i][index] = '.'
32+
}
33+
}
34+
}
35+
36+
func check(board [][]rune, row, column int) bool {
37+
for i := 0; i < len(board); i++ {
38+
for j := 0; j < column; j++ {
39+
if board[i][j] == 'Q' && (row+j == column+i || row+column == i+j || row == i) {
40+
return false
41+
}
42+
}
43+
}
44+
return true
45+
}
46+
47+
func build(board [][]rune) []string {
48+
var combination []string
49+
for _, row := range board {
50+
combination = append(combination, string(row))
51+
}
52+
return combination
53+
}

tests/backtracking/n_queens_test.go

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package backtracking_test
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
7+
"github.com/anirudhology/leetcode-go/problems/backtracking"
8+
)
9+
10+
func TestSolveNQueens(t *testing.T) {
11+
normalize := func(input [][]string) [][]string {
12+
if input == nil {
13+
return [][]string{}
14+
}
15+
return input
16+
}
17+
18+
result1 := backtracking.SolveNQueens(4)
19+
expected1 := [][]string{
20+
{"..Q.", "Q...", "...Q", ".Q.."},
21+
{".Q..", "...Q", "Q...", "..Q."},
22+
}
23+
if !reflect.DeepEqual(normalize(result1), normalize(expected1)) {
24+
t.Errorf("Expected %v but got %v", expected1, result1)
25+
}
26+
27+
result2 := backtracking.SolveNQueens(1)
28+
expected2 := [][]string{
29+
{"Q"},
30+
}
31+
if !reflect.DeepEqual(normalize(result2), normalize(expected2)) {
32+
t.Errorf("Expected %v but got %v", expected2, result2)
33+
}
34+
35+
result3 := backtracking.SolveNQueens(0)
36+
expected3 := [][]string{}
37+
if !reflect.DeepEqual(normalize(result3), normalize(expected3)) {
38+
t.Errorf("Expected %v but got %v", expected3, result3)
39+
}
40+
41+
result4 := backtracking.SolveNQueens(2)
42+
expected4 := [][]string{}
43+
if !reflect.DeepEqual(normalize(result4), normalize(expected4)) {
44+
t.Errorf("Expected %v but got %v", expected4, result4)
45+
}
46+
47+
result5 := backtracking.SolveNQueens(3)
48+
expected5 := [][]string{}
49+
if !reflect.DeepEqual(normalize(result5), normalize(expected5)) {
50+
t.Errorf("Expected %v but got %v", expected5, result5)
51+
}
52+
}

0 commit comments

Comments
 (0)