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

728x90

๐Ÿšฉ ์‹œ๋ฎฌ๋ ˆ์ด์…˜, ๊ตฌํ˜„

* ์‚ผ์„ฑ SW ์—ญ๋Ÿ‰ ํ…Œ์ŠคํŠธ ๊ธฐ์ถœ ๋ฌธ์ œ

 

 

thinking

1. ํ† ๋„ค์ด๋„ ํšŒ์ „ ๋ฐฉํ–ฅ (y์˜ ์œ„์น˜)

2. ๋ฐฉํ–ฅ๋ณ„ ๋ชจ๋ž˜ ๋น„์œจ ์œ„์น˜

3. a๊ฐ’๊ณผ ๊ฒฉ์ž ๋ฐ–์˜ ๋ชจ๋ž˜์˜ ์–‘

์ด๋ ‡๊ฒŒ 3๊ฐ€์ง€๊ฐ€ ๋ฌธ์ œํ’€์ด์˜ ๊ด€๊ฑด์ด์—ˆ๋‹ค. ๊ตฌํ˜„ ๋ฌธ์ œ๋Š” ๋ง๊ทธ๋Œ€๋กœ ๋ฌธ์ œ์—์„œ ํ•˜๋ผ๋Š”๋Œ€๋กœ ํ•˜๋ฉด ๋˜๋Š”๋ฐ ํ† ๋„ค์ด๋„ ๊ตฌํ˜„์ด ์–ด๋ ค์› ๋‹ค. 

 

1. ํ† ๋„ค์ด๋„ ํšŒ์ „ ๋ฐฉํ–ฅ (y์˜ ์œ„์น˜)

ํ† ๋„ค์ด๋„ ๋„๋Š” ๋ฐฉ๋ฒ•์„ ๋‘๊ฐ€์ง€๋กœ ๊ตฌํ•ด๋ดค๋‹ค.
N = 5์ธ ๊ฒฝ์šฐ, ์œ„์˜ ๊ทธ๋ฆผ์ด๋ž‘ ๋งจ ์œ„ ๋ฌธ์ œ์— ์ฃผํ™ฉ์ƒ‰์œผ๋กœ ํ‘œ์‹œํ•œ ๊ฒƒ ์ฒ˜๋Ÿผ ์ด 24๋ฒˆ ์›€์ง์ธ๋‹ค.
(์™ผ์ชฝ ์˜ค๋ฅธ์ชฝ ์œ„ ์•„๋ž˜ = 0 1 2 3)

โ—พ  ๋ฐฉ๋ฒ• 1 - ๋ชซ๊ณผ ๋‚˜๋จธ์ง€๋กœ ๊ตฌํ•˜๊ธฐ (๊ฒ€์ •์ƒ‰)

→↑(2 3)์ด ←↓(0 1)์— ๋น„ํ•ด ํ•œ๋ฒˆ์”ฉ ๋” ์›€์ง์ด๊ณ ,  ํ•œ๋ฐ”ํ€ด ๋‹ค ๋Œ๋ฉด ์ด์ „๋ณด๋‹ค ํ•œ๋ฒˆ ๋” ์›€์ง์ด๋ฏ€๋กœ
๋ชซ์„ ํšŒ์ฐจ, ๋‚˜๋จธ์ง€๋ฅผ dxdy direction (d) ๋ฐฉํ–ฅ์œผ๋กœ ์„ค์ •ํ•œ ํ›„, d=0 ๋˜๋Š” 2์ด๋ฉด ํ•œ๋ฒˆ ๋” ์‹คํ–‰

 

โ—พ  ๋ฐฉ๋ฒ• 2 - [←↓][→↑] ๋ฐฉํ–ฅ ๋‘๊ฐœ์”ฉ ๋ฌถ์–ด์„œ ๊ตฌํ•˜๊ธฐ (ํŒŒ๋žญ์ด)

๊ทธ๋ฆผ์—์„œ ํŒŒ๋ž€์ƒ‰์œผ๋กœ ๋ฌถ์€๊ฒƒ ์ฒ˜๋Ÿผ ์™ผ์•„๋ž˜/์˜ค์œ„ ๋ฅผ ์„ธํŠธ๋กœ ๋ฌถ์–ด์„œ ํ•จ์ˆ˜ ์‹คํ–‰

 

๋งˆ์ง€๋ง‰ ํšŒ์ฐจ์—์„œ ์ˆซ์ž๊ฐ€ ๋ถ€์กฑํ•œ๊ฑด ์‹ ๊ฒฝ ์•ˆ์จ๋„ ๋œ๋‹ค. ์–ด์ฐจํ”ผ ์ธ๋ฑ์Šค ๋ฐ–์œผ๋กœ ๋‚˜๊ฐ€๋ฉด ์ข…๋ฃŒํ•˜๋„๋ก ์ฝ”๋“œ๋ฅผ ์งœ๋ฉด ๋˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

 

2. ๋ฐฉํ–ฅ๋ณ„ ๋ชจ๋ž˜ ๋น„์œจ ์œ„์น˜

๋ฐฉํ–ฅ๋ณ„ ๋ชจ๋ž˜ ๋น„์œจ์˜ ์ขŒํ‘œ๋Š” ๊ทธ๋ฆผ์ฒ˜๋Ÿผ ๋ฐฉํ–ฅ๋ณ„๋กœ ๊ทœ์น™์„ ์ฐพ์•„ ํŠœํ”Œํ˜•ํƒœ๋กœ ๋ฌถ์—ˆ๋‹ค.

a ๋Š” ๋น„์œจ์ด ์—†๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค๋ฅธ ๋น„์œจ์• ๋“ค๊ณผ ๊ตฌ๋ถ„์ง“๊ธฐ ์œ„ํ•ด 0์ด๋ผ๊ณ  ๋’€๊ณ , for๋ฌธ์„ ๋Œ๋ฉด์„œ ๋งจ ๋งˆ์ง€๋ง‰์œผ๋กœ ๊ฐ’์„ ๊ตฌํ•˜๊ธฐ์œ„ํ•ด ๋ฆฌ์ŠคํŠธ์˜ ๋งจ ๋งˆ์ง€๋ง‰์— ๋‘์—ˆ๋‹ค.

 

 

์ฝ”๋“œ

โ—พ  ํ† ๋„ค์ด๋„ ๋„๋Š” ๋ฐฉ๋ฒ• 1 - ๋ชซ๊ณผ ๋‚˜๋จธ์ง€๋กœ ๊ตฌํ•˜๊ธฐ

# ๋ชจ๋ž˜ ๊ณ„์‚ฐํ•˜๋Š” ํ•จ์ˆ˜
def recount(s_x, s_y, direction):
    global ans

    if s_y < 0:
        return
        
    # 3. a, out_sand
    total = 0  # a ๊ตฌํ•˜๊ธฐ ์œ„ํ•œ ๋ณ€์ˆ˜
    for dx, dy, z in direction:
        nx = s_x + dx
        ny = s_y + dy
        if z == 0:  # a(๋‚˜๋จธ์ง€)
            new_sand = sand[s_x][s_y] - total
        else:  # ๋น„์œจ
            new_sand = int(sand[s_x][s_y] * z)
            total += new_sand

        if 0 <= nx < N and 0 <= ny < N:  # ์ธ๋ฑ์Šค ๋ฒ”์œ„์ด๋ฉด ๊ฐ’ ๊ฐฑ์‹ 
            sand[nx][ny] += new_sand
        else:  # ๋ฒ”์œ„ ๋ฐ–์ด๋ฉด ans ์นด์šดํŠธ
            ans += new_sand


N = int(input())
sand = [list(map(int, input().split())) for _ in range(N)]

# 2. ๋ฐฉํ–ฅ๋ณ„ ๋ชจ๋ž˜ ๋น„์œจ ์œ„์น˜
left = [(1, 1, 0.01), (-1, 1, 0.01), (1, 0, 0.07), (-1, 0, 0.07), (1, -1, 0.1),
         (-1, -1, 0.1), (2, 0, 0.02), (-2, 0, 0.02), (0, -2, 0.05), (0, -1, 0)]
right = [(x, -y, z) for x,y,z in left]
down = [(-y, x, z) for x,y,z in left]
up = [(y, x, z) for x,y,z in left]

s_x, s_y = N//2, N//2
ans = 0
dx = [0, 1, 0, -1]
dy = [-1, 0, 1, 0]

# 1.ํ† ๋„ค์ด๋„ ํšŒ์ „ ๋ฐฉํ–ฅ(y์œ„์น˜)
dict = {0: left, 1: down, 2: right, 3: up}
time = 0
for i in range(2*N-1):
    # ๋ชซ: i//4(ํƒ€์ž„+1), ๋‚˜๋จธ์ง€:i%4(๋ฐฉํ–ฅ)
    d = i % 4
    if d == 0 or d == 2:  # ๋‹ค์Œ ํšŒ์ฐจ(d==0) ์ด๊ฑฐ๋‚˜ right(d==2) ์ด๋ฉด ํ•œ๋ฒˆ ๋”
        time += 1
    for _ in range(time):
        n_x = s_x + dx[d]
        n_y = s_y + dy[d]
        recount(n_x, n_y, dict[d])  # y์ขŒํ‘œ, ๋ฐฉํ–ฅ
        s_x, s_y = n_x, n_y

print(ans)

 

โ—พ  ํ† ๋„ค์ด๋„ ๋„๋Š” ๋ฐฉ๋ฒ• 2 - [←↓][→↑] ๋‘ ๋ฐฉํ–ฅ์”ฉ ๋ฌถ์–ด์„œ ๊ตฌํ•˜๊ธฐ

# ๋ชจ๋ž˜ ๊ณ„์‚ฐํ•˜๋Š” ํ•จ์ˆ˜
def recount(time, dx, dy, direction):
    global ans, s_x, s_y

    # y์ขŒํ‘œ ๊ณ„์‚ฐ & x์ขŒํ‘œ ๊ฐฑ์‹ 
    for _ in range(time):
        s_x += dx
        s_y += dy
        if s_y < 0:  # ๋ฒ”์œ„ ๋ฐ–์ด๋ฉด stop
            break

        # 3. a, out_sand
        total = 0  # a ๊ตฌํ•˜๊ธฐ ์œ„ํ•œ ๋ณ€์ˆ˜
        for dx, dy, z in direction:
            nx = s_x + dx
            ny = s_y + dy
            if z == 0:  # a(๋‚˜๋จธ์ง€)
                new_sand = sand[s_x][s_y] - total
            else:  # ๋น„์œจ
                new_sand = int(sand[s_x][s_y] * z)
                total += new_sand

            if 0 <= nx < N and 0 <= ny < N:   # ์ธ๋ฑ์Šค ๋ฒ”์œ„์ด๋ฉด ๊ฐ’ ๊ฐฑ์‹ 
                sand[nx][ny] += new_sand
            else:  # ๋ฒ”์œ„ ๋ฐ–์ด๋ฉด ans ์นด์šดํŠธ
                ans += new_sand


N = int(input())
sand = [list(map(int, input().split())) for _ in range(N)]

# 2. ๋ฐฉํ–ฅ๋ณ„ ๋ชจ๋ž˜ ๋น„์œจ ์œ„์น˜
left = [(1, 1, 0.01), (-1, 1, 0.01), (1, 0, 0.07), (-1, 0, 0.07), (1, -1, 0.1),
         (-1, -1, 0.1), (2, 0, 0.02), (-2, 0, 0.02), (0, -2, 0.05), (0, -1, 0)]
right = [(x, -y, z) for x, y, z in left]
down = [(-y, x, z) for x, y, z in left]
up = [(y, x, z) for x, y, z in left]

s_x, s_y = N//2, N//2  # ์‹œ์ž‘์ขŒํ‘œ(x์ขŒํ‘œ)
ans = 0  # out_sand

# 1.ํ† ๋„ค์ด๋„ ํšŒ์ „ ๋ฐฉํ–ฅ(y์œ„์น˜)
for i in range(1, N + 1):
    if i % 2:
        recount(i, 0, -1, left)
        recount(i, 1, 0, down)
    else:
        recount(i, 0, 1, right)
        recount(i, -1, 0, up)

print(ans)

 

 

์œ„: ๋ฐฉ๋ฒ•2 / ์•„๋ž˜: ๋ฐฉ๋ฒ•1

 

๋Œ“๊ธ€