ํฐ์คํ ๋ฆฌ ๋ทฐ
๐ฉ๐ป๐ป ๋ฌธ์
โ๐ป ํ์ด
๐จ Go
// https://www.acmicpc.net/problem/2667
package main
import (
"bufio"
"fmt"
"os"
"sort"
"strings"
)
var (
graph [][]string
visited [][]bool
numOfHouses int
)
func main() {
reader := bufio.NewReader(os.Stdin)
writer := bufio.NewWriter(os.Stdout)
defer writer.Flush()
var n int
fmt.Fscanln(reader, &n)
graph = make([][]string, n)
for i := 0; i < n; i++ {
graph[i] = make([]string, n)
}
visited = make([][]bool, n)
for i := 0; i < n; i++ {
visited[i] = make([]bool, n)
}
for i := 0; i < n; i++ {
var input string
input, _ = reader.ReadString('\n') // ๊ณต๋ฐฑ ํฌํจํ์ฌ ์
๋ ฅ ๋ฐ๊ธฐ ์ํด ReadString() ์ฌ์ฉ
convInput := strings.ReplaceAll(input, "\n", "")
inputs := strings.Split(convInput, "")
for j, input := range inputs {
graph[i][j] = input
}
}
numbers := []int{}
for i := 0; i < n; i++ {
for j := 0; j < n; j++ {
if graph[i][j] == "1" && !visited[i][j] {
numOfHouses = 0
dfs(i, j)
numbers = append(numbers, numOfHouses)
}
}
}
sort.Ints(numbers)
fmt.Fprintln(writer, len(numbers))
for _, num := range numbers {
fmt.Fprintln(writer, num)
}
}
func dfs(row, col int) {
visited[row][col] = true
numOfHouses++
if row+1 < len(graph) && graph[row+1][col] == "1" && !visited[row+1][col] {
dfs(row+1, col)
}
if row-1 >= 0 && graph[row-1][col] == "1" && !visited[row-1][col] {
dfs(row-1, col)
}
if col+1 < len(graph[row]) && graph[row][col+1] == "1" && !visited[row][col+1] {
dfs(row, col+1)
}
if col-1 >= 0 && graph[row][col-1] == "1" && !visited[row][col-1] {
dfs(row, col-1)
}
}
๐จ Python3
# https://www.acmicpc.net/problem/2667
import sys
graph = []
visited = []
num_of_houses = 0
def dfs(row, col):
global graph
global visited
global num_of_houses
visited[row][col] = True
num_of_houses += 1
if row+1 < len(graph) and graph[row+1][col] == 1 and not visited[row+1][col]:
dfs(row+1, col)
if row-1 >= 0 and graph[row-1][col] == 1 and not visited[row-1][col]:
dfs(row-1, col)
if col+1 < len(graph) and graph[row][col+1] == 1 and not visited[row][col+1]:
dfs(row, col+1)
if col-1 >= 0 and graph[row][col-1] == 1 and not visited[row][col-1]:
dfs(row, col-1)
if __name__ == "__main__":
n = int(sys.stdin.readline())
graph = [[0 for i in range(n)] for j in range(n)]
visited = [[False for i in range(n)] for j in range(n)]
for i in range(n):
inputs = list(map(int, list(sys.stdin.readline().rstrip())))
for j, val in enumerate(inputs):
graph[i][j] = val
numbers = []
for i in range(n):
for j in range(n):
if graph[i][j] == 1 and not visited[i][j]:
num_of_houses = 0
dfs(i, j)
numbers.append(num_of_houses)
numbers.sort()
print(len(numbers))
for num in numbers:
print(num)
728x90
'dev > algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
BOJ / 2947๋ฒ / ๋๋ฌด ์กฐ๊ฐ [Go][Python3] (0) | 2020.12.18 |
---|---|
BOJ / 2468๋ฒ / ์์ ์์ญ [Go][Python3] (0) | 2020.12.16 |
BOJ / 11724๋ฒ / ์ฐ๊ฒฐ ์์์ ๊ฐ์ [Go][Python3] (0) | 2020.12.14 |
BOJ / 1012๋ฒ / ์ ๊ธฐ๋ ๋ฐฐ์ถ [Go][Python3] (0) | 2020.12.13 |
BOJ / 4963๋ฒ / ์ฌ์ ๊ฐ์ [Go][Python3] (0) | 2020.12.12 |
๋๊ธ
๊ธ ๋ณด๊ดํจ
์ต๊ทผ์ ๋ฌ๋ฆฐ ๋๊ธ
TAG
- ๋ฐฑ์ค
- ์ด๋ถํ์
- MongoDB
- ballet
- Algorithm
- ์๊ณ ๋ฆฌ์ฆ
- ์๋ฐ
- ๋ชฝ๊ณ ๋๋น
- BFS
- ํ
- ์คํ
- ๋งฅ๋ถ ์ ๊ทธ๋ ์ด๋
- ์๊ฐ๊ต์ฒด
- ๋งฅ๋ถํ๋ก
- ๋ฐ๋
- Golang
- ๋ถํ ์ ๋ณต
- go
- AWS
- dfs
- dp
- java
- baekjoon
- ๋งฅ๋ถ
- BOJ
- python3
- ํด์๋งต
- Macbook pro 2012 mid 13
- ํ๋ก์ด๋์์ฌ
- ๋ธ๋ฃจํธํฌ์ค
- Total
- Today
- Yesterday