๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm/๋ฐฑ์ค€(BOJ)

[ ํŒŒ์ด์ฌ(python) ] ๋ฐฑ์ค€ 2578 - ๋น™๊ณ 

by YWTechIT 2021. 5. 28.
728x90

๐Ÿ“ ๋ฐฑ์ค€ 2578 - ๋น™๊ณ 

๋ฐฑ์ค€ 2578 - ๋น™๊ณ 


โšก๏ธ ๋‚˜์˜ ํ’€์ด

๊ตฌํ˜„๋ ฅ์„ ๋ฌป๋Š” ๋ฌธ์ œ์˜€๋Š”๋ฐ, 21.5.17.๋ถ€ํ„ฐ ํ’€๊ธฐ ์‹œ์ž‘ํ•˜๋‹ค ์ค‘๊ฐ„์— ๋ง‰ํ˜€ boj_10703 ์œ ์„ฑ ๋ฌธ์ œ๋ฅผ ํ’€๊ณ  ๋‹ค์‹œ ํ‘ธ๋‹ˆ๊นŒ ๊ฐ์ด ์žกํ˜€์„œ ์ •๋‹ต ํŒ์ •์„ ๋ฐ›์€ ๋ฌธ์ œ๋‹ค. ์ด ๋ฌธ์ œ์˜ ํ•ต์‹ฌ ๋ถ€๋ถ„์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

 

  1. ์‚ฌํšŒ์ž๊ฐ€ ๋ถ€๋ฅด๋Š” ์ˆ˜๋ฅผ ์ฐจ๋ก€๋กœ ์ง€์›Œ๊ฐ€๋ฉด์„œ check ํ•œ๋‹ค.(์ˆ˜๊ฐ€ ๋ถˆ๋ฆฌ๋ฉด 0์œผ๋กœ ์ดˆ๊ธฐํ™”์‹œํ‚จ๋‹ค.)
  2. ๋น™๊ณ ๋ฅผ check ํ•  ๋•Œ ๊ฐ€๋กœ, ์„ธ๋กœ, ๋Œ€๊ฐ์„  ๊ฐ๊ฐ checkํ•œ๋‹ค. ์ด๋•Œ, ํ•œ ์ค„ ํ˜น์€ ํ•œ ์—ด ํ˜น์€ ํ•œ ๋Œ€๊ฐ์„ ์—์„œ 0์ด 5๊ฐœ๊ฐ€ ๋‚˜์˜ค๋ฉด bingo+=1์„ ํ•œ๋‹ค.
  3. bingo >= 3์ด๋ฉด ํ•ด๋‹น ์ˆซ์ž๊ฐ€ ๋ช‡ ๋ฒˆ์งธ cnt๋œ ์ˆซ์ž์ธ์ง€ ์ถœ๋ ฅํ•œ๋‹ค.

 

๋‹ค์Œ์œผ๋กœ ๊ฐ€๋กœ, ์„ธ๋กœ, ๋Œ€๊ฐ์„ ์—์„œ bingo๋ฅผ ํŒ๋‹จํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ดํŽด๋ณด์ž.

  1. ๊ฐ€๋กœ(row): count.('[0, 0, 0, 0, 0]')์ด ๋˜๋ฉด +1์„ ํ•ด์ฃผ์—ˆ๋‹ค. ์ฃผ์˜ํ•  ์ ์€ ํŒ๋ณ„ํ•˜๋ ค๋Š” ๊ฐ’์„ strํ˜•์œผ๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค์Œ count๋ฅผ ์‚ฌ์šฉํ•˜์ž.
  2. ์„ธ๋กœ(column): arr์„ ์„ธ๋กœ๋กœ ๋Œ๋ ค์•ผํ•˜๋Š”๋ฐ lambda ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์—ด๋กœ ๋งŒ๋“ค์–ด์ฃผ๊ณ  count ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค.
  3. ๋Œ€๊ฐ์„ (diagonal): ๋Œ€๊ฐ์„  ์ „์ฒด์˜ ํ•ฉ์ด 0์ด ๋˜๋Š”์ง€๋ฅผ ํ™•์ธํ–ˆ๋‹ค. ์ • ๋Œ€๊ฐ์„  ๋ฐฉํ–ฅ, ์—ญ ๋Œ€๊ฐ์„  ๋ฐฉํ–ฅ ๋‘ ๊ฐ€์ง€์˜ ๊ฒฝ์šฐ๋ฅผ ํŒ๋‹จํ•ด์•ผ ํ•œ๋‹ค.

 

์ฒ˜์Œ์—๋Š” ์ • ๋Œ€๊ฐ์„ , ์—ญ ๋Œ€๊ฐ์„  ํŒ๋‹จ ๋ณ€์ˆ˜๋ฅผ ๊ฐ๊ฐ ๋”ฐ๋กœ ์„ค์ •ํ–ˆ๋Š”๋ฐ, ๊ทธ๋Ÿด ํ•„์š”๊ฐ€ ์—†์ด ๋‘ ๊ฒฝ์šฐ๋ฅผ ํ•ฉ์ณ์„œ index๋ฅผ 2๊ฐœ ์ค˜๋„ ๋์—ˆ๋‹ค.

 

๋ฌธ์ œ๋ฅผ ํ’€๋‹ค ๋ณด๋ฉด ๋Š๋ผ๊ฒ ์ง€๋งŒ ์ด๋ฏธ bingo๊ฐ€ ๋˜์—ˆ๋˜ ๊ฐ’์„ ๋˜ ๋ˆ„์ ์‹œํ‚ค๋Š” ์ƒํ™ฉ์ด ์ƒ๊ธด๋‹ค. (์ด ๋ฌธ์ œ๋ฅผ ํ‘ธ๋Š”๋ฐ ์˜ค๋ž˜ ๊ฑธ๋ฆฐ ์ด์œ ๋„ ์—ฌ๊ธฐ์„œ ๋ง‰ํ˜”๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.) ๊ฒฐ๋ก ์ ์œผ๋กœ check_row, check_column, check_diagonal ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด์„œ bingo๊ฐ€ ๋˜๋Š” ํ–‰, ์—ด, ๋Œ€๊ฐ์„  index๋Š” True๋กœ ๋ฐ”๊ฟ”๋†“๊ณ  ๋‹ค์Œ bingo๋ฅผ ํŒ๋‹จํ•˜๋Š” ์กฐ๊ฑด์€ bingo์ด๋ฉด์„œ ํ–‰, ์—ด, ๋Œ€๊ฐ์„  index๊ฐ€ False์ธ ๊ฐ’๋งŒ ํ†ต๊ณผํ•˜๊ฒŒ ์„ค์ •ํ–ˆ๋”๋‹ˆ ์ด์ „ bingo๊ฐ€ ๋ˆ„์ ์ด ๋˜๋Š” ์ผ์€ ์—†์—ˆ๋‹ค.

 

๋งˆ์ง€๋ง‰์œผ๋กœbingo๊ฐ€ 3๊ฐœ ์ด์ƒ๋˜๋ฉด break ๋Œ€์‹  exit๋ฅผ ์„ ์–ธํ•ด์„œ ์ „์ฒด ๋ฐ˜๋ณต๋ฌธ์„ ๋๋‚ด๋ฒ„๋ ธ๋‹ค.

 

์ •๋‹ต ํŒ์ •์„ ๋ฐ›๊ณ  ๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ์ฝ”๋“œ๋ฅผ ๋ณด๋‹ค๊ฐ€ ์ฒ˜์Œ 4์ค‘ ๋ฐ˜๋ณต๋ฌธ์„ 3์ค‘ ๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ๋Š์–ด๋†“์€ ์‚ฌ๋žŒ๋„ ์žˆ์—ˆ๋‹ค. ๊ทธ ์ ์„ ์ฐธ๊ณ ํ•ด์„œ ์‚ด์ง ์ˆ˜์ •ํ–ˆ๋Š”๋ฐ, MC์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ 2์ฐจ์›์ด ์•„๋‹Œ 1์ฐจ์›์œผ๋กœ ์„ ์–ธํ•˜๊ณ  ์ฒ˜์Œ ๋ฐ˜๋ณต๋ฌธ์˜ ๋ฒ”์œ„๋ฅผ 25๊นŒ์ง€ ์„ค์ •ํ•ด๋†“์€ ๋‹ค์Œ์— ์ˆœ์„œ๋Œ€๋กœ index๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ๋ฐฉ๋ฒ•์ด์—ˆ๋‹ค. ์‚ฌํšŒ์ž๋Š” ์ฐจ๋ก€๋Œ€๋กœ ์ˆซ์ž๋ฅผ ๋ถ€๋ฅด๊ธฐ ๋•Œ๋ฌธ์— ๊ตณ์ด 2์ฐจ์› ๋ฐฐ์—ด์„ ์„ ์–ธํ•  ํ•„์š”๊ฐ€ ์—†์—ˆ๋‹ค. (์–ด๋–ป๊ฒŒ ์ด๋Ÿฐ ์ƒ๊ฐ์„.. ใ„ทใ„ท ๐Ÿฅถ ๐Ÿฅถ)

 

์ „์ฒด์ฝ”๋“œ๋Š” ํ•˜๋‹จ์„ ์ฐธ๊ณ ํ•˜์ž.

 

# ๋‚˜์˜ ์ฝ”๋“œ
n = 5
arr = [list(map(int, input().split())) for _ in range(n)]
MC = [list(map(int, input().split())) for _ in range(n)]

bingo, order = 0, 0
check_row, check_column, check_diagonal = [False] * n, [False] * n, [False] * 2

def check(arr):
    global bingo, check_diagonal
    temp_right, temp_reverse = 0, 0

    for i in range(n):
        if str(arr[i]).count('[0, 0, 0, 0, 0]') and not check_row[i]:  # check row
            bingo += 1
            check_row[i] = True

        temp_column = list(map(lambda x: x[i], arr))  # check column
        if str(temp_column).count('[0, 0, 0, 0, 0]') and not check_column[i]:
            bingo += 1
            check_column[i] = True

        temp_right += arr[i][i]    # check diagonal
        temp_reverse += arr[i][n - i - 1]

    if not temp_right and not check_diagonal[0]:
        check_diagonal[0] = True
        bingo += 1

    if not temp_reverse and not check_diagonal[1]:
        check_diagonal[1] = True
        bingo += 1

    return bingo

for numbers in MC:
    for target in numbers:
        for i in range(n):
            for j in range(n):
                if target == arr[i][j]:
                    arr[i][j] = 0
                    check(arr)
                    order += 1
                    if bingo >= 3:
                        print(order)
                        exit()
# ๋‚ด ์ฝ”๋“œ + ๋‹ค๋ฅธ์‚ฌ๋žŒ์˜ ์ฝ”๋“œ
n = 5
arr = [list(map(int, input().split())) for _ in range(n)]

MC = []
for _ in range(5):
    MC += list(map(int, input().split()))

bingo, order = 0, 0
check_row, check_column, check_diagonal = [False] * n, [False] * n, [False] * 2

def check(arr):
    global bingo, check_diagonal
    temp_right, temp_reverse = 0, 0

    for i in range(n):
        if str(arr[i]).count('[0, 0, 0, 0, 0]') and not check_row[i]:  # check row
            bingo += 1
            check_row[i] = True

        temp_column = list(map(lambda x: x[i], arr))  # check column
        if str(temp_column).count('[0, 0, 0, 0, 0]') and not check_column[i]:
            bingo += 1
            check_column[i] = True

        temp_right += arr[i][i]    # check diagonal
        temp_reverse += arr[i][n - i - 1]

    if not temp_right and not check_diagonal[0]:
        check_diagonal[0] = True
        bingo += 1

    if not temp_reverse and not check_diagonal[1]:
        check_diagonal[1] = True
        bingo += 1

    return bingo

for target in range(25):
    for i in range(5):
        for j in range(5):
            if MC[target] == arr[i][j]:
                arr[i][j] = 0
                check(arr)
                order += 1
                if bingo >= 3:
                    print(order)
                    exit()
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€