[BOJ][๐ŸŸก4][๋ฐฑ์ค€#20056] ๋งˆ๋ฒ•์‚ฌ ์ƒ์–ด์™€ ํŒŒ์ด์–ด๋ณผ

์ž‘์„ฑ:    

์—…๋ฐ์ดํŠธ:

์นดํ…Œ๊ณ ๋ฆฌ:

ํƒœ๊ทธ: , , ,

๋ฌธ์ œ ์ถœ์ฒ˜

BAEKJOON Online Judge #20056


๋ฌธ์ œ

์–ด๋ฅธ ์ƒ์–ด๊ฐ€ ๋งˆ๋ฒ•์‚ฌ๊ฐ€ ๋˜์—ˆ๊ณ , ํŒŒ์ด์–ด๋ณผ์„ ๋ฐฐ์› ๋‹ค. ๋งˆ๋ฒ•์‚ฌ ์ƒ์–ด๊ฐ€ ํฌ๊ธฐ๊ฐ€ Nร—N์ธ ๊ฒฉ์ž์— ํŒŒ์ด์–ด๋ณผ M๊ฐœ๋ฅผ ๋ฐœ์‚ฌํ–ˆ๋‹ค. ๊ฐ€์žฅ ์ฒ˜์Œ์— ํŒŒ์ด์–ด๋ณผ์€ ๊ฐ์ž ์œ„์น˜์—์„œ ์ด๋™์„ ๋Œ€๊ธฐํ•˜๊ณ  ์žˆ๋‹ค. i๋ฒˆ ํŒŒ์ด์–ด๋ณผ์˜ ์œ„์น˜๋Š” (ri, ci), ์งˆ๋Ÿ‰์€ mi์ด๊ณ , ๋ฐฉํ–ฅ์€ di, ์†๋ ฅ์€ si์ด๋‹ค. ์œ„์น˜ (r, c)๋Š” rํ–‰ c์—ด์„ ์˜๋ฏธํ•œ๋‹ค. ๊ฒฉ์ž์˜ ํ–‰๊ณผ ์—ด์€ 1๋ฒˆ๋ถ€ํ„ฐ N๋ฒˆ๊นŒ์ง€ ๋ฒˆํ˜ธ๊ฐ€ ๋งค๊ฒจ์ ธ ์žˆ๊ณ , 1๋ฒˆ ํ–‰์€ N๋ฒˆ๊ณผ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๊ณ , 1๋ฒˆ ์—ด์€ N๋ฒˆ ์—ด๊ณผ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋‹ค. ํŒŒ์ด์–ด๋ณผ์˜ ๋ฐฉํ–ฅ์€ ์–ด๋–ค ์นธ๊ณผ ์ธ์ ‘ํ•œ 8๊ฐœ์˜ ์นธ์˜ ๋ฐฉํ–ฅ์„ ์˜๋ฏธํ•˜๋ฉฐ, ์ •์ˆ˜๋กœ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

7 0 1

6 ย  2

5 4 3

๋งˆ๋ฒ•์‚ฌ ์ƒ์–ด๊ฐ€ ๋ชจ๋“  ํŒŒ์ด์–ด๋ณผ์—๊ฒŒ ์ด๋™์„ ๋ช…๋ นํ•˜๋ฉด ๋‹ค์Œ์ด ์ผ๋“ค์ด ์ผ์–ด๋‚œ๋‹ค.

๋ชจ๋“  ํŒŒ์ด์–ด๋ณผ์ด ์ž์‹ ์˜ ๋ฐฉํ–ฅ di๋กœ ์†๋ ฅ si์นธ ๋งŒํผ ์ด๋™ํ•œ๋‹ค.

์ด๋™ํ•˜๋Š”ย ์ค‘์—๋Š” ๊ฐ™์€ ์นธ์— ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํŒŒ์ด์–ด๋ณผ์ด ์žˆ์„ ์ˆ˜๋„ ์žˆ๋‹ค.

์ด๋™์ด ๋ชจ๋‘ ๋๋‚œ ๋’ค, 2๊ฐœ ์ด์ƒ์˜ ํŒŒ์ด์–ด๋ณผ์ด ์žˆ๋Š” ์นธ์—์„œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ผ์ด ์ผ์–ด๋‚œ๋‹ค.

๊ฐ™์€ ์นธ์— ์žˆ๋Š” ํŒŒ์ด์–ด๋ณผ์€ ๋ชจ๋‘ ํ•˜๋‚˜๋กœ ํ•ฉ์ณ์ง„๋‹ค. ํŒŒ์ด์–ด๋ณผ์€ 4๊ฐœ์˜ ํŒŒ์ด์–ด๋ณผ๋กœ ๋‚˜๋ˆ„์–ด์ง„๋‹ค. ๋‚˜๋ˆ„์–ด์ง„ ํŒŒ์ด์–ด๋ณผ์˜ ์งˆ๋Ÿ‰, ์†๋ ฅ, ๋ฐฉํ–ฅ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

์งˆ๋Ÿ‰์€ย โŒŠ(ํ•ฉ์ณ์ง„ ํŒŒ์ด์–ด๋ณผ ์งˆ๋Ÿ‰์˜ ํ•ฉ)/5โŒ‹์ด๋‹ค. ์†๋ ฅ์€ย โŒŠ(ํ•ฉ์ณ์ง„ ํŒŒ์ด์–ด๋ณผ ์†๋ ฅ์˜ ํ•ฉ)/(ํ•ฉ์ณ์ง„ ํŒŒ์ด์–ด๋ณผ์˜ ๊ฐœ์ˆ˜)โŒ‹์ด๋‹ค. ํ•ฉ์ณ์ง€๋Š” ํŒŒ์ด์–ด๋ณผ์˜ ๋ฐฉํ–ฅ์ด ๋ชจ๋‘ ํ™€์ˆ˜์ด๊ฑฐ๋‚˜ ๋ชจ๋‘ ์ง์ˆ˜์ด๋ฉด, ๋ฐฉํ–ฅ์€ 0, 2, 4, 6์ด ๋˜๊ณ , ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด 1, 3, 5, 7์ด ๋œ๋‹ค.

์งˆ๋Ÿ‰์ด 0์ธ ํŒŒ์ด์–ด๋ณผ์€ ์†Œ๋ฉธ๋˜์–ด ์—†์–ด์ง„๋‹ค.

๋งˆ๋ฒ•์‚ฌ ์ƒ์–ด๊ฐ€ ์ด๋™์„ K๋ฒˆ ๋ช…๋ นํ•œ ํ›„, ๋‚จ์•„์žˆ๋Š” ํŒŒ์ด์–ด๋ณผ ์งˆ๋Ÿ‰์˜ ํ•ฉ์„ ๊ตฌํ•ด๋ณด์ž.


์ž…๋ ฅ

์ฒซ์งธ ์ค„์— N, M, K๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ M๊ฐœ์˜ ์ค„์— ํŒŒ์ด์–ด๋ณผ์˜ ์ •๋ณด๊ฐ€ ํ•œ ์ค„์— ํ•˜๋‚˜์”ฉ ์ฃผ์–ด์ง„๋‹ค. ํŒŒ์ด์–ด๋ณผ์˜ ์ •๋ณด๋Š” ๋‹ค์„ฏ ์ •์ˆ˜ ri, ci, mi, si, di๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค. ์„œ๋กœ ๋‹ค๋ฅธ ๋‘ ํŒŒ์ด์–ด๋ณผ์˜ ์œ„์น˜๊ฐ€ ๊ฐ™์€ ๊ฒฝ์šฐ๋Š” ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง€์ง€ ์•Š๋Š”๋‹ค.


์ถœ๋ ฅ

๋งˆ๋ฒ•์‚ฌ ์ƒ์–ด๊ฐ€ ์ด๋™์„ K๋ฒˆ ๋ช…๋ นํ•œ ํ›„, ๋‚จ์•„์žˆ๋Š” ํŒŒ์ด์–ด๋ณผ ์งˆ๋Ÿ‰์˜ ํ•ฉ์„ ์ถœ๋ ฅํ•œ๋‹ค.


์ œํ•œ

4 โ‰ค N โ‰ค 50 0 โ‰ค M โ‰ค N2 1 โ‰ค K โ‰ค 1,000 1 โ‰ค ri, ci โ‰ค N 1 โ‰ค mi โ‰ค 1,000 1 โ‰ค si โ‰ค 1,000 0 โ‰ค di โ‰ค 7


์˜ˆ์ œ

์˜ˆ์ œ 1

์ž…๋ ฅ

4 2 1
1 1 5 2 2
1 4 7 1 6


์ถœ๋ ฅ

8


์˜ˆ์ œ 2

์ž…๋ ฅ

4 2 2
1 1 5 2 2
1 4 7 1 6


์ถœ๋ ฅ

8


์˜ˆ์ œ 3

์ž…๋ ฅ

4 2 3
1 1 5 2 2
1 4 7 1 6


์ถœ๋ ฅ

0


์˜ˆ์ œ 4

์ž…๋ ฅ

7 5 3
1 3 5 2 4
2 3 5 2 6
5 2 9 1 7
6 2 1 3 5
4 4 2 4 2


์ถœ๋ ฅ

9


My Sol

def replace(k):
    global N
    if k < 0:
        while k < 0:
            k += N
    if k >= N:
        while k >= N:
            k -= N
    return k

def move():
    global N, mat, tog, t, direction
    for i in range(N):
        for j in range(N):
            while mat[i][j][t]:
                m, d, s = mat[i][j][t].pop()
                di, dj = direction[d]
                si, sj = replace(i+di*s), replace(j+dj*s)
                if not (0<=si<N and 0<=sj<N): continue
                mat[si][sj][tog[t]].append([m, d, s])
    t^=1


def union_and_divide():
    global N, mat, t, direction
    multi_fireball = [[0]*N for _ in range(N)]
    for i in range(N):
        for j in range(N):
            dir = 1
            fireball_cnt = len(mat[i][j][t])
            if fireball_cnt > 1:
                multi_fireball[i][j] = 1
            while mat[i][j][t]:
                m, d, s = mat[i][j][t].pop()
                if not mat[i][j][t]: break
                std_dir = mat[i][j][t][0][1]
                if std_dir%2 != d%2: dir = 0
                mat[i][j][t][0][0] += m
                mat[i][j][t][0][2] += s

            if not fireball_cnt: continue
            if multi_fireball[i][j]:
                new_m = m//5
                if new_m:
                    new_s = s//fireball_cnt
                    for k in range(1-dir, 8, 2):
                        mat[i][j][tog[t]].append([new_m, k, new_s])
            else:
                mat[i][j][tog[t]].append([m, d, s])
    t^=1

direction = {
    0: (-1, 0),
    1: (-1, 1),
    2: (0, 1),
    3: (1, 1),
    4: (1, 0),
    5: (1, -1),
    6: (0, -1),
    7: (-1, -1)
}

t = 0
tog = {1:0, 0:1}
N, M, K = map(int, input().split())
mat = [[[[], []] for _ in range(N)] for _ in range(N)]
for _ in range(M):
    r, c, mass, speed, direc = map(int, input().split())
    mat[r-1][c-1][t].append([mass, direc, speed])

for _ in range(K):
    move()
    union_and_divide()

# ์นด์šดํŠธ
ret = 0
for i in range(N):
    for j in range(N):
        while mat[i][j][0]:
            m, d, s = mat[i][j][0].pop()
            ret += m

print(ret)

๋‹ค์ฐจ์›์„ ํ™œ์šฉํ•ด์•ผ ํ•˜๋Š” ๊ตฌํ˜„ ๋ฌธ์ œ์˜€๋‹ค. ์ ˆ์ฐจ๋Œ€๋กœ ํ•˜๋ฉด ์–ด๋ ต์ง€ ์•Š์œผ๋‚˜, ๋ฌธ์ œ์—์„œ ๊ฐ€๋ณ๊ฒŒ ์„ค๋ช…ํ•˜๊ณ  ์ง€๋‚˜๋Š” ๋ถ€๋ถ„์ด ๊ฒฐ์ •์ ์ธ ์š”์ธ์ด ๋งŽ์•„ ์ž์„ธํžˆ ์ž˜ ์ฝ์–ด๋ณด์•„์•ผ ํ’€ ์ˆ˜ ์žˆ์—ˆ๋‹ค.

  1. ์ž…๋ ฅ์„ ๋ฐ›์•„ ์ฒ˜๋ฆฌํ•˜๋Š”๋ฐ, 2์ฐจ์› ๋ฐฐ์—ด mat์— toggle ์ธ์ž t์— ๋”ฐ๋ฅธ 0, 1์˜ ์˜ค๊ณ ๊ฐ์„ ์œ„ํ•˜์—ฌ ๋นˆ ๋ฐฐ์—ด 2๊ฐœ๊ฐ€ ์žˆ๋Š” 1์ฐจ์› ๋ฐฐ์—ด์„ mat์˜ ๊ฐ ์˜์—ญ๋งˆ๋‹ค ์ฑ„์›Œ๋„ฃ์–ด ์ดˆ๊ธฐํ™”ํ•œ๋‹ค. ์ฆ‰, 3์ฐจ์› ๋ฐฐ์—ด์ด๋‹ค.
  2. K๋ฒˆ ๊ฐ™์€ ํŒจํ„ด์„ ๋ฐ˜๋ณตํ•˜๋ฏ€๋กœ ๊ฑฑ์ •์—†์ด K๋งŒํผ for๋ฌธ์„ ๋Œ๋ฆฌ๋Š”๋ฐ, ํŒŒ์ด์–ด๋ณผ์ด ์›€์ง์ด๋Š” move ํ•จ์ˆ˜๊ณผ ์›€์ง์ธ ํŒŒ์ด์–ด๋ณผ๋“ค์ด ํ•ฉ์ณ์ง€๊ณ , 2๊ฐœ ์ด์ƒ์ด๋ผ๋ฉด ๋‚˜๋ˆ„์–ด์ง€๋Š” union_and_divide ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•œ๋‹ค.
  3. ํŒŒ์ด์–ด๋ณผ๋“ค์˜ ๋ฐฉํ–ฅ์— ๋”ฐ๋ผ di, dj๋ฅผ ๊ฒฐ์ •ํ•  ์ˆ˜ ์žˆ๋„๋ก direction dictionary๋ฅผ ๋ฏธ๋ฆฌ ๋‘๊ณ , move ํ•จ์ˆ˜์—์„œ๋Š” ํŒŒ์ด์–ด๋ณผ๋“ค๋งˆ๋‹ค direction dictionary์—์„œ ๋ฐฉํ–ฅ ๋ฒกํ„ฐ๋ฅผ, ๊ทธ๋ฆฌ๊ณ  ์†๋„๋ฅผ ์—ฌ๊ธฐ์— ๊ณฑํ•ด ํ˜„์žฌ ์œ„์น˜์— ๋”ํ•˜์—ฌ ์ตœ์ข… ์ด๋™ ์œ„์น˜๋ฅผ ๊ฒฐ์ •ํ•œ๋‹ค.
  4. ์—ฌ๊ธฐ์„œ ์ค‘์š”ํ•œ ์ ์ด 2๊ฐ€์ง€ ์žˆ๋Š”๋ฐ, ์ฒซ์งธ๋Š” ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚œ ํŒŒ์ด์–ด๋ณผ์€ ๋‹ค์‹œ ๊ตฌ์—ญ ์•ˆ์œผ๋กœ ๋“ค์–ด์™€์•ผ ํ•˜๋ฏ€๋กœ, ํฌ๊ธฐ์— ๋”ฐ๋ผ N์„ ๋”ํ•˜๊ฑฐ๋‚˜ ๋นผ์ฃผ๋Š” replace ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด ์ด๋™ ์œ„์น˜๋ฅผ ์กฐ์ •ํ•ด์ฃผ์—ˆ๋‹ค. ๋‘˜์งธ๋Š” ๊ธฐ์กด ์กฐํšŒํ•˜๋Š” fireball๋“ค์˜ t๊ฐ€ 0์—์„œ ํ™•์ธํ•˜๋Š”๋ฐ, ์ƒˆ๋กญ๊ฒŒ ์ด๋™ํ•˜๋Š” fireball์€ 3์ฐจ์› ์ถ•์ด 1์ธ ์ง€์ ์— ๋‘์–ด์•ผ ํ•œ๋‹ค. ๊ทธ๋ž˜์•ผ ์ดํ›„์˜ move๋ฅผ ์œ„ํ•œ for๋ฌธ์—์„œ ์ด๋ฏธ ์ด๋™ํ•œ fireball์ด ์กฐํšŒ๋ฅผ ํ”ผํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๊ฒƒ์ด mat์„ 3์ฐจ์› ๋ฐฐ์—ด๋กœ ๋งŒ๋“  ์ด์œ ์ด๋‹ค.
  5. ์ดํ›„ 3์ฐจ์› ์ถ•์ด 1์ธ ์ง€์ ์„ ์กฐํšŒํ•˜๋ฉฐ ์ด๋™๋œ ํŒŒ์ด์–ด๋ณผ๋“ค์„ ์กฐํšŒํ•˜๋Š”๋ฐ, ๊ฐ ์œ„์น˜์—์„œ ํŒŒ์ด์–ด๋ณผ์ด 2๊ฐœ ์ด์ƒ์ด๋ผ๋ฉด ๋ฐฉํ–ฅ๊ณผ ๋ฌด๊ฒŒ, ์†๋„๋ฅผ ๋ˆ„์  ๋ฐ ํ™•์ธํ•˜์—ฌ ํ•˜๋‚˜๋กœ ๋ชจ์•„์ค€๋‹ค.
  6. ๋งŒ์•ฝ ๊ธฐ์กด์— ํŒŒ์ด์–ด๋ณผ์ด ์—†์—ˆ๋‹ค๋ฉด ๊ทธ๋ƒฅ ๋„˜์–ด๊ฐ€๊ณ , 1๊ฐœ๋งŒ ์žˆ์—ˆ๋‹ค๋ฉด 3์ฐจ์› ์ถ•์ด 0์ธ ์ง€์ ์œผ๋กœ ํŒŒ์ด์–ด๋ณผ์„ ๋ณด๋‚ด์ฃผ๋ฉฐ, 2๊ฐœ ์ด์ƒ์ด์—ˆ๋‹ค๋ฉด 3์ฐจ์› ์ถ•์ด 0์ธ ์ง€์ ์œผ๋กœ ํ˜„์žฌ ํ•ฉ์ณ์ง„ ํŒŒ์ด์–ด๋ณผ์„ 4์กฐ๊ฐ์œผ๋กœ ์ชผ๊ฐœ ๋ชจ๋‘ ๋„ฃ์–ด์ค€๋‹ค.
  7. ์ด๋•Œ ์ค‘์š”ํ•œ ์ ์€ ์ง€๊ธˆ ๋ณด๋‚ด๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ํ˜„์žฌ ์œ„์น˜์— 4๊ฐœ์˜ ํŒŒ์ด์–ด๋ณผ์ด ๋‚จ์•„์žˆ๋Š” ๊ฒƒ์ด๋ฉฐ, ๋‹ค์Œ move์—์„œ ๊ฐ๊ฐ์˜ ํŒŒ์ด์–ด๋ณผ์ด ์‚ฌ๋ฐฉ์œผ๋กœ ํผ์ ธ๋‚˜๊ฐ€๋Š” ๊ฒƒ์ด๋‹ค. ์ด๊ฒƒ์„ ํ˜„์žฌ ํ„ด์— ํ•ด์ฃผ์ง€ ์•Š์•„์•ผ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.
  8. ๋ชจ๋“  K๋ฅผ ๋งˆ์นœ๋‹ค๋ฉด mat์˜ ์ „์ฒด ํ”ฝ์…€์„ ์™„์ „ํƒ์ƒ‰ํ•˜๋ฉฐ 3์ฐจ์› ์ถ•์ด 0์ธ ๋ฐฐ์—ด์˜ fireball๋“ค ๊ฐ 0๋ฒˆ ์ธ๋ฑ์Šค ๊ฐ’์„ ๋ชจ๋‘ ๋”ํ•˜์—ฌ ์ถœ๋ ฅํ•˜๋ฉด ๋œ๋‹ค.


์ •ํ™•ํžˆ๋Š” mat์— fireball(1์ฐจ์›)์˜ ์ง‘ํ•ฉ(2์ฐจ์›)์ด 2๊ฐ€์ง€ ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋Š”(3์ฐจ์›) ๊ฒƒ๋“ค์ด NxN๋งŒํผ(2์ฐจ์›) ์žˆ์œผ๋ฏ€๋กœ 5์ฐจ์›์˜ ๊ณ„์‚ฐ์ด ํ•„์š”ํ•˜์ง€๋งŒ, ์‚ฌ์‹ค ํฌ๊ธฐ๊ฐ€ ๊ทธ๋ฆฌ ํฌ์ง€ ์•Š๊ณ  ์ฐจ์›์— ๋Œ€ํ•œ ๊ณ„์‚ฐ์ด ๋งŽ์ง€ ์•Š์•„ ์ด๋Ÿฐ ๋ถ€๋ถ„์—์„œ๋Š” ํฐ ์–ด๋ ค์›€์ด ์—†๋‹ค.

ํ•˜์ง€๋งŒ fireball์ด ๋‚˜๋ˆ„์–ด์งˆ ๋•Œ ํ˜„์žฌ ํ„ด์—์„œ๋Š” ํ˜„์žฌ ์œ„์น˜์— ๋‚จ์•„์žˆ๋‹ค๋Š” ๊ฒƒ์ด ์˜ค๋‹ต์„ ํ”ผํ•˜๋Š” ๋ฐ์— ์ค‘์š”ํ•œ ํ‚คํฌ์ธํŠธ๊ฐ€ ๋˜๊ฒ ๋‹ค.


๊ฒฐ๊ณผ

๋งž์•˜์Šต๋‹ˆ๋‹ค!!


๋ชจ๋ฒ”๋‹ต์•ˆ

์ถœ์ฒ˜

# empty

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ