-
Notifications
You must be signed in to change notification settings - Fork 1
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
10-fnzksxl #52
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
![image](https://private-user-images.githubusercontent.com/108349655/311416804-ebb5035d-dea4-43fc-8167-05982ea45f73.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MzkyNDE2NDAsIm5iZiI6MTczOTI0MTM0MCwicGF0aCI6Ii8xMDgzNDk2NTUvMzExNDE2ODA0LWViYjUwMzVkLWRlYTQtNDNmYy04MTY3LTA1OTgyZWE0NWY3My5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjUwMjExJTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI1MDIxMVQwMjM1NDBaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT04MGQ3NDJlYzQyMWIzZDdlMmVjMTEzNjEwNmUxMDcxNzc5Zjg0YTc3MWEyMzUyMzBkZTViYzVkZTk5MzlhYmQyJlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCJ9.qEp3SyUOITxeC1BnpLZZJvVxD0oyWazwze8HTCHyoVY)
- ์๊ฐ์ด๊ณผ๋ deque ์ฌ์ฉ์ผ๋ก ํด๊ฒฐ
- ์ฃฝ์ ๋๋ฌด๋ฅผ ํ๋ฒ์ ๋ํด๊ณ /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)
}
}
}
}
}
There was a problem hiding this 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)
@wkdghdwns199 deque๋ก.. ์๊ฐ ์ด๊ณผ๊ฐ ์๊ฑธ๋ฆฌ๋๊ฐ์?! |
@SeongHoonC ์ด๋ฐ ๊ตฌํ ๋ฌธ์ ๊ฐ ์ค์ ์ฝํ ์์ ๋์ค๋ฉด ์๋นํ ๊ท์ฐฎ์ ๊ฒ ๊ฐ์ต๋๋ค ใ |
@fnzksxl ์ ํฐ์ง๋๋ผ๊ตฌ์..! ์ ๊ธฐํ๊ฒ ํธ๋ ์ฌ๋ ๋ง๋ค์ ใ ใ |
๐ ๋ฌธ์ ๋งํฌ
๋๋ฌด ์ฌํ ํฌ
โ๏ธ ์์๋ ์๊ฐ
2์๊ฐ 30๋ถ
โจ ์๋ ์ฝ๋
์๊ฐ ์ด๊ณผ ํด๊ฒฐํ๋ ๋ฐ์ ์๊ฐ์ ๋ค ๋ ๋ ค๋จน์์ต๋๋ค. ๋นํ๋ค๊ณ ๋ณผ ์ ์์ฃ .
์ด ๋ฌธ์ ๋ ๋ด, ์ฌ๋ฆ, ๊ฐ์, ๊ฒจ์ธ์ ๊ฐ๊ฐ์ ํ์คํฌ๋ฅผ k๋ฒ ๋๋ฆฐ ํ์ ๊ฒฐ๊ณผ๋ฅผ ์์ธกํด์ผ ํฉ๋๋ค.
๋จ์ ๊ตฌํํ๋๋ฐ์๋ ํฐ ๋ฌธ์ ๊ฐ ์์ง๋ง ์ ๊ฒฝ ์จ์ ์ฝ๋๋ฅผ ๊ตฌํํ์ง ์์ผ๋ฉด ์๊ฐ ์ด๊ณผ์ ๋ช์ ๋น ์ง๊ฒ ๋ฉ๋๋ค..
์ฒ์์ผ๋ก ๊ตฌํํ๋ ๋ฐฉ๋ฒ์ NxN ํ ์ด๋ธ์ ๊ฐ ์นธ์ deque ์๋ฃ๊ตฌ์กฐ๋ฅผ ํ ๋นํด์ ๋๋ฌด๋ฅผ ๋นผ์ค๊ณ ,
๋ด๊ณผ ์ฌ๋ฆ, ๊ฐ์๊ณผ ๊ฒจ์ธ๋ก ๋ ๋ฒ์ ์ด์ค๋ฐ๋ณต๋ฌธ์ด ๋์๊ฐ๊ฒ ํ์ต๋๋ค. ์ด๋ ๊ฒ ํ๋๊น ๊ณ์ ์๊ฐ์ด๊ณผ๊ฐ ๋์ค๋๋ผ๊ตฌ์.
๊ทธ๋์ ๋ ๊ฐ๋ฅผ ํ ๋ฒ์ ๋ฐ๊ฟจ์ต๋๋ค.
์ฃผ์ ์์ด๋์ด๋ trees[i][j]์ ์๋ ๋์ ๋๋ฆฌ[k]์ ๋๋ฌด์ ๊ฐ์๋ฅผ ์ ์ฅํ๋ ๊ฒ๋๋ค.
์ด๋ ๊ฒ ํ๋ฉด bat[i][j] ์นธ์์ ๋์ด๊ฐ K์ธ ๋๋ฌด๋ค์ ์ฑ์ฅ์ํค๋ ๋ฐ ํ์ํ ๋น๋ฃ์ ์์ด
k * trees[i][j][k]๊ฐ ๋๊ฒ ์ฃ
์ ๋ ฌํ ๋์ ๋๋ฆฌ๋ฅผ ์ํํ๋ฉด์ ๋๋ฌด์ ์ฑ์ฅ์ ํ์ํ ๋น๋ฃ์ ํ์ฌ ๋ ์ ๋น๋ฃ๋ฅผ ๋น๊ตํด๊ฐ๋๋ค.
์ฑ์ฅ์ด ๊ฐ๋ฅํ ๋
-> ๋น๋ฃ ์ฐจ๊ฐ
-> ๋๋ฌด ๋์ด + 1
-> ๋๋ฌด ๋์ด + 1 ์ด 5์ ๋ฐฐ์๋ผ๋ฉด ์ฃผ๋ณ 8๊ฐ์ ๋์ด 1 ๋๋ฌด ์ฌ๊ธฐ
์ฑ์ฅ์ด ์๋ ๋
2-1. ๋ชจ๋ ๋๋ฌด๊ฐ ์ฑ์ฅ์ด ์๋ ๋
-> ๋๋ฌด ๋์ด // 2 * ๋๋ฌด๋ฅผ ๋ ์ ๋น๋ฃ์ ์ถ๊ฐ
2-2. ์ผ๋ถ ๋๋ฌด๋ง ์ฑ์ฅ์ด ์๋ ๋
-> ์ผ๋ถ ๋๋ฌด๋งํผ ๋น๋ฃ ์ฐจ๊ฐ
-> ์ผ๋ถ ๋๋ฌด + 1
-> ์ผ๋ถ ๋๋ฌด ๋์ด + 1์ด 5์ ๋ฐฐ์๋ผ๋ฉด ์ฃผ๋ณ 8๊ฐ์ ๋์ด 1 ๋๋ฌด ์ฌ๊ธฐ
-> ๋๋ฌด ๋์ด // 2 * ์ฃฝ์ ๋๋ฌด๋ฅผ ๋ ์ ๋น๋ฃ์ ์ถ๊ฐ
์ฃผ๋ณ ๋ ์ ๋๋ฌด๋ฅผ ์ถ๊ฐํด์ค ๋, deque๋ก ๊ตฌํํ์ ๋๋ ํ์ ํ๋์ฉ ๋ฃ์ด์ค์ผํด์
๋๋ฌด ๊ฐ์๋งํผ ๊ณ์ ์ถ๊ฐ๋ฅผ ํด์คฌ์ด์ผ ํ๋๋ฐ, ๋์ ๋๋ฆฌ์ ๋๋ฌด์ ๊ฐ์๋ฅผ ๋ฃ๋ ๋ฐฉ์์ผ๋ก ๊ตฌํํ๋ฉด
ํ ๋ฒ์ ์ถ๊ฐ๊ฐ ๊ฐ๋ฅํฉ๋๋ค!
๊ตฌํ ๋ฌธ์ ๋ผ ์ฝ๋๊ฐ ์ข ๊น๋๋ค.
(๊ทธ๋์ ์ฃผ์ ๋ฌ์๋๊น ํ๋๋ฐ ์ค๊ฐ์ ๊ด๋์ต๋๋ค)
๐ ์๋กญ๊ฒ ์๊ฒ๋ ๋ด์ฉ
์๊ฐ ์ด๊ณผ๊ฐ ๋๋ค๋ฉด ์๋ฃํ์ ๋น ๋ฅด๊ฒ ๋ฐ๊ฟ๋ณด์..