ํฐ์คํ ๋ฆฌ ๋ทฐ
๐ฉ๐ป๐ป ๋ฌธ์
โ๐ป ํ์ด
๐จ Go
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
reader := bufio.NewReader(os.Stdin)
writer := bufio.NewWriter(os.Stdout)
defer writer.Flush()
var slice = make([][]int, 9)
for index := range slice {
slice[index] = make([]int, 9)
}
for i := range slice {
for j := range slice[i] {
fmt.Fscanf(reader, "%d ", &slice[i][j])
}
}
if sudoku(slice) {
for i := range slice {
for j := range slice[i] {
fmt.Fprintf(writer, "%d ", slice[i][j])
}
fmt.Fprintln(writer)
}
}
}
func sudoku(slice [][]int) bool {
var row, col int
if !getUnassignedPosition(slice, &row, &col) {
return true
}
for i := 0; i < 9; i++ {
if isSafe(slice, row, col, i+1) {
slice[row][col] = i + 1
if sudoku(slice) {
return true
}
slice[row][col] = 0
}
}
return false
}
func getUnassignedPosition(slice [][]int, rowptr, colptr *int) bool {
for i := 0; i < 9; i++ {
for j := 0; j < 9; j++ {
if slice[i][j] == 0 {
*rowptr = i
*colptr = j
return true
}
}
}
return false
}
func isSafe(slice [][]int, row, col, num int) bool {
if !usedInCol(slice, col, num) &&
!usedInRow(slice, row, num) &&
!usedInSubGrid(slice, row-row%3, col-col%3, num) {
return true
}
return false
}
func usedInCol(slice [][]int, col, num int) bool {
for i := 0; i < 9; i++ {
if slice[i][col] == num {
return true
}
}
return false
}
func usedInRow(slice [][]int, row, num int) bool {
for i := 0; i < 9; i++ {
if slice[row][i] == num {
return true
}
}
return false
}
func usedInSubGrid(slice [][]int, startRow, startCol, num int) bool {
for i := 0; i < 3; i++ {
for j := 0; j < 3; j++ {
if slice[i+startRow][j+startCol] == num {
return true
}
}
}
return false
}
728x90
'dev > algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
BOJ / 14889๋ฒ / ์คํํธ์ ๋งํฌ [Go] (0) | 2020.07.30 |
---|---|
BOJ / 14888๋ฒ / ์ฐ์ฐ์ ๋ผ์๋ฃ๊ธฐ [Go] (0) | 2020.07.29 |
BOJ / 9663๋ฒ / N-Queen [Go] (0) | 2020.07.23 |
BOJ / 15652๋ฒ / N๊ณผ M (4) [Go] (0) | 2020.07.22 |
BOJ / 15651๋ฒ / N๊ณผ M (3) [Go] (0) | 2020.07.21 |
๋๊ธ
๊ธ ๋ณด๊ดํจ
์ต๊ทผ์ ๋ฌ๋ฆฐ ๋๊ธ
TAG
- AWS
- ๋ถํ ์ ๋ณต
- ์๊ณ ๋ฆฌ์ฆ
- ๋งฅ๋ถ
- go
- ๋งฅ๋ถํ๋ก
- ๋ธ๋ฃจํธํฌ์ค
- ๋ฐฑ์ค
- Algorithm
- ์คํ
- python3
- dp
- ์๋ฐ
- ํ๋ก์ด๋์์ฌ
- Macbook pro 2012 mid 13
- ballet
- ์ด๋ถํ์
- ํด์๋งต
- ๋ฐ๋
- ํ
- BFS
- ๋งฅ๋ถ ์ ๊ทธ๋ ์ด๋
- baekjoon
- MongoDB
- java
- ์๊ฐ๊ต์ฒด
- dfs
- ๋ชฝ๊ณ ๋๋น
- Golang
- BOJ
- Total
- Today
- Yesterday