Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

10-fnzksxl #52

Merged
merged 1 commit into from
Mar 13, 2024
Merged

10-fnzksxl #52

merged 1 commit into from
Mar 13, 2024

Conversation

fnzksxl
Copy link
Member

@fnzksxl fnzksxl commented Mar 7, 2024

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

๋‚˜๋ฌด ์žฌํ…Œํฌ

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

2์‹œ๊ฐ„ 30๋ถ„

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

์‹œ๊ฐ„ ์ดˆ๊ณผ ํ•ด๊ฒฐํ•˜๋Š” ๋ฐ์— ์‹œ๊ฐ„์„ ๋‹ค ๋‚ ๋ ค๋จน์—ˆ์Šต๋‹ˆ๋‹ค. ๋‹นํ–ˆ๋‹ค๊ณ  ๋ณผ ์ˆ˜ ์žˆ์ฃ .

์ด ๋ฌธ์ œ๋Š” ๋ด„, ์—ฌ๋ฆ„, ๊ฐ€์„, ๊ฒจ์šธ์— ๊ฐ๊ฐ์˜ ํƒœ์Šคํฌ๋ฅผ k๋ฒˆ ๋Œ๋ฆฐ ํ›„์˜ ๊ฒฐ๊ณผ๋ฅผ ์˜ˆ์ธกํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
๋‹จ์ˆœ ๊ตฌํ˜„ํ•˜๋Š”๋ฐ์—๋Š” ํฐ ๋ฌธ์ œ๊ฐ€ ์—†์ง€๋งŒ ์‹ ๊ฒฝ ์จ์„œ ์ฝ”๋“œ๋ฅผ ๊ตฌํ˜„ํ•˜์ง€ ์•Š์œผ๋ฉด ์‹œ๊ฐ„ ์ดˆ๊ณผ์˜ ๋Šช์— ๋น ์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค..

์ฒ˜์Œ์œผ๋กœ ๊ตฌํ˜„ํ–ˆ๋˜ ๋ฐฉ๋ฒ•์€ NxN ํ…Œ์ด๋ธ”์˜ ๊ฐ ์นธ์— deque ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ํ• ๋‹นํ•ด์„œ ๋‚˜๋ฌด๋ฅผ ๋นผ์˜ค๊ณ ,
๋ด„๊ณผ ์—ฌ๋ฆ„, ๊ฐ€์„๊ณผ ๊ฒจ์šธ๋กœ ๋‘ ๋ฒˆ์˜ ์ด์ค‘๋ฐ˜๋ณต๋ฌธ์ด ๋Œ์•„๊ฐ€๊ฒŒ ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋‹ˆ๊นŒ ๊ณ„์† ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚˜์˜ค๋”๋ผ๊ตฌ์š”.

๊ทธ๋ž˜์„œ ๋‘ ๊ฐœ๋ฅผ ํ•œ ๋ฒˆ์— ๋ฐ”๊ฟจ์Šต๋‹ˆ๋‹ค.

  • ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ๋”•์…”๋„ˆ๋ฆฌ๋กœ ๋ฐ”๊พธ๊ธฐ
  • ์ด์ค‘๋ฐ˜๋ณต๋ฌธ์„ ํ•˜๋‚˜๋กœ ์ค„์ด๊ธฐ

์ฃผ์š” ์•„์ด๋””์–ด๋Š” trees[i][j]์— ์žˆ๋Š” ๋”•์…”๋„ˆ๋ฆฌ[k]์— ๋‚˜๋ฌด์˜ ๊ฐœ์ˆ˜๋ฅผ ์ €์žฅํ•˜๋Š” ๊ฒ๋‹ˆ๋‹ค.
์ด๋ ‡๊ฒŒ ํ•˜๋ฉด bat[i][j] ์นธ์—์„œ ๋‚˜์ด๊ฐ€ K์ธ ๋‚˜๋ฌด๋“ค์„ ์„ฑ์žฅ์‹œํ‚ค๋Š” ๋ฐ ํ•„์š”ํ•œ ๋น„๋ฃŒ์˜ ์–‘์ด
k * trees[i][j][k]๊ฐ€ ๋˜๊ฒ ์ฃ 

์ •๋ ฌํ•œ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์ˆœํšŒํ•˜๋ฉด์„œ ๋‚˜๋ฌด์˜ ์„ฑ์žฅ์— ํ•„์š”ํ•œ ๋น„๋ฃŒ์™€ ํ˜„์žฌ ๋•…์˜ ๋น„๋ฃŒ๋ฅผ ๋น„๊ตํ•ด๊ฐ‘๋‹ˆ๋‹ค.

  1. ์„ฑ์žฅ์ด ๊ฐ€๋Šฅํ•  ๋•Œ
    -> ๋น„๋ฃŒ ์ฐจ๊ฐ
    -> ๋‚˜๋ฌด ๋‚˜์ด + 1
    -> ๋‚˜๋ฌด ๋‚˜์ด + 1 ์ด 5์˜ ๋ฐฐ์ˆ˜๋ผ๋ฉด ์ฃผ๋ณ€ 8๊ฐœ์— ๋‚˜์ด 1 ๋‚˜๋ฌด ์‹ฌ๊ธฐ

  2. ์„ฑ์žฅ์ด ์•ˆ๋  ๋•Œ
    2-1. ๋ชจ๋“  ๋‚˜๋ฌด๊ฐ€ ์„ฑ์žฅ์ด ์•ˆ๋  ๋•Œ
    -> ๋‚˜๋ฌด ๋‚˜์ด // 2 * ๋‚˜๋ฌด๋ฅผ ๋•…์˜ ๋น„๋ฃŒ์— ์ถ”๊ฐ€
    2-2. ์ผ๋ถ€ ๋‚˜๋ฌด๋งŒ ์„ฑ์žฅ์ด ์•ˆ๋  ๋•Œ
    -> ์ผ๋ถ€ ๋‚˜๋ฌด๋งŒํผ ๋น„๋ฃŒ ์ฐจ๊ฐ
    -> ์ผ๋ถ€ ๋‚˜๋ฌด + 1
    -> ์ผ๋ถ€ ๋‚˜๋ฌด ๋‚˜์ด + 1์ด 5์˜ ๋ฐฐ์ˆ˜๋ผ๋ฉด ์ฃผ๋ณ€ 8๊ฐœ์— ๋‚˜์ด 1 ๋‚˜๋ฌด ์‹ฌ๊ธฐ
    -> ๋‚˜๋ฌด ๋‚˜์ด // 2 * ์ฃฝ์€ ๋‚˜๋ฌด๋ฅผ ๋•…์˜ ๋น„๋ฃŒ์— ์ถ”๊ฐ€

์ฃผ๋ณ€ ๋•…์— ๋‚˜๋ฌด๋ฅผ ์ถ”๊ฐ€ํ•ด์ค„ ๋•Œ, deque๋กœ ๊ตฌํ˜„ํ–ˆ์„ ๋•Œ๋Š” ํ์— ํ•˜๋‚˜์”ฉ ๋„ฃ์–ด์ค˜์•ผํ•ด์„œ
๋‚˜๋ฌด ๊ฐœ์ˆ˜๋งŒํผ ๊ณ„์† ์ถ”๊ฐ€๋ฅผ ํ•ด์คฌ์–ด์•ผ ํ–ˆ๋Š”๋ฐ, ๋”•์…”๋„ˆ๋ฆฌ์— ๋‚˜๋ฌด์˜ ๊ฐœ์ˆ˜๋ฅผ ๋„ฃ๋Š” ๋ฐฉ์‹์œผ๋กœ ๊ตฌํ˜„ํ•˜๋ฉด
ํ•œ ๋ฒˆ์— ์ถ”๊ฐ€๊ฐ€ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค!

P.S ํ™•์‹คํžˆ defaultdict๊ฐ€ ๋Š๋ฆฝ๋‹ˆ๋‹ค! ๊ฐ™์€ ๋กœ์ง์— defaultdict๋กœ ์ œ์ถœํ•˜๋ฉด ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋– ์š” ใ… ใ… 

๊ตฌํ˜„ ๋ฌธ์ œ๋ผ ์ฝ”๋“œ๊ฐ€ ์ข€ ๊น๋‹ˆ๋‹ค.
(๊ทธ๋ž˜์„œ ์ฃผ์„ ๋‹ฌ์•„๋‘˜๊นŒ ํ–ˆ๋Š”๋ฐ ์ค‘๊ฐ„์— ๊ด€๋’€์Šต๋‹ˆ๋‹ค)

import sys

dx = [-1, -1, 0, 1, 1, 1, 0, -1]
dy = [0, 1, 1, 1, 0, -1, -1, -1]

input = sys.stdin.readline


def whole_year():
    def push_trees(tree):
        if not (age + 1) % 5:
            for d in range(8):
                nx = i + dx[d]
                ny = j + dy[d]
                if nx >= 0 and nx < N and ny >= 0 and ny < N:
                    new_tree.append((nx, ny, tree))

    new_tree = []
    for i in range(N):
        for j in range(N):
            if trees[i][j]:  # ๋นˆ ๋”•์…”๋„ˆ๋ฆฌ๋ฉด ๊ทธ๋ƒฅ ํ†ต๊ณผ
                dead_trees = 0
                alive = {}
                OKAY = True  # ํ† ์–‘์ด ๋‚˜๋ฌด๋“ค์„ ์„ฑ์žฅ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋Š”์ง€ ์—†๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•œ ํ”Œ๋ž˜๊ทธ
                for age in sorted(trees[i][j]):  # ์–ด๋ฆฐ ๋‚˜๋ฌด๋ถ€ํ„ฐ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด ๋”•์…”๋„ˆ๋ฆฌ ์ •๋ ฌ
                    if OKAY:
                        if age * trees[i][j][age] > bat[i][j]:
                            survive = bat[i][j] // age
                            if survive:
                                bat[i][j] -= survive * age
                                alive[age+1] = survive
                                push_trees(survive)
                            dead_trees += (trees[i][j][age] - survive) * (age // 2)
                            OKAY = False
                        else:
                            bat[i][j] -= age * trees[i][j][age]
                            alive[age+1] = trees[i][j][age]
                            push_trees(trees[i][j][age])
                    else:
                        dead_trees += trees[i][j][age] * (age // 2)

                bat[i][j] += dead_trees
                trees[i][j] = alive

            bat[i][j] += yang[i][j]

    for x, y, tree in new_tree:
        if 1 in trees[x][y].keys():
            trees[x][y][1] += tree
        else:
            trees[x][y][1] = tree


N, M, K = map(int, input().split())

answer = 0
yang = [list(map(int, input().split())) for _ in range(N)]
bat = [[5]*N for _ in range(N)]
trees = [[{} for _ in range(N)] for _ in range(N)]

for _ in range(M):
    x, y, z = map(int, input().split())
    trees[x-1][y-1][z] = 1


for _ in range(K):
    whole_year()

for i in range(N):
    for j in range(N):
        answer += sum(trees[i][j].values())

print(answer)

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋‚œ๋‹ค๋ฉด ์ž๋ฃŒํ˜•์„ ๋น ๋ฅด๊ฒŒ ๋ฐ”๊ฟ”๋ณด์ž..

Copy link
Collaborator

@SeongHoonC SeongHoonC left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image
  1. ์‹œ๊ฐ„์ดˆ๊ณผ๋Š” deque ์‚ฌ์šฉ์œผ๋กœ ํ•ด๊ฒฐ
  2. ์ฃฝ์€ ๋‚˜๋ฌด๋ฅผ ํ•œ๋ฒˆ์— ๋”ํ•ด๊ณ  /2 ํ•ด์„œ ์˜ค์ฐจ๊ฐ€ ๋‹ฌ๋ผ์ง„ ๋ฌธ์ œ ํ•ด๊ฒฐ

ํ›„..๊ณจ3 ๊ตฌํ˜„ ๋ณต์žกํ•˜๋„ค์š”

val dX = listOf(0, 0, 1, -1, 1, -1, -1, 1)
val dY = listOf(1, -1, 0, 0, 1, -1, 1, -1)
fun main() {
    val br = BufferedReader(InputStreamReader(System.`in`))
    val (n, m, k) = br.readLine().split(" ").map { it.toInt() }
    val trees = Array(n + 1) { Array(n + 1) { ArrayDeque<Int>() } }
    val nutrients = Array(n + 1) { Array(n + 1) { 5 } }
    val s2d2 = Array(n + 1) { Array(n + 1) { 0 } }

    for (i in 1..n) {
        val line = br.readLine().split(" ").map { it.toInt() }
        for (j in 1..n) {
            s2d2[i][j] = line[j - 1]
        }
    }

    for (i in 1..m) {
        val (r, c, age) = br.readLine().split(" ").map { it.toInt() }
        trees[r][c].add(age)
        trees[r][c].sort()
    }

    for (i in 0 until k) {
        // ๋ด„ ์—ฌ๋ฆ„ ๊ฒจ์šธ์€ ํ•œ๋ฒˆ์— ๊ฐ€๋Šฅ
        springSummerWinter(trees, nutrients, s2d2, n)
        fall(trees, n)
    }
    println(trees.sumOf { x -> x.sumOf { y -> y.count() } })
}

private fun springSummerWinter(
    trees: Array<Array<ArrayDeque<Int>>>,
    nutrients: Array<Array<Int>>,
    s2d2: Array<Array<Int>>,
    n: Int,
) {
    for (i in 1..n) {
        for (j in 1..n) {
            for (k in trees[i][j].indices) {
                // ๋น„๋ฃŒ๊ฐ€ ์žˆ์œผ๋ฉด ๋‚˜๋ฌด ์„ฑ์žฅ! - ๋ด„
                if (trees[i][j][k] <= nutrients[i][j]) {
                    nutrients[i][j] -= trees[i][j][k]
                    trees[i][j][k] += 1
                    continue
                }
                var sum = 0
                // ๋น„๋ฃŒ ์ฃฝ์–ด์„œ ์ถ”๊ฐ€ - ์—ฌ๋ฆ„
                for (l in k until trees[i][j].size) {
                    sum += trees[i][j][l] / 2
                }
                // ์ฃฝ์€ ๋‚˜๋ฌด ์น˜์šฐ๊ธฐ - ์—ฌ๋ฆ„
                nutrients[i][j] += sum
                val next = ArrayDeque<Int>()
                next.addAll(trees[i][j].subList(0, k))
                trees[i][j] = next
                break
            }
            // ๊ฒจ์šธ์— ๋น„๋ฃŒ ์ถ”๊ฐ€
            nutrients[i][j] += s2d2[i][j]
        }
    }
}

private fun fall(trees: Array<Array<ArrayDeque<Int>>>, n: Int) {
    for (i in 1..n) {
        for (j in 1..n) {
            // 8๋ฐฉ์— ๋‚˜๋ฌด 1 ์ถ”๊ฐ€
            val extraTree = trees[i][j].count { it % 5 == 0 && it != 0 }
            for (k in 0..7) {
                val nextX = i + dX[k]
                val nextY = j + dY[k]
                if (nextX > n || nextY > n || nextY < 1 || nextX < 1) {
                    continue
                }
                repeat(extraTree) {
                    trees[nextX][nextY].addFirst(1)
                }
            }
        }
    }
}

@alstjr7437 alstjr7437 removed their request for review March 11, 2024 03:55
Copy link
Collaborator

@wkdghdwns199 wkdghdwns199 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3์ผ ๊ฑธ๋ฆฌ๋‹ค ๊ฒฐ๊ตญ ๋ณด๊ณ  ํ‘ผ ๋ฌธ์ œ.. 3์ฐจ์›์œผ๋กœ ์ฒ˜๋ฆฌํ•  ์ƒ๊ฐ์€ ๊ฟˆ์—๋„ ๋ชปํ–ˆ๋„ค์š”..!

import sys
from collections import deque
input = sys.stdin.readline

dx = [-1,-1,-1,0,0,1,1,1]
dy = [-1,0,1,-1,1,-1,0,1]

N,M,K = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(N)]
trees = [[deque() for _ in range(N)] for _ in range(N)]

for _ in range(M) :
    x,y,z = map(int, input().split())
    trees[x-1][y-1].append(z)

ground = [[5] * N for _ in range(N)]
for _ in range(K) :
    
    for i in range(N) :
        for j in range(N):
            trees_length = len(trees[i][j])
            for k in range(trees_length) :
                if ground[i][j] >= trees[i][j][k] :
                    ground[i][j] -= trees[i][j][k]
                    trees[i][j][k] += 1
                else :
                    for _ in range(k,trees_length):
                        ground[i][j] += trees[i][j].pop() // 2
                    break

    for i in range(N):
        for j in range(N) :
            for z in trees[i][j] :
                if z % 5 == 0:
                    for idx in range(8):
                        move_x = i + dx[idx]
                        move_y = j + dy[idx]
                        if 0 <= move_x < N and 0 <= move_y < N :
                            trees[move_x][move_y].appendleft(1)
            ground[i][j] += A[i][j]

answer = 0
for i in range(N) :
    for j in range(N):
        answer += len(trees[i][j])
print(answer)

@fnzksxl
Copy link
Member Author

fnzksxl commented Mar 13, 2024

@wkdghdwns199 deque๋กœ.. ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ์•ˆ๊ฑธ๋ฆฌ๋˜๊ฐ€์š”?!

@fnzksxl
Copy link
Member Author

fnzksxl commented Mar 13, 2024

@SeongHoonC ์ด๋Ÿฐ ๊ตฌํ˜„ ๋ฌธ์ œ๊ฐ€ ์‹ค์ œ ์ฝ”ํ…Œ์—์„œ ๋‚˜์˜ค๋ฉด ์ƒ๋‹นํžˆ ๊ท€์ฐฎ์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค ใ…œ

@fnzksxl fnzksxl merged commit a8ba522 into main Mar 13, 2024
9 checks passed
@fnzksxl fnzksxl deleted the 10-fnzksxl branch March 13, 2024 10:42
@wkdghdwns199
Copy link
Collaborator

@fnzksxl ์•ˆ ํ„ฐ์ง€๋”๋ผ๊ตฌ์š”..! ์‹ ๊ธฐํ•˜๊ฒŒ ํ‘ธ๋Š” ์‚ฌ๋žŒ ๋งŽ๋„ค์š” ใ…‹ใ…‹

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants