|
| 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