ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Python] ๋ฐฑ์ค€ 3184 ์–‘ (BFS)
    ๐Ÿ’ป Algorithm/Python 2022. 7. 2. 15:49

     

    ๐Ÿ“Œ ๋ฌธ์ œ

    ๋ฏธํ‚ค์˜ ๋’ท๋งˆ๋‹น์—๋Š” ํŠน์ • ์ˆ˜์˜ ์–‘์ด ์žˆ๋‹ค. ๊ทธ๊ฐ€ ํ‘น ์ž ๋“  ์‚ฌ์ด์— ๋ฐฐ๊ณ ํ”ˆ ๋Š‘๋Œ€๋Š” ๋งˆ๋‹น์— ๋“ค์–ด์™€ ์–‘์„ ๊ณต๊ฒฉํ–ˆ๋‹ค.

     

    ๋งˆ๋‹น์€ ํ–‰๊ณผ ์—ด๋กœ ์ด๋ฃจ์–ด์ง„ ์ง์‚ฌ๊ฐํ˜• ๋ชจ์–‘์ด๋‹ค. ๊ธ€์ž '.' (์ )์€ ๋นˆ ํ•„๋“œ๋ฅผ ์˜๋ฏธํ•˜๋ฉฐ, ๊ธ€์ž '#'๋Š” ์šธํƒ€๋ฆฌ๋ฅผ, 'o'๋Š” ์–‘, 'v'๋Š” ๋Š‘๋Œ€๋ฅผ ์˜๋ฏธํ•œ๋‹ค.

     

    ํ•œ ์นธ์—์„œ ์ˆ˜ํ‰, ์ˆ˜์ง๋งŒ์œผ๋กœ ์ด๋™ํ•˜๋ฉฐ ์šธํƒ€๋ฆฌ๋ฅผ ์ง€๋‚˜์ง€ ์•Š๊ณ  ๋‹ค๋ฅธ ์นธ์œผ๋กœ ์ด๋™ํ•  ์ˆ˜ ์žˆ๋‹ค๋ฉด, ๋‘ ์นธ์€ ๊ฐ™์€ ์˜์—ญ ์•ˆ์— ์†ํ•ด ์žˆ๋‹ค๊ณ  ํ•œ๋‹ค. ๋งˆ๋‹น์—์„œ "ํƒˆ์ถœ"ํ•  ์ˆ˜ ์žˆ๋Š” ์นธ์€ ์–ด๋–ค ์˜์—ญ์—๋„ ์†ํ•˜์ง€ ์•Š๋Š”๋‹ค๊ณ  ๊ฐ„์ฃผํ•œ๋‹ค.

     

    ๋‹คํ–‰ํžˆ ์šฐ๋ฆฌ์˜ ์–‘์€ ๋Š‘๋Œ€์—๊ฒŒ ์‹ธ์›€์„ ๊ฑธ ์ˆ˜ ์žˆ๊ณ  ์˜์—ญ ์•ˆ์˜ ์–‘์˜ ์ˆ˜๊ฐ€ ๋Š‘๋Œ€์˜ ์ˆ˜๋ณด๋‹ค ๋งŽ๋‹ค๋ฉด ์ด๊ธฐ๊ณ , ๋Š‘๋Œ€๋ฅผ ์šฐ๋ฆฌ์—์„œ ์ซ“์•„๋‚ธ๋‹ค. ๊ทธ๋ ‡์ง€ ์•Š๋‹ค๋ฉด ๋Š‘๋Œ€๊ฐ€ ๊ทธ ์ง€์—ญ ์•ˆ์˜ ๋ชจ๋“  ์–‘์„ ๋จน๋Š”๋‹ค.

     

    ๋งจ ์ฒ˜์Œ ๋ชจ๋“  ์–‘๊ณผ ๋Š‘๋Œ€๋Š” ๋งˆ๋‹น ์•ˆ ์˜์—ญ์— ์กด์žฌํ•œ๋‹ค.

     

    ์•„์นจ์ด ๋„๋‹ฌํ–ˆ์„ ๋•Œ ์‚ด์•„๋‚จ์€ ์–‘๊ณผ ๋Š‘๋Œ€์˜ ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜๋ผ.

    ์ž…๋ ฅ

    ์ฒซ ์ค„์—๋Š” ๋‘ ์ •์ˆ˜ R๊ณผ C๊ฐ€ ์ฃผ์–ด์ง€๋ฉฐ(3 ≤ R, C ≤ 250), ๊ฐ ์ˆ˜๋Š” ๋งˆ๋‹น์˜ ํ–‰๊ณผ ์—ด์˜ ์ˆ˜๋ฅผ ์˜๋ฏธํ•œ๋‹ค.

    ๋‹ค์Œ R๊ฐœ์˜ ์ค„์€ C๊ฐœ์˜ ๊ธ€์ž๋ฅผ ๊ฐ€์ง„๋‹ค. ์ด๋“ค์€ ๋งˆ๋‹น์˜ ๊ตฌ์กฐ(์šธํƒ€๋ฆฌ, ์–‘, ๋Š‘๋Œ€์˜ ์œ„์น˜)๋ฅผ ์˜๋ฏธํ•œ๋‹ค.

    ์ถœ๋ ฅ

    ํ•˜๋‚˜์˜ ์ค„์— ์•„์นจ๊นŒ์ง€ ์‚ด์•„์žˆ๋Š” ์–‘๊ณผ ๋Š‘๋Œ€์˜ ์ˆ˜๋ฅผ ์˜๋ฏธํ•˜๋Š” ๋‘ ์ •์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

    ์˜ˆ์ œ ์ž…๋ ฅ 1

    6 6
    ...#..
    .##v#.
    #v.#.#
    #.o#.#
    .###.#
    ...###

    ์˜ˆ์ œ ์ถœ๋ ฅ 1

    0 2

    ์˜ˆ์ œ ์ž…๋ ฅ 2

    8 8
    .######.
    #..o...#
    #.####.#
    #.#v.#.#
    #.#.o#o#
    #o.##..#
    #.v..v.#
    .######.

    ์˜ˆ์ œ ์ถœ๋ ฅ 2

    3 1

    ์˜ˆ์ œ ์ž…๋ ฅ 3

    9 12
    .###.#####..
    #.oo#...#v#.
    #..o#.#.#.#.
    #..##o#...#.
    #.#v#o###.#.
    #..#v#....#.
    #...v#v####.
    .####.#vv.o#
    .......####.

    ์˜ˆ์ œ ์ถœ๋ ฅ 3

    3 5

     


     

    ๐Ÿ“Œ ํ’€์ด

    ๐Ÿ’ฌ Code

    import sys
    from collections import deque
    input = sys.stdin.readline
    
    
    def bfs(x, y):
        q = deque([(x, y)])
        o = 0
        v = 0
    
        while q:
            x, y = q.popleft()
    
            for i in range(5):
                nx = x + dx[i]
                ny = y + dy[i]
    
                if 0 <= nx < n and 0 <= ny < m and not visited[nx][ny]:
                    if graph[nx][ny] == '#':
                        continue
                    elif graph[nx][ny] == 'v':
                        v += 1
                    elif graph[nx][ny] == 'o':
                        o += 1
                    q.append((nx, ny))
                    visited[nx][ny] = 1
    
        return o, v
    
    
    if __name__ == '__main__':
        n, m = map(int, input().split())
        graph = []
        visited = [[0 for _ in range(m)] for _ in range(n)]
    
        for _ in range(n):
            graph.append(list(input().rstrip()))
    
        dx = [0, -1, 1, 0, 0]
        dy = [0, 0, 0, -1, 1]
    
        sheep, wolf = 0, 0
    
        for i in range(n):
            for j in range(m):
                if not visited[i][j] and graph[i][j] != '#':
                    s, w = bfs(i, j)
                    if s > w:
                        sheep += s
                    else:
                        wolf += w
    
        print(sheep, wolf)

    ๐Ÿ’ก Solution

    • graph: ๋งˆ๋‹น ์ƒํƒœ
    • visited: ํ•ด๋‹น ์ขŒํ‘œ์— ๋ฐฉ๋ฌธํ–ˆ๋Š”์ง€ ์ฒดํฌํ•˜๋Š” 2์ฐจ์› ๋ฐฐ์—ด
        for i in range(n):
            for j in range(m):
                if not visited[i][j] and graph[i][j] != '#':
                    s, w = bfs(i, j)
                    if s > w:
                        sheep += s
                    else:
                        wolf += w

    (0, 0)๋ถ€ํ„ฐ ์ขŒํ‘œ ์ฒดํฌ๋ฅผ ๋Œ๋ฆฝ๋‹ˆ๋‹ค. ๋งŒ์•ฝ (0, 0)์— ์•„์ง ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์•˜๊ณ  ์ด ์ขŒํ‘œ์— ๋†“์—ฌ์žˆ๋Š” ๊ฒƒ์ด ์šธํƒ€๋ฆฌ๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด bfs(0, 0)์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. bfs ํ•จ์ˆ˜์—์„œ๋Š” ํ•œ ์˜์—ญ์˜ ์ขŒํ‘œ๋ฅผ ๋ชจ๋‘ ๋ฐฉ๋ฌธํ•˜๋ฉด์„œ ์–‘๊ณผ ๋Š‘๋Œ€์˜ ์ˆ˜๋ฅผ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค. 2์ค‘ for๋ฌธ์—์„œ๋Š” ์ด ๋ฆฌํ„ด๊ฐ’์„ ๋ฐ›์•„์„œ ์–‘๊ณผ ๋Š‘๋Œ€์˜ ์‹ธ์›€ ๊ฒฐ๊ณผ๋ฅผ ์กฐ๊ฑด๋ฌธ์œผ๋กœ ์ฒดํฌํ•˜๊ณ  ์‚ด์•„๋‚จ์€ ์–‘๊ณผ ๋Š‘๋Œ€์˜ ์ˆ˜๋ฅผ ์—…๋ฐ์ดํŠธํ•ฉ๋‹ˆ๋‹ค.

    +) ์ฒ˜์Œ์—๋Š” ๋ฐฉํ–ฅ๋ฒกํ„ฐ์— ์ƒํ•˜์ขŒ์šฐ๋งŒ ๋„ฃ์—ˆ์—ˆ๋Š”๋ฐ,

    .#.
    #o#
    .#.

    ์ฒ˜๋Ÿผ ์šธํƒ€๋ฆฌ ์‚ฌ์ด์— ํ•œ ๋งˆ๋ฆฌ๋งŒ ๋‘˜๋Ÿฌ์‹ธ์—ฌ ์žˆ๋Š” ๊ฒฝ์šฐ์—๋Š” ํ•ด๋‹น ์ขŒํ‘œ๋ฅผ ์ฒดํฌํ•˜์ง€ ๋ชปํ•˜๊ณ  ์ด๋™์ขŒํ‘œ๋งŒ ์ฒดํฌํ•ด์„œ ๋„˜์–ด๊ฐ€๊ธธ๋ž˜ ๋ฐฉํ–ฅ๋ฒกํ„ฐ์— ์ œ์ž๋ฆฌ๋„ ์ถ”๊ฐ€ํ•ด์คฌ์Šต๋‹ˆ๋‹ค.

    ๋Œ“๊ธ€

Designed by Tistory.