add new solutions
This commit is contained in:
parent
71189b61cf
commit
9c2c959a9b
10 changed files with 349 additions and 8 deletions
24
solutions/0/q52/solution.go
Normal file
24
solutions/0/q52/solution.go
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
package q52
|
||||
|
||||
func findNSolutions(nQueen, row int, rows, cols, diag1, diag2 []bool) int {
|
||||
solutions := 0
|
||||
if row == nQueen {
|
||||
return 1
|
||||
}
|
||||
for col := range nQueen {
|
||||
// if grid[row][col] is placeable
|
||||
if !rows[row] && !cols[col] && !diag1[row+col] && !diag2[row-col+nQueen] {
|
||||
rows[row], cols[col], diag1[row+col], diag2[row-col+nQueen] = true, true, true, true
|
||||
solutions += findNSolutions(nQueen, row+1, rows, cols, diag1, diag2)
|
||||
rows[row], cols[col], diag1[row+col], diag2[row-col+nQueen] = false, false, false, false
|
||||
}
|
||||
}
|
||||
return solutions
|
||||
}
|
||||
|
||||
func totalNQueens(n int) int {
|
||||
buf := make([]bool, 6*n)
|
||||
return findNSolutions(n, 0, buf[0:n], buf[n:2*n], buf[2*n:4*n], buf[4*n:])
|
||||
}
|
||||
|
||||
var _ = totalNQueens
|
||||
28
solutions/0/q63/solution.go
Normal file
28
solutions/0/q63/solution.go
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
package q63
|
||||
|
||||
func uniquePathsWithObstacles(obstacleGrid [][]int) int {
|
||||
w, h := len(obstacleGrid[0]), len(obstacleGrid)
|
||||
if obstacleGrid[h-1][w-1]+obstacleGrid[0][0] > 0 {
|
||||
return 0
|
||||
}
|
||||
|
||||
obstacleGrid[h-1][w-1] = -1
|
||||
for i := w + h - 2; i >= 0; i-- {
|
||||
for x := min(i, w-1); x >= 0 && i-x < h; x-- {
|
||||
y := i - x
|
||||
if obstacleGrid[y][x] == 1 {
|
||||
continue
|
||||
}
|
||||
if x+1 < w && obstacleGrid[y][x+1] != 1 {
|
||||
obstacleGrid[y][x] += obstacleGrid[y][x+1]
|
||||
}
|
||||
if y+1 < h && obstacleGrid[y+1][x] != 1 {
|
||||
obstacleGrid[y][x] += obstacleGrid[y+1][x]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return -1 * obstacleGrid[0][0]
|
||||
}
|
||||
|
||||
var _ = uniquePathsWithObstacles
|
||||
Loading…
Add table
Add a link
Reference in a new issue