Skip to content

Commit 37418e0

Browse files
author
zj972
committed
add 51. golang
1 parent 10065b3 commit 37418e0

File tree

3 files changed

+186
-0
lines changed

3 files changed

+186
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
|048|Rotate Image|[Golang](https://github.com/zj972/leetcode/tree/master/code/048.%20Rotate%20Image)|Medium|
5555
|049|Group Anagrams|[Golang](https://github.com/zj972/leetcode/tree/master/code/049.%20Group%20Anagrams)|Medium|
5656
|050|Pow(x, n)|[Golang](https://github.com/zj972/leetcode/tree/master/code/050.%20Pow(x%2C%20n))|Medium|
57+
|051|N-Queens|[Golang](https://github.com/zj972/leetcode/tree/master/code/051.%20N-Queens)|Hard|
5758
|058|Length of Last Word|[JavaScript](https://github.com/zj972/leetcode/tree/master/code/058.%20Length%20of%20Last%20Word)|Easy|
5859
|062|Unique Paths|[JavaScript](https://github.com/zj972/leetcode/tree/master/code/062.%20Unique%20Paths)|Medium|
5960
|066|Plus One|[JavaScript](https://github.com/zj972/leetcode/tree/master/code/066.%20Plus%20One)|Easy|

code/051. N-Queens/README.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# 51. N-Queens
2+
3+
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other.
4+
5+
![img](https://leetcode.com/static/images/problemset/8-queens.png)
6+
7+
Given an integer n, return all distinct solutions to the n-queens puzzle.
8+
9+
Each solution contains a distinct board configuration of the n-queens' placement, where 'Q' and '.' both indicate a queen and an empty space respectively.
10+
11+
##### Example:
12+
13+
Input: 4
14+
15+
```text
16+
Output: [
17+
[".Q..", // Solution 1
18+
"...Q",
19+
"Q...",
20+
"..Q."],
21+
22+
["..Q.", // Solution 2
23+
"Q...",
24+
"...Q",
25+
".Q.."]
26+
]
27+
```
28+
29+
Explanation: There exist two distinct solutions to the 4-queens puzzle as shown above.
30+
31+
n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击。
32+
33+
![img](https://leetcode.com/static/images/problemset/8-queens.png)
34+
35+
上图为 8 皇后问题的一种解法。
36+
37+
给定一个整数 n,返回所有不同的 n 皇后问题的解决方案。
38+
39+
每一种解法包含一个明确的 n 皇后问题的棋子放置方案,该方案中 'Q' 和 '.' 分别代表了皇后和空位。
40+
41+
##### 示例:
42+
43+
输入: 4
44+
45+
```text
46+
输出: [
47+
[".Q..", // 解法 1
48+
"...Q",
49+
"Q...",
50+
"..Q."],
51+
52+
["..Q.", // 解法 2
53+
"Q...",
54+
"...Q",
55+
".Q.."]
56+
]
57+
```
58+
59+
解释: 4 皇后问题存在两个不同的解法。

code/051. N-Queens/main.go

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
//. 46
8+
//Q 81
9+
//Runtime: 56 ms, faster than 18.52% of Go online submissions for N-Queens.
10+
//func solveNQueens(n int) [][]string {
11+
// ret := [][]string{}
12+
// tmp := make([][]uint8, n)
13+
// row := make([]bool, n)
14+
// for i := 0; i < n; i++ {
15+
// tmp[i] = make([]uint8, n)
16+
// for j := 0; j < n; j++ {
17+
// tmp[i][j] = 46
18+
// }
19+
// row[i] = true
20+
// }
21+
//
22+
// lBias := make([]bool, 2*n-1)
23+
// rBias := make([]bool, 2*n-1)
24+
// for i := 0; i < 2*n-1; i++ {
25+
// lBias[i] = true
26+
// rBias[i] = true
27+
// }
28+
//
29+
// addQ(tmp, 0, 0, row, lBias, rBias, &ret, false)
30+
// return ret
31+
//}
32+
//
33+
//func addQ(data [][]uint8, x int, y int, row []bool, lBias []bool, rBias []bool, ret *[][]string, back bool) {
34+
// fmt.Printf("data: %v, x: %v, y: %v, row: %v, lBias: %v, rBias: %v\n", data, x, y, row, lBias, rBias)
35+
// //校验当前位置是否可以插入Q
36+
// if back {
37+
// //最上面回滚,结束
38+
// if x == 0 {
39+
// return
40+
// }
41+
// i := 0
42+
// for ; i < len(data); i++ {
43+
// if data[x-1][i] == 81 {
44+
// data[x-1][i] = 46
45+
// setCheck(x-1, i, row, lBias, rBias, true)
46+
// break
47+
// }
48+
// }
49+
// if i == len(data)-1 {
50+
// addQ(data, x-1, i, row, lBias, rBias, ret, true)
51+
// } else {
52+
// addQ(data, x-1, i+1, row, lBias, rBias, ret, false)
53+
// }
54+
// } else {
55+
// if row[y] && lBias[x+y] && rBias[(len(rBias)-1)/2+x-y] {
56+
// data[x][y] = 81
57+
// if x == len(data)-1 {
58+
// tmp := []string{}
59+
// for i := 0; i < len(data); i++ {
60+
// tmp = append(tmp, string(data[i]))
61+
// }
62+
// *ret = append(*ret, tmp)
63+
//
64+
// data[x][y] = 46
65+
// addQ(data, x, y, row, lBias, rBias, ret, true)
66+
// } else {
67+
// setCheck(x, y, row, lBias, rBias, false)
68+
// addQ(data, x+1, 0, row, lBias, rBias, ret, false)
69+
// }
70+
// } else {
71+
// if y+1 < len(data) {
72+
// addQ(data, x, y+1, row, lBias, rBias, ret, false)
73+
// } else {
74+
// addQ(data, x, y, row, lBias, rBias, ret, true)
75+
// }
76+
// }
77+
// }
78+
//}
79+
//
80+
//func setCheck(x int, y int, row []bool, lBias []bool, rBias []bool, key bool) {
81+
// row[y] = key
82+
// lBias[x+y] = key
83+
// rBias[(len(rBias)-1)/2+x-y] = key
84+
//}
85+
86+
//Runtime: 8 ms, faster than 100.00% of Go online submissions for N-Queens.
87+
func solveNQueens(n int) [][]string {
88+
return dfs(nil, nil, n)
89+
}
90+
91+
func dfs(solutions [][]string, xs []int, n int) [][]string {
92+
qy := len(xs)
93+
if qy == n {
94+
return append(solutions, solution(xs, n))
95+
}
96+
97+
qx:
98+
for qx := 0; qx < n; qx++ {
99+
for y, x := range xs {
100+
if x == qx || qy-y == x-qx || qy-y == qx-x {
101+
continue qx
102+
}
103+
}
104+
solutions = dfs(solutions, append(xs, qx), n)
105+
}
106+
return solutions
107+
}
108+
109+
func solution(xs []int, n int) []string {
110+
b := make([]byte, n)
111+
for i := range b {
112+
b[i] = '.'
113+
}
114+
115+
s := make([]string, n)
116+
for y, x := range xs {
117+
b[x] = 'Q'
118+
s[y] = string(b)
119+
b[x] = '.'
120+
}
121+
return s
122+
}
123+
124+
func main() {
125+
fmt.Println(solveNQueens(4))
126+
}

0 commit comments

Comments
 (0)