3. What will be the output of the following Go code snippet for N=4?
func main() {
solutions := solveNQueens(4)
fmt.Println(len(solutions))
}
func solveNQueens(n int) [][]string {
var res [][]string
board := make([]int, n)
backtrack(0, n, board, &res)
return res
}
func backtrack(row, n int, board []int, res *[][]string) {
if row == n {
*res = append(*res, generateBoard(board, n))
return
}
for col := 0; col < n; col++ {
if isValid(row, col, board) {
board[row] = col
backtrack(row+1, n, board, res)
}
}
}
func isValid(row, col int, board []int) bool {
for i := 0; i < row; i++ {
if board[i] == col || abs(board[i]-col) == row-i {
return false
}
}
return true
}
func generateBoard(board []int, n int) []string {
var res []string
for i := 0; i < n; i++ {
row := make([]byte, n)
for j := 0; j < n; j++ {
if board[i] == j {
row[j] = 'Q'
} else {
row[j] = '.'
}
}
res = append(res, string(row))
}
return res
}
func abs(x int) int {
if x < 0 {
return -x
}
return x
}