[BOJ][๐ŸŸก5][๋ฐฑ์ค€#01245] ๋†์žฅ ๊ด€๋ฆฌ

์ž‘์„ฑ:    

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

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

ํƒœ๊ทธ: , , ,

๋ฌธ์ œ ์ถœ์ฒ˜

BAEKJOON Online Judge #1245


๋ฌธ์ œ

๋†๋ถ€ ๋ฏผ์‹์ด๊ฐ€ ๊ด€๋ฆฌํ•˜๋Š” ๋†์žฅ์€ Nร—M ๊ฒฉ์ž๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค. ๋ฏผ์‹์ด๋Š” ๋†์žฅ์„ ๊ด€๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ์‚ฐ๋ด‰์šฐ๋ฆฌ๋งˆ๋‹ค ๊ฒฝ๋น„์›๋ฅผ ๋ฐฐ์น˜ํ•˜๋ ค ํ•œ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด ๋†์žฅ์— ์‚ฐ๋ด‰์šฐ๋ฆฌ๊ฐ€ ์ด ๋ช‡ ๊ฐœ ์žˆ๋Š”์ง€๋ฅผ ์„ธ๋Š” ๊ฒƒ์ด ๋ฌธ์ œ๋‹ค. ์‚ฐ๋ด‰์šฐ๋ฆฌ์˜ ์ •์˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ์‚ฐ๋ด‰์šฐ๋ฆฌ๋Š” ๊ฐ™์€ ๋†’์ด๋ฅผ ๊ฐ€์ง€๋Š” ํ•˜๋‚˜์˜ ๊ฒฉ์ž ํ˜น์€ ์ธ์ ‘ํ•œ ๊ฒฉ์ž๋“ค์˜ ์ง‘ํ•ฉ์œผ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค. (์—ฌ๊ธฐ์„œ โ€œ์ธ์ ‘ํ•˜๋‹คโ€์˜ ์ •์˜๋Š” X์ขŒํ‘œ ์ฐจ์ด์™€ Y์ขŒํ‘œ ์ฐจ์ด ๋ชจ๋‘ 1 ์ดํ•˜์ผ ๊ฒฝ์šฐ๋กœ ์ •์˜๋œ๋‹ค.) ๋˜ํ•œ ์‚ฐ๋ด‰์šฐ๋ฆฌ์™€ ์ธ์ ‘ํ•œ ๊ฒฉ์ž๋Š” ๋ชจ๋‘ ์‚ฐ๋ด‰์šฐ๋ฆฌ์˜ ๋†’์ด๋ณด๋‹ค ์ž‘์•„์•ผํ•œ๋‹ค. ๋ฌธ์ œ๋Š” ๊ฒฉ์ž ๋‚ด์— ์‚ฐ๋ด‰์šฐ๋ฆฌ์˜ ๊ฐœ์ˆ˜๊ฐ€ ์ด ๋ช‡ ๊ฐœ์ธ์ง€ ๊ตฌํ•˜๋Š” ๊ฒƒ์ด๋‹ค.


์ž…๋ ฅ

์ฒซ์งธ ์ค„์— ์ •์ˆ˜ N(1 < N โ‰ค 100), M(1 < M โ‰ค 70)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N+1๋ฒˆ์งธ ์ค„๊นŒ์ง€ ๊ฐ ์ค„๋งˆ๋‹ค ๊ฒฉ์ž์˜ ๋†’์ด๋ฅผ ์˜๋ฏธํ•˜๋Š” M๊ฐœ์˜ ์ •์ˆ˜๊ฐ€ ์ž…๋ ฅ๋œ๋‹ค. ๊ฒฉ์ž์˜ ๋†’์ด๋Š” 500๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์Œ์ด ์•„๋‹Œ ์ •์ˆ˜์ด๋‹ค.


์ถœ๋ ฅ

์ฒซ์งธ ์ค„์— ์‚ฐ๋ด‰์šฐ๋ฆฌ์˜ ๊ฐœ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.


์˜ˆ์ œ

์ž…๋ ฅ

8 7
4 3 2 2 1 0 1
3 3 3 2 1 0 1
2 2 2 2 1 0 0
2 1 1 1 1 0 0
1 1 0 0 0 1 0
0 0 0 1 1 1 0
0 1 2 2 1 1 0
0 1 1 1 2 1 0


์ถœ๋ ฅ

3


My Sol

import sys
input = sys.stdin.readline

def main():
    def find_heights():
        H = [set() for _ in range(501)]
        for i in range(I):
            for j in range(J):
                H[mat[i][j]].add((i, j))
        return H

    def check_around(i, j, h):
        if not (i, j) in H[mat[i][j]]: return
        H[mat[i][j]].remove((i, j))

        for di in range(-1, 2):
            si = i+di
            if not 0<=si<I: continue
            for dj in range(-1, 2):
                sj = j+dj
                if not 0<=sj<J: continue
                if mat[si][sj] > h: continue
                check_around(si, sj, mat[si][sj])
        return
        
    
    I, J = map(int, input().split())
    mat = [list(map(int, input().split())) for _ in range(I)]
    H = find_heights()
    cnt = 0
    while H:
        if not H[-1]:
            H.pop()
            continue
            
        while H[-1]:
            i, j = H[-1].pop()
            H[-1].add((i, j))
            check_around(i, j, mat[i][j])
            cnt += 1
    return cnt

print(main())          

๊ทธ๋ž˜ํ”„์™€ DFS๋ฅผ ํ™œ์šฉํ•˜๋Š” ๋ฌธ์ œ์˜€๋‹ค. ๊ฐ„๋‹จํ•˜๊ฒŒ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.

  1. ๊ฐ€๋กœ, ์„ธ๋กœ, ๋†’์ด ์ •๋ณด์˜ ์ž…๋ ฅ์„ ๋ฐ›๋Š”๋‹ค.
  2. ์ตœ๋Œ€ ๋†’์ด๋Š” 500์ด๋ฏ€๋กœ, ๊ธธ์ด๊ฐ€ 501์ธ list์— set์„ ๊ฐ๊ฐ ๋„ฃ์–ด์ค€๋‹ค.
  3. ๋†’์ด ์ •๋ณด mat ์˜ ์ขŒํ‘œ๋ณ„๋กœ ๋†’์ด๋ฅผ ๊ณ„์‚ฐํ•ด H ์— ๋„ฃ์–ด์ค€๋‹ค.
  4. ๋†’์€ ๋ด‰์šฐ๋ฆฌ๋ถ€ํ„ฐ check_around ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด์„œ ์ฃผ๋ณ€์˜ ์ขŒํ‘œ๋ฅผ ํ™•์ธํ•œ๋‹ค. ๋†’์•„์ง€๊ธฐ ์ „๊นŒ์ง€ ๊ณ„์† ๋ฐ˜๋ณตํ•˜๋ฉด ์ฃผ๋ณ€์„ ์‚ดํ•€๋‹ค.
  5. ์ด ๊ณผ์ •์—์„œ ๋ฐฉ๋ฌธํ•œ ์ขŒํ‘œ๋Š” H์—์„œ ์ œ๊ฑฐํ•œ๋‹ค. ์ดํ›„์— ๋ด‰์šฐ๋ฆฌ๋กœ ์ธ์‹ํ•˜์ง€ ์•Š๊ธฐ ์œ„ํ•จ์ด๋‹ค.
  6. check_around๋ฅผ ํ•œ ๋ฒˆ ์‹คํ–‰ํ–ˆ๋‹ค๋ฉด ํ˜„์žฌ๊นŒ์ง€ ์ค‘์— ๋ด‰์šฐ๋ฆฌ๋กœ ์น  ์ˆ˜ ์žˆ๋Š” ๋ด‰์šฐ๋ฆฌ๋กœ๋ถ€ํ„ฐ ์ฃผ๋ณ€์„ ์ œ๊ฑฐํ•œ ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— cnt๋ฅผ 1 ์˜ฌ๋ ค์ค€๋‹ค.
  7. ๋ชจ๋“  mat์˜ ์ขŒํ‘œ์— ๋Œ€ํ•ด ์ด ๊ณผ์ •์„ ์‹ค์‹œํ•œ ๋’ค cnt๋ฅผ ๋ฐ˜ํ™˜ํ•˜์—ฌ ์ถœ๋ ฅํ•œ๋‹ค.


๊ฒฐ๊ณผ

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

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