๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
4๏ธโƒฃ Python/Problem Solving

[Python Algorithm] ๊ฒฝ๋กœ ์ฐพ๊ธฐ BOJ #11403 (feat. BFS, ํ”Œ๋กœ์ด๋“œ ์›Œ์…œ)

by seolhee2750 2022. 6. 23.
๋ฌธ์ œ

https://www.acmicpc.net/problem/11403

 

11403๋ฒˆ: ๊ฒฝ๋กœ ์ฐพ๊ธฐ

๊ฐ€์ค‘์น˜ ์—†๋Š” ๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„ G๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ๋ชจ๋“  ์ •์  (i, j)์— ๋Œ€ํ•ด์„œ, i์—์„œ j๋กœ ๊ฐ€๋Š” ๊ฒฝ๋กœ๊ฐ€ ์žˆ๋Š”์ง€ ์—†๋Š”์ง€ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

www.acmicpc.net

 

๋‚ด ๋ฌธ์ œ ํ’€์ด (BFS)
import sys
sys.setrecursionlimit(10 ** 6)

n = int(sys.stdin.readline().strip())
table = []
for _ in range(n):
    table.append(list(map(int, sys.stdin.readline().strip().split())))
result = table

def bfs(queue, find, checkList):
    idx = 0
    check = False

    while idx < len(queue):
        now = queue[idx]
        idx += 1

        if 1 not in table[now]:
            continue
        else:
            for x in range(n):
                if table[now][x] == 1 and checkList[now][x] is False:
                    checkList[now][x] = True
                    if x == find:
                        check = True
                        break
                    else:
                        queue.append(x)
                if check:
                    break

        if check:
            break

    if check:
        return 1
    else:
        return 0

for i in range(n):
    for j in range(n):
        if result[i][j] == 1:
            continue
        else:
            checkList = [[False for _ in range(n)] for _ in range(n)]
            checkList[i][j] = True
            result[i][j] = bfs([i], j, checkList)

print('\n'.join(' '.join(map(str, i)) for i in result))

๐Ÿ‘‰ BFS๋กœ ํ’€์ด (ํ”Œ๋กœ์ด๋“œ ์›Œ์…œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๋ชฐ๋ผ์„œ bfs๋กœ ํ’€์ดํ–ˆ๋‹ค.)

  • ํ–‰๋ ฌ์„ ๋Œ๋ฉฐ 1์ด ์žˆ์„ ๊ฒฝ์šฐ ๊ฒฝ๋กœ๊ฐ€ ํ™•์ธ๋œ ๊ฒƒ์ด๋ฏ€๋กœ ๊ทธ๋Œ€๋กœ result์— 1์„ ์ €์žฅํ–ˆ๋‹ค.
  • ๊ทธ ์™ธ์˜ ๊ฒฝ์šฐ์—๋Š” bfs ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ฒฝ๋กœ๋ฅผ ํ™•์ธํ•ด์ฃผ์—ˆ๋Š”๋ฐ, ์ค‘๋ณต์„ ํ”ผํ•˜๊ธฐ ์œ„ํ•œ checkList ํ•จ์ˆ˜๋„ ์ƒ์„ฑํ–ˆ๋‹ค.
    ์‹œ์ž‘ํ•˜๋Š” ์ž๋ฆฌ๋งŒ True๋กœ ๋ฐ”๊ฟ”๋†“๊ณ  bfs ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ–ˆ๋‹ค.
  • bfs ํ•จ์ˆ˜์—์„œ๋Š” queue์— ํƒ์ƒ‰์ด ํ•„์š”ํ•œ ํ–‰๋“ค์„ ์ถ”๊ฐ€ํ•˜๋ฉฐ ํ™•์ธํ•ด์ฃผ์—ˆ๋‹ค.
    ์˜ˆ๋ฅผ ๋“ค์–ด a, b ์ž๋ฆฌ์— ๋Œ€ํ•œ ๊ฒฐ๊ณผ๋ฅผ ๊ฐ€์ ธ์˜ค๊ณ ์ž ํ•  ๋•Œ, queue์—๋Š” a๊ฐ€ ์ €์žฅ๋˜์–ด ์žˆ์„ ๊ฒƒ์ด๋‹ค.
    ๊ทธ๋ฆฌ๊ณ  b๊นŒ์ง€ ๋„๋‹ฌํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ค‘๊ฐ„์— ์–ด๋– ํ•œ ๋…ธ๋“œ๋ฅผ ๊ฑฐ์น˜๋“ ์ง€, ๋ฐ”๋กœ ๊ฐ€๋“ ์ง€ ํ•œ ์„ ํƒ์ง€๊ฐ€ ํ•„์š”ํ•˜๋‹ค.
    ๋”ฐ๋ผ์„œ ๊ฐ€์žฅ ๋จผ์ € aํ–‰์— 1์ด ์ €์žฅ๋œ ๊ณณ์ด ์žˆ๋Š”์ง€๋ฅผ ํ™•์ธํ•ด์ค€๋‹ค.
    ๋งŒ์•ฝ 1์ด ํ•˜๋‚˜๋„ ์—†๋‹ค๋ฉด ์—ฐ๊ฒฐ๋  ์ˆ˜ ์žˆ๋Š” ๊ฐ€๋Šฅ์„ฑ์ด ์ „ํ˜€ ์—†๋‹ค๋Š” ๋œป์ด๋ฏ€๋กœ continue ํ•ด์ค€๋‹ค.
    ๊ทธ๋ ‡์ง€ ์•Š์„ ๊ฒฝ์šฐ 1์ด ์ €์žฅ๋œ aํ–‰ ๋ชจ๋“  ์ž๋ฆฌ์˜ j ๊ฐ’์„ queue์— ์ถ”๊ฐ€ํ•˜๋Š”๋ฐ, (๋ชจ๋‘ ๊ฐ€๋Šฅ์„ฑ ์žˆ์œผ๋ฏ€๋กœ)
    ํ˜น์‹œ j ๊ฐ’์ด ์ฒ˜์Œ์— ์ฐพ๊ณ ์ž ํ•œ ๋ชฉ์ ์ง€์ธ b์™€ ๊ฐ™๋‹ค๋ฉด ๋”์ด์ƒ ํƒ์ƒ‰ํ•  ํ•„์š”๊ฐ€ ์—†์œผ๋ฏ€๋กœ ๋ชจ๋‘ ์ข…๋ฃŒํ•œ๋‹ค.
    ์œ„์™€ ๊ฐ™์€ ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•˜์—ฌ ์—ฐ๊ฒฐ ๊ฐ€๋Šฅํ•œ ๊ฒฝ๋กœ๊ฐ€ ์žˆ๋Š”์ง€ ํŒ๋ณ„ํ•ด์ค„ ์ˆ˜ ์žˆ๋‹ค.

 

๋‚ด ๋ฌธ์ œ ํ’€์ด (ํ”Œ๋กœ์ด๋“œ ์›Œ์…œ)
import sys
sys.setrecursionlimit(10 ** 6)

n = int(sys.stdin.readline().strip())
table = []
for _ in range(n):
    table.append(list(map(int, sys.stdin.readline().strip().split())))

for k in range(n):
    for i in range(n):
        for j in range(n):
            if table[i][k] and table[k][j]:
                table[i][j] = 1

print('\n'.join(' '.join(map(str, i)) for i in table))

๐Ÿ‘‰ ํ”Œ๋กœ์ด๋“œ ์›Œ์…œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ด์šฉํ–ˆ๋‹ค.

  • k๋ฅผ ์ค‘๊ฐ„ ๋…ธ๋“œ๋กœ ์ƒ๊ฐํ•˜๊ณ , k๊ฐ’์„ ๊ฑฐ์น˜๋Š” ๊ฐ„์„ ์ด ์žˆ๋Š”์ง€๋ฅผ ํ™•์ธํ–ˆ๋‹ค.
  • 3์ค‘ for๋ฌธ์„ ์ด์šฉํ•˜์—ฌ ๋ชจ๋“  ์ž๋ฆฌ์˜ ์—ฐ๊ฒฐ ๊ฐ€๋Šฅ์„ฑ์„ ํ™•์ธํ•ด์ค„ ์ˆ˜ ์žˆ์—ˆ๋‹ค.
    ์˜ˆ๋ฅผ ๋“ค์–ด 1 -> 3 -> 4 -> 5 ๋กœ ์—ฐ๊ฒฐ๋˜๋Š” ๋…ธ๋“œ๋“ค์ด ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜์ž.
    ์ด ๋•Œ i == 1, j == 5 ์ธ ์ž๋ฆฌ๋Š” ์—ฐ๊ฒฐ์ด ๊ฐ€๋Šฅํ•œ ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.
    ์ด๋ฅผ ํ”Œ๋กœ์ด๋“œ ์›Œ์…œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์— ์ ์šฉํ•˜๋ฉด,
    iํ–‰๊ณผ j์—ด์—์„œ ๊ฐ๊ฐ 1์ด ์กด์žฌํ•˜๋Š”์ง€๋ฅผ ํ™•์ธํ•˜๊ณ , ๋ชจ๋‘ ์กด์žฌํ•œ๋‹ค๋ฉด ์—ฐ๊ฒฐ์ด ๊ฐ€๋Šฅํ•œ ๊ฒƒ์œผ๋กœ ํŒ๋ณ„ํ•œ๋‹ค.

+ ์›๋ž˜ ํ”Œ๋กœ์ด๋“œ ์›Œ์…œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ a -> b ๊ฒฝ๋กœ์™€ a -> c -> b ๊ฒฝ๋กœ ์ค‘ ๋” ๊ฐ€๊นŒ์šด ๊ฒƒ์„ ํŒ๋ณ„ํ•˜๊ณ  ์—…๋ฐ์ดํŠธํ•ด์ฃผ๋Š” ์—ญํ• ์„ ํ•˜๋Š”๋ฐ, ์ด ๋ฌธ์ œ์˜ ๊ฒฝ์šฐ ๊ฑฐ๋ฆฌ๋ฅผ ํŒ๋ณ„ํ•  ํ•„์š”์—†์ด ์—ฐ๊ฒฐ์ด ๊ฐ€๋Šฅํ•œ์ง€๋งŒ ํŒ๋‹จํ•˜๋ฉด ๋˜๋ฏ€๋กœ ์œ„์™€๊ฐ™์ด ํ’€์ดํ•˜๋ฉด ๋œ๋‹ค.

 

๐Ÿ’ก ํ”ผ๋“œ๋ฐฑ
  • bfs๋กœ ํ’€์ดํ–ˆ์„ ๋•Œ๋Š” ์‹œ๊ฐ„๋ณต์žก๋„๋„ ์•ˆ์ข‹์•˜๊ณ  ํ’€์ด๋„ ๋ณต์žกํ–ˆ๋Š”๋ฐ
    ํ”Œ๋กœ์ด๋“œ ์›Œ์…œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ํ™œ์šฉํ•˜๋‹ˆ๊นŒ ํ›จ์”ฌ ํšจ์œจ์ ์œผ๋กœ ํ’€ ์ˆ˜ ์žˆ์—ˆ๋‹ค.

๋Œ“๊ธ€