-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path51.n-queens.go
110 lines (104 loc) · 1.97 KB
/
51.n-queens.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//package main
import "fmt"
/*
* @lc app=leetcode id=51 lang=golang
*
* [51] N-Queens
*
* https://leetcode.com/problems/n-queens/description/
*
* algorithms
* Hard (37.39%)
* Total Accepted: 127.3K
* Total Submissions: 340.4K
* Testcase Example: '4'
*
* The n-queens puzzle is the problem of placing n queens on an n×n chessboard
* such that no two queens attack each other.
*
*
*
* Given an integer n, return all distinct solutions to the n-queens puzzle.
*
* Each solution contains a distinct board configuration of the n-queens'
* placement, where 'Q' and '.' both indicate a queen and an empty space
* respectively.
*
* Example:
*
*
* Input: 4
* Output: [
* [".Q..", // Solution 1
* "...Q",
* "Q...",
* "..Q."],
*
* ["..Q.", // Solution 2
* "Q...",
* "...Q",
* ".Q.."]
* ]
* Explanation: There exist two distinct solutions to the 4-queens puzzle as
* shown above.
*
*
*/
func solveNQueens(n int) [][]string {
var cols []int
var res [][]int
search(n, cols, &res)
var ret [][]string
ret = trans2Str(res)
return ret
}
func trans2Str(xs [][]int) [][]string {
var ret [][]string
for i := 0; i < len(xs); i++ {
var b []string
for _, j := range xs[i] {
line := make([]byte, len(xs[i]))
for k := range line {
if j != k {
line[k] = '.'
} else {
line[k] = 'Q'
}
}
b = append(b, string(line))
}
ret = append(ret, b)
}
return ret
}
func search(n int, cols []int, res *[][]int) {
if len(cols) == n {
colsCopy := make([]int, len(cols))
copy(colsCopy, cols)
*res = append(*res, colsCopy)
return
}
for col := 0; col < n; col++ {
if !isValid(cols, col) {
continue
}
cols = append(cols, col)
search(n, cols, res)
cols = cols[0 : len(cols)-1]
}
}
func isValid(cols []int, col int) bool {
for k, v := range cols {
if v == col {
return false
}
colK := len(cols)
if colK-k == col-v {
return false
}
if v-col == colK-k {
return false
}
}
return true
}