ํฐ์คํ ๋ฆฌ ๋ทฐ
๐ฉ๐ป๐ป ๋ฌธ์
โ๐ป ํ์ด
๐จ Go
package main
import (
"bufio"
"fmt"
"os"
)
var cnt int
func main() {
var n int
reader := bufio.NewReader(os.Stdin)
fmt.Fscanln(reader, &n)
var board = make([][]int, n)
for i := 0; i < n; i++ {
board[i] = make([]int, n)
}
_ = solveNQueen(board, n, 0)
fmt.Println(cnt)
}
func solveNQueen(board [][]int, n, col int) bool {
if col >= n {
cnt++
return true
}
for i := 0; i < n; i++ {
if isSafe(board, n, i, col) {
board[i][col] = 1
solveNQueen(board, n, col+1)
board[i][col] = 0
}
}
return false
}
func isSafe(board [][]int, n, row, col int) bool {
for i := 0; i < col; i++ {
if board[row][i] == 1 {
return false
}
}
for i, j := row, col; i >= 0 && j >= 0; i, j = i-1, j-1 {
if board[i][j] == 1 {
return false
}
}
for i, j := row, col; i < n && j >= 0; i, j = i+1, j-1 {
if board[i][j] == 1 {
return false
}
}
return true
}
728x90
'dev > algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
BOJ / 14888๋ฒ / ์ฐ์ฐ์ ๋ผ์๋ฃ๊ธฐ [Go] (0) | 2020.07.29 |
---|---|
BOJ / 2580๋ฒ / ์ค๋์ฟ [Go] (0) | 2020.07.24 |
BOJ / 15652๋ฒ / N๊ณผ M (4) [Go] (0) | 2020.07.22 |
BOJ / 15651๋ฒ / N๊ณผ M (3) [Go] (0) | 2020.07.21 |
BOJ / 15650๋ฒ / N๊ณผ M (2) [Go] (0) | 2020.07.20 |
๋๊ธ
๊ธ ๋ณด๊ดํจ
์ต๊ทผ์ ๋ฌ๋ฆฐ ๋๊ธ
TAG
- dp
- AWS
- ์๊ฐ๊ต์ฒด
- ๋ชฝ๊ณ ๋๋น
- baekjoon
- ์๋ฐ
- ๋งฅ๋ถ
- BOJ
- ๋ฐฑ์ค
- MongoDB
- ๋งฅ๋ถ ์ ๊ทธ๋ ์ด๋
- ๋ฐ๋
- BFS
- ์คํ
- java
- go
- dfs
- ballet
- ๋งฅ๋ถํ๋ก
- Algorithm
- ํด์๋งต
- ๋ถํ ์ ๋ณต
- ํ
- ์ด๋ถํ์
- ์๊ณ ๋ฆฌ์ฆ
- ํ๋ก์ด๋์์ฌ
- Golang
- Macbook pro 2012 mid 13
- python3
- ๋ธ๋ฃจํธํฌ์ค
- Total
- Today
- Yesterday