ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [๋ฐฑ์ค€] 14502 ์—ฐ๊ตฌ์†Œ with Python
    PS 2022. 1. 24. 20:28
    728x90
    ๋ฐ˜์‘ํ˜•

    ๐Ÿ“Œ BOJ 14502 ์—ฐ๊ตฌ์†Œ

    ๐Ÿ’ก ์กฐ๊ฑด

    1. ์—ฐ๊ตฌ์†Œ๋Š” ํฌ๊ธฐ๊ฐ€ Nร—M์ธ ์ง์‚ฌ๊ฐํ˜•์œผ๋กœ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ง์‚ฌ๊ฐํ˜•์€ 1ร—1 ํฌ๊ธฐ์˜ ์ •์‚ฌ๊ฐํ˜•์œผ๋กœ ๋‚˜๋ˆ„์–ด์ ธ ์žˆ๋‹ค.

    2. ์ƒˆ๋กœ ์„ธ์šธ ์ˆ˜ ์žˆ๋Š” ๋ฒฝ์˜ ๊ฐœ์ˆ˜๋Š” 3๊ฐœ์ด๋ฉฐ, ๊ผญ 3๊ฐœ๋ฅผ ์„ธ์›Œ์•ผ ํ•œ๋‹ค.

    3. ๋ฒฝ์„ 3๊ฐœ ์„ธ์šด ๋’ค, ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ํผ์งˆ ์ˆ˜ ์—†๋Š” ๊ณณ์„ ์•ˆ์ „ ์˜์—ญ์ด๋ผ๊ณ  ํ•œ๋‹ค.

    4. ์ง€๋„์˜ ์„ธ๋กœ ํฌ๊ธฐ N๊ณผ ๊ฐ€๋กœ ํฌ๊ธฐ M์ด ์ฃผ์–ด์ง„๋‹ค. (3 โ‰ค N, M โ‰ค 8)

    5. 0์€ ๋นˆ ์นธ, 1์€ ๋ฒฝ, 2๋Š” ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ์žˆ๋Š” ์œ„์น˜์ด๋‹ค. 2์˜ ๊ฐœ์ˆ˜๋Š” 2๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , 10๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜์ด๋‹ค.

    6. DFS + BFS, ๋ธŒ๋ฃจํŠธํฌ์Šค ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์œ ํ˜•์˜ ๋ฌธ์ œ

    ๐Ÿ–ฅ ์†Œ์Šค ์ฝ”๋“œ

    from sys import stdin
    from collections import deque
    
    n, m = map(int, stdin.readline().split())
    board = []
    virus_xy = []
    
    for i in range(n):
        data = list(map(int, stdin.readline().split()))
        board.append(data)
        for j in range(len(data)):
            if data[j] == 2:
                virus_xy.append((i, j))
    
    score = -1e9
    dx, dy = [1, 0, 0, -1], [0, 1, -1, 0]
    
    
    def get_score(board):
        global score
        cnt = 0
        for x in range(n):
            for y in range(m):
                if board[x][y] == 0:
                    cnt += 1
    
        score = max(score, cnt)
    
    
    def insfection(x, y, board):
        q = deque()
        q.append((x, y))
    
        while q:
            a, b = q.popleft()
            for i in range(4):
                nx, ny = a + dx[i], b + dy[i]
                if -1 < nx < n and -1 < ny < m:
                    if board[nx][ny] == 0:
                        board[nx][ny] = 2
                        q.append((nx, ny))
    
    
    def solutions(cnt):
        if cnt == 3:
            new_board = [item[:] for item in board]
            for x, y in virus_xy:
                insfection(x, y, new_board)
            get_score(new_board)
    
        else:
            for x in range(n):
                for y in range(m):
                    if board[x][y] == 0:
                        board[x][y] = 1
                        cnt += 1
                        solutions(cnt)
                        cnt -= 1
                        board[x][y] = 0
    
    
    solutions(0)
    print(score)

    ๐Ÿ”– ์˜ˆ์ œ ๋ฐ ์‹คํ–‰๊ฒฐ๊ณผ

    ์˜ˆ์ œ

    7 7
    2 0 0 0 1 1 0
    0 0 1 0 1 2 0
    0 1 1 0 1 0 0
    0 1 0 0 0 0 0
    0 0 0 0 0 1 1
    0 1 0 0 0 0 0
    0 1 0 0 0 0 0

    ์‹คํ–‰๊ฒฐ๊ณผ

    27

    โŒจ๏ธ ๋ฌธ์ œ ํ’€์ด

    1. ๋งต์˜ ์ •๋ณด๋ฅผ ์ž…๋ ฅ๋ฐ›์œผ๋ฉด์„œ, ๋ฐ”์ด๋Ÿฌ์Šค์˜ ์œ„์น˜๋ฅผ virus_xy ๋ฆฌ์ŠคํŠธ์— ์ €์žฅํ•œ๋‹ค.

    2. ์•ˆ์ „ ์˜์—ญ์˜ ์ตœ๋Œ“๊ฐ’์„ ๋‹ด์„ ๋ณ€์ˆ˜ score ๋ฅผ -int(1e9)๋กœ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค.

    3. ์ด ๋ฌธ์ œ์˜ ํ’€์ด๋ฅผ ์œ„ํ•ด์„œ ์‚ฌ์šฉํ•  ํ•จ์ˆ˜๋Š” ์ด 3๊ฐœ์ด๋‹ค.

      • get_score(board) : ์•ˆ์ „ ์˜์—ญ์˜ ํฌ๊ธฐ๋ฅผ ๊ณ„์‚ฐํ•˜์—ฌ ์ตœ๋Œ“๊ฐ’์ผ ๊ฒฝ์šฐ score ๊ฐฑ์‹ 
      • insfection(x, y, board) : ๋ฒฝ์„ ์„ธ์šฐ๊ณ , ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ์ด๋™ํ•˜๋ฉฐ ๊ฐ์—ผ. ๋ณด๋“œ๋ฅผ ๊ฐฑ์‹ . BFS ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉ.
      • solutions(cnt) : 3๊ฐœ์˜ ๋ฒฝ์„ ์„ธ์šธ ์ˆ˜ ์žˆ๊ฒŒ cnt๋ฅผ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์‚ฌ์šฉ. DFS ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉ.
    4. ์„ค์น˜ํ•œ ๋ฒฝ์˜ ๊ฐœ์ˆ˜๊ฐ€ 3๊ฐœ๊ฐ€ ๋  ๋•Œ๊นŒ์ง€ DFS ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•ด ๋งต์„ ๊ฐฑ์‹ ํ•œ๋‹ค.
      ๋ฒฝ์ด 3๊ฐœ๊ฐ€ ์„ค์น˜ ๋˜์—ˆ๋‹ค๋ฉด, ์ƒˆ๋กœ์šด ๋ณด๋“œ๋ฅผ ๋งŒ๋“ค๊ณ  ๊ฐ์—ผ์„ ์‹œ์ผœ์ค€๋‹ค.

    5. ์ƒˆ๋กœ์šด ๋ณด๋“œ๋ฅผ ๊ฐ์—ผ์‹œํ‚จ ํ›„, score ๊ณ„์‚ฐ์„ ํ•œ๋‹ค.
      ๊ณ„์‚ฐํ•œ score ๊ฐ’์ด ์ตœ๋Œ“๊ฐ’์ผ ๊ฒฝ์šฐ, ๊ฐฑ์‹ ํ•œ๋‹ค.

    ๐Ÿ’พ ๋Š๋‚€์ 

    1. PyPy3 ๋กœ ์ฑ„์ ํ•˜์—ฌ ํ†ต๊ณผํ–ˆ๋‹ค.
    2. DFS๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฒฝ์„ 3๊ฐœ ์„ค์น˜ํ•œ ํ›„, BFS๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ์—ผ, score ๊ณ„์‚ฐํ•˜๋Š” ๋กœ์ง์„ ์ดํ•ดํ•˜๋Š” ๊ฒƒ์ด ์ฒ˜์Œ์—” ์–ด๋ ค์› ๋‹ค.
    3. ๋ฌธ์ œ๋ฅผ ํ’€๊ณ ์„œ ๋งค์šฐ ๋’ค๋Šฆ๊ฒŒ ๋ธ”๋กœ๊ทธ ํฌ์ŠคํŒ…์„ ํ•˜๋Š” ๊ฒƒ์ธ๋ฐ, ๋กœ์ง์„ ์ดํ•ดํ•˜๋‹ˆ ๋น„์Šทํ•œ ์œ ํ˜•์˜ ๋ฌธ์ œ๋ฅผ ํ‘ธ๋Š” ๊ฒƒ์ด ํ™•์‹คํžˆ ์ˆ˜์›”ํ•ด์กŒ๋‹ค.
    4. ์—ฌ๊ธฐ์„œ ์กฐ๊ธˆ ๋” ์‘์šฉ์„ ํ•˜์ž๋ฉด, ๋ฒฝ์„ ์„ค์น˜ํ–ˆ๋Š”์ง€ ์•ˆํ–ˆ๋Š”์ง€์˜ state๋ฅผ ์ €์žฅํ• 
      dp ๋ฐฐ์—ด์„ ๋งŒ๋“ค๋ฉด ๋ฉ”๋ชจ๋ฆฌ์ œ์ด์…˜์„ ํ†ตํ•ด ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ์ค„์ผ ์ˆ˜ ์žˆ๋Š” ์œ ์‚ฌ๋ฌธ์ œ๋„ ์žˆ์—ˆ๋‹ค.
    ๋ฐ˜์‘ํ˜•

    ๋Œ“๊ธ€

Designed by Tistory.