ํฐ์คํ ๋ฆฌ ๋ทฐ
๐ฉ๐ป๐ป ๋ฌธ์
โ๐ป ํ์ด
๐จ Go
// https://www.acmicpc.net/problem/7576
package main
import (
"bufio"
"fmt"
"os"
)
var (
graph [][]int
queue []tomato
dcol = []int{-1, 0, 1, 0}
drow = []int{0, 1, 0, -1}
)
type tomato struct {
row int
col int
}
func main() {
reader := bufio.NewReader(os.Stdin)
writer := bufio.NewWriter(os.Stdout)
defer writer.Flush()
var m, n int
fmt.Fscanln(reader, &m, &n)
graph = make([][]int, n)
for i := 0; i < n; i++ {
graph[i] = make([]int, m)
}
for i := 0; i < n; i++ {
for j := 0; j < m; j++ {
fmt.Fscanf(reader, "%d ", &graph[i][j])
if graph[i][j] == 1 {
queue = append(queue, tomato{row: i, col: j}) // ์ต์ ํ ๋งํ ์ ์ขํ๋ฅผ ํ์ ์ถ๊ฐํ๋ค.
}
}
}
bfs()
fmt.Fprintln(writer, getTimeToGrowRipe(graph))
}
func bfs() {
for len(queue) > 0 {
row := queue[0].row
col := queue[0].col
queue = queue[1:]
for i := 0; i < 4; i++ { // ํ์ ๋ด๊ธด ์์๋๋ก ์ํ์ข์ฐ ํ ๋งํ ๋ฅผ ํ์ธํ๋ค.
newRow := row + drow[i]
newCol := col + dcol[i]
if newRow < 0 || newRow > len(graph)-1 || newCol < 0 || newCol > len(graph[row])-1 {
continue
}
if graph[newRow][newCol] != 0 {
continue
}
graph[newRow][newCol] += (graph[row][col] + 1) // ์ต์ง ์์ ํ ๋งํ ๊ฐ์ ํ์ฌ ํ ๋งํ ๊ฐ + 1๋ก ๊ฐฑ์ ํ๋ค.
queue = append(queue, tomato{newRow, newCol}) // ์ต๊ฒ ๋ ํ ๋งํ ์ขํ๋ฅผ ํ์ ์ถ๊ฐํ๋ค.
}
}
}
func getTimeToGrowRipe(graph [][]int) int {
var totalTime int
for i := 0; i < len(graph); i++ {
for j := 0; j < len(graph[i]); j++ {
if graph[i][j] == 0 { // ์ ์ต์ ํ ๋งํ ๊ฐ ์๋ ๊ฒฝ์ฐ
return -1
}
if graph[i][j] > totalTime {
totalTime = graph[i][j]
}
}
}
return totalTime - 1
}
๐จ Python3
# https://www.acmicpc.net/problem/7576
import sys
graph = []
queue = []
dcol = [-1, 0, 1, 0]
drow = [0, 1, 0, -1]
def bfs():
while len(queue) > 0:
row = queue[0][0]
col = queue[0][1]
queue.pop(0)
for i in range(4):
new_row = row + drow[i]
new_col = col + dcol[i]
if new_row < 0 or new_row > len(graph)-1 or new_col < 0 or new_col > len(graph[row])-1:
continue
if graph[new_row][new_col] != 0:
continue
graph[new_row][new_col] += (graph[row][col]+1)
queue.append((new_row, new_col))
def get_time_to_grow_ripe(graph):
total_time = 0
for i in range(len(graph)):
for j in range(len(graph[i])):
if graph[i][j] == 0:
return -1
total_time = max(graph[i][j], total_time)
return total_time-1
if __name__ == "__main__":
m, n = list(map(int, sys.stdin.readline().split()))
for i in range(n):
graph.append(list(map(int, sys.stdin.readline().split())))
for j in range(m):
if graph[i][j] == 1:
queue.append((i, j))
bfs()
print(get_time_to_grow_ripe(graph))
728x90
'dev > algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
BOJ / 11052๋ฒ / ์นด๋ ๊ตฌ๋งคํ๊ธฐ [Go][Python3] (0) | 2021.03.18 |
---|---|
BOJ / 1697๋ฒ / ์จ๋ฐ๊ผญ์ง [Go][Python3] (0) | 2021.03.17 |
BOJ / 11140๋ฒ / LOL [Go][Python3] (0) | 2021.03.15 |
BOJ / 5883๋ฒ / ์์ดํฐ 9S [Go][Python3] (0) | 2021.03.14 |
BOJ / 14606๋ฒ / ํผ์ (Small) [Go][Python3] (0) | 2021.03.13 |
๋๊ธ
๊ธ ๋ณด๊ดํจ
์ต๊ทผ์ ๋ฌ๋ฆฐ ๋๊ธ
TAG
- ์๊ณ ๋ฆฌ์ฆ
- ๋ชฝ๊ณ ๋๋น
- ballet
- ์๊ฐ๊ต์ฒด
- Golang
- ๋ฐฑ์ค
- AWS
- MongoDB
- java
- python3
- BFS
- ๋งฅ๋ถ ์ ๊ทธ๋ ์ด๋
- ๋ถํ ์ ๋ณต
- ํ๋ก์ด๋์์ฌ
- ์ด๋ถํ์
- BOJ
- dp
- baekjoon
- Macbook pro 2012 mid 13
- ๋งฅ๋ถํ๋ก
- go
- ํ
- ๋งฅ๋ถ
- ํด์๋งต
- Algorithm
- ๋ธ๋ฃจํธํฌ์ค
- ์๋ฐ
- ๋ฐ๋
- ์คํ
- dfs
- Total
- Today
- Yesterday