ํฐ์คํ ๋ฆฌ ๋ทฐ
๐ฉ๐ป๐ป ๋ฌธ์
โ๐ป ํ์ด
๐จ Go
// https://www.acmicpc.net/problem/1992
// ์ฟผ๋ํธ๋ฆฌ๋ฅผ ๋ฌธ์์ด๋ก ๋ฐ๊พธ๋ ๋ฌธ์
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
var (
image [][]string
writer *bufio.Writer
)
func main() {
reader := bufio.NewReader(os.Stdin)
writer = bufio.NewWriter(os.Stdout)
defer writer.Flush()
var n int
fmt.Fscanln(reader, &n)
for i := 0; i < n; i++ {
input, _ := reader.ReadString('\n') // ๊ณต๋ฐฑ ํฌํจํ์ฌ ์
๋ ฅ ๋ฐ๊ธฐ ์ํด ReadString() ์ฌ์ฉ
inputs := strings.Split(input, "")
image = append(image, inputs)
}
quadTree(0, 0, n)
fmt.Fprintln(writer, "")
}
func quadTree(row, col, n int) {
first := image[row][col]
var isComplete bool = true
for i := row; i < row+n; i++ {
for j := col; j < col+n; j++ {
if i == row && j == col {
continue
}
if image[i][j] != first {
isComplete = false
break
}
}
if !isComplete {
break
}
}
if isComplete {
fmt.Fprint(writer, first)
} else {
fmt.Fprint(writer, "(")
interval := n / 2
quadTree(row, col, interval)
quadTree(row, col+interval, interval)
quadTree(row+interval, col, interval)
quadTree(row+interval, col+interval, interval)
fmt.Fprint(writer, ")")
}
}
๐จ Python3
# https://www.acmicpc.net/problem/1992
# ์ฟผ๋ํธ๋ฆฌ๋ฅผ ๋ฌธ์์ด๋ก ๋ฐ๊พธ๋ ๋ฌธ์
import sys
image = []
def quad_tree(row, col, n):
global image
first = image[row][col]
is_complete = True
for i in range(row, row+n):
for j in range(col, col+n):
if i == row and j == col:
continue
if image[i][j] != first:
is_complete = False
break
if not is_complete:
break
if is_complete:
print(first, end='')
else:
print("(", end='')
interval = n//2
quad_tree(row, col, interval)
quad_tree(row, col+interval, interval)
quad_tree(row+interval, col, interval)
quad_tree(row+interval, col+interval, interval)
print(")", end='')
if __name__ == "__main__":
n = int(sys.stdin.readline())
for i in range(n):
image.append(list(map(int, list(sys.stdin.readline().rstrip()))))
quad_tree(0, 0, n)
print()
728x90
'dev > algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
BOJ / 1629๋ฒ / ๊ณฑ์ [Go][Python3] (0) | 2020.12.31 |
---|---|
BOJ / 1780๋ฒ / ์ข ์ด์ ๊ฐ์ [Go][Python3] (0) | 2020.12.30 |
BOJ / 2630๋ฒ / ์์ข ์ด ๋ง๋ค๊ธฐ [Go][Python3] (0) | 2020.12.28 |
BOJ / 5635๋ฒ / ์์ผ [Go][Python3] (0) | 2020.12.27 |
BOJ / 3182๋ฒ / ํ๋์ด๋ ๊ณต๋ถ๊ฐ ํ๊ธฐ ์ซ์ด! [Go][Python3] (0) | 2020.12.26 |
๋๊ธ
๊ธ ๋ณด๊ดํจ
์ต๊ทผ์ ๋ฌ๋ฆฐ ๋๊ธ
TAG
- ๋งฅ๋ถํ๋ก
- ๋ฐฑ์ค
- java
- ๋งฅ๋ถ ์ ๊ทธ๋ ์ด๋
- ๋ฐ๋
- baekjoon
- BFS
- BOJ
- ์๊ฐ๊ต์ฒด
- ๋ชฝ๊ณ ๋๋น
- ํ๋ก์ด๋์์ฌ
- ๋งฅ๋ถ
- Macbook pro 2012 mid 13
- ํ
- ballet
- ํด์๋งต
- dp
- python3
- AWS
- ์๋ฐ
- Golang
- ์คํ
- ์ด๋ถํ์
- go
- ๋ธ๋ฃจํธํฌ์ค
- Algorithm
- MongoDB
- ๋ถํ ์ ๋ณต
- ์๊ณ ๋ฆฌ์ฆ
- dfs
- Total
- Today
- Yesterday