ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป ๋ฌธ์ œ

 

7576๋ฒˆ: ํ† ๋งˆํ† 

์ฒซ ์ค„์—๋Š” ์ƒ์ž์˜ ํฌ๊ธฐ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๋‘ ์ •์ˆ˜ M,N์ด ์ฃผ์–ด์ง„๋‹ค. M์€ ์ƒ์ž์˜ ๊ฐ€๋กœ ์นธ์˜ ์ˆ˜, N์€ ์ƒ์ž์˜ ์„ธ๋กœ ์นธ์˜ ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค. ๋‹จ, 2 ≤ M,N ≤ 1,000 ์ด๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ๋Š” ํ•˜๋‚˜์˜ ์ƒ์ž์— ์ €์žฅ๋œ ํ† ๋งˆํ† 

www.acmicpc.net


โœ๐Ÿป ํ’€์ด

๐ŸŽจ 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
๋Œ“๊ธ€