DevYoon

[SWEA] 2382. ๋ฏธ์ƒ๋ฌผ ๊ฒฉ๋ฆฌ (Python) ๋ณธ๋ฌธ

PS/SWEA

[SWEA] 2382. ๋ฏธ์ƒ๋ฌผ ๊ฒฉ๋ฆฌ (Python)

gimewn 2022. 4. 8. 01:41

link ๐Ÿ”— https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV597vbqAH0DFAVl 

 

SW Expert Academy

SW ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์—ญ๋Ÿ‰ ๊ฐ•ํ™”์— ๋„์›€์ด ๋˜๋Š” ๋‹ค์–‘ํ•œ ํ•™์Šต ์ปจํ…์ธ ๋ฅผ ํ™•์ธํ•˜์„ธ์š”!

swexpertacademy.com

 

 

t = int(input())
for tc in range(1, t+1):
    N, M, K = map(int, input().split()) # ์…€์˜ ๊ฐœ์ˆ˜, ๊ฒฉ๋ฆฌ ์‹œ๊ฐ„, ๊ตฐ์ง‘์˜ ๊ฐœ์ˆ˜
    misaeng = [list(map(int, input().split())) for _ in range(K)] # ์„ธ๋กœ, ๊ฐ€๋กœ, ๋ฏธ์ƒ๋ฌผ ์ˆ˜, ์ด๋™๋ฐฉํ–ฅ
    direct = [(-1, 0), (1, 0), (0, -1), (0, 1)]  # ์ƒ ํ•˜ ์ขŒ ์šฐ
    modi_dir = []
    ret = 0
    for _ in range(M):
        for m in misaeng:
            # ๋ฏธ์ƒ๋ฌผ ์ด๋™, ๊ฒฝ๊ณ„์ฒ˜๋ฆฌ
            m[0] = m[0]+direct[m[3]-1][0] # ์„ธ๋กœ ์ด๋™
            m[1] = m[1]+direct[m[3]-1][1] # ๊ฐ€๋กœ ์ด๋™
            if m[0] == 0 or m[0] == N-1 or m[1] == 0 or m[1] == N-1: # ์•ฝํ’ˆ์ด ๋ฐœ๋ผ์ ธ ์žˆ๋Š” ๊ณณ์— ๋‹ค๋‹ค๋ฅด๋ฉด
                m[2] //= 2
                if m[3] % 2 == 1:
                    m[3] += 1 # ๋ฐฉํ–ฅ์ด ํ™€์ˆ˜๋ฉด 1 ๋”ํ•ด์ฃผ๊ณ 
                else:
                    m[3] -= 1 # ๋ฐฉํ–ฅ์ด ์ง์ˆ˜๋ฉด 1์„ ๋นผ์ฃผ๋ฉด ๋ฐ˜๋Œ€ ๋ฐฉํ–ฅ์ด ๋œ๋‹ค
        # ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ ํ›„ ๊ฐ™์€ ์ขŒํ‘œ์— ์žˆ๋‹ค๋ฉด ๋ณ‘ํ•ฉ
        misaeng.sort(key=lambda x:(x[0], x[1], x[2]), reverse=True) # ์„ธ๋กœ, ๊ฐ€๋กœ, ๋ฏธ์ƒ๋ฌผ ์ˆœ์„œ๋กœ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ
        idx = 1
        while idx < len(misaeng):
            if misaeng[idx-1][0] == misaeng[idx][0] and misaeng[idx-1][1] == misaeng[idx][1]: # y, x ์ขŒํ‘œ๊ฐ€ ๊ฐ™์œผ๋ฉด
                misaeng[idx-1][2] += misaeng[idx][2] # ์•ž์˜ ๋ฏธ์ƒ๋ฌผ ์ˆ˜์—(๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌํ–ˆ์œผ๋‹ˆ๊นŒ ๋” ํผ) ํ˜„์žฌ ๋ฏธ์ƒ๋ฌผ ์ˆ˜ ๋”ํ•ด์ฃผ๊ณ 
                misaeng.pop(idx) # ํ˜„์žฌ ๊ตฐ์ง‘ ์‚ญ์ œ
            else: # ์ขŒํ‘œ ๊ฐ™์ง€ ์•Š์œผ๋ฉด idx 1 ๋”ํ•ด์ฃผ๊ธฐ
                idx += 1
    for s in misaeng: # ๋‚จ์€ ๋ฏธ์ƒ๋ฌผ ์ˆ˜ ๋ชจ๋‘ ๋”ํ•ด์ฃผ๊ธฐ
        ret += s[2]
 
    print(f'#{tc} {ret}')

 

๐Ÿ’ฅ ์ฃผ์˜

  • ๋ฏธ์ƒ๋ฌผ๋“ค ํ•œ ๋ฒˆ์”ฉ ์ด๋™์‹œํ‚จ ๋‹ค์Œ ์ •๋ ฌ → ๊ฐ™์€ ๊ฐ’ ๋ณ‘ํ•ฉํ–ˆ์–ด์•ผ ํ–ˆ๋Š”๋ฐ, ์ด๋™ํ•  ๋•Œ๋งˆ๋‹ค ์ •๋ ฌํ•ด์„œ ๊ฐ™์€ ๊ฐ’ ๋ณ‘ํ•ฉํ•˜๊ณ  ์žˆ์—ˆ๋‹ค...ใ…Žใ…Ž ์–ด์ฉ์ง€ ๋ญ๊ฐ€ ์ด์ƒํ•˜๋”๋ผ๋‹ˆ
  • ์œผ์•„์•„ใ…ใ… ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ž˜ํ•˜๊ณ  ์‹ถ๋‹คใ…ใ…ใ…