ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [๋ฐฑ์ค€] 6987 ์›”๋“œ์ปต with Python
    PS 2021. 9. 22. 23:14
    728x90
    ๋ฐ˜์‘ํ˜•

    ๐Ÿ“Œ BOJ 6987 ์›”๋“œ์ปต

    ๐Ÿ’ก ์กฐ๊ฑด ๋ฐ ํ’€์ด

    1. 6๊ฐœ์˜ ๊ตญ๊ฐ€๊ฐ€ ์žˆ๊ณ , ์ด 18๋ฒˆ์˜ ๊ฒฝ๊ธฐ๋ฅผ ํ•œ๋‹ค.
    2. ์Šน, ๋ฌด, ํŒจ์˜ ๊ฒฐ๊ณผ๊ฐ€ ์žˆ์œผ๋ฉฐ, ์Šน, ๋ฌด, ํŒจ์˜ ์ˆ˜๋Š” 6๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜ ๋˜๋Š” 0
    3. ๋ฐฑํŠธ๋ž˜ํ‚น ์œ ํ˜•์˜ ๋ฌธ์ œ
    4. ์ž…๋ ฅ์€ ๋„ค ์ค„๋กœ ๋“ค์–ด์˜ค๋ฉฐ, ๊ฐ ์ค„์— ๋Œ€ํ•ด ๊ฐ€๋Šฅํ•œ ๊ฒฐ๊ณผ 1, ๋ถˆ๊ฐ€๋Šฅํ•œ ๊ฒฐ๊ณผ 0 ์„์ถœ๋ ฅํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค

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

    from sys import stdin
    from itertools import combinations as cb
    
    
    def solution(round):
        global ans
        if round == 15:
            ans = 1
            for sub in res:
                if sub.count(0) != 3:
                    ans = 0
                    break
            return
    
        t1, t2 = game[round]
        for x, y in ((0, 2), (1, 1), (2, 0)):
            if res[t1][x] > 0 and res[t2][y] > 0:
                res[t1][x] -= 1
                res[t2][y] -= 1
                solution(round + 1)
                res[t1][x] += 1
                res[t2][y] += 1
    
    
    answer = []
    game = list(cb(range(6), 2))
    # ๋ฐฑํŠธ๋ž˜ํ‚น
    for _ in range(4):
        data = list(map(int, stdin.readline().split()))
        res = [data[i:i + 3] for i in range(0, 16, 3)]
        ans = 0
        solution(0)
        answer.append(ans)
    
    print(*answer)

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

    ์˜ˆ์ œ

    5 0 0 3 0 2 2 0 3 0 0 5 4 0 1 1 0 4
    4 1 0 3 0 2 4 1 0 1 1 3 0 0 5 1 1 3
    5 0 0 4 0 1 2 2 1 2 0 3 1 0 4 0 0 5
    5 0 0 3 1 1 2 1 2 2 0 3 0 0 5 1 0 4

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

    1 1 0 0

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

    1. data ๋ณ€์ˆ˜์— ๊ฐ ๋‚˜๋ผ์˜ ์ผ์ •์„ ๋‹ด๊ณ , res ๋ฒ ์—ด์— 3๊ฐœ์”ฉ ์ชผ๊ฐœ์–ด ๋‹ค์‹œ ๋„ฃ๋Š”๋‹ค.
    2. ๊ฒฐ๊ณผ๋ฅผ ๋‹ด์„ ans ๋ณ€์ˆ˜๋ฅผ 0์œผ๋กœ ์ดˆ๊ธฐํ™” ์‹œํ‚จ๋‹ค.
    3. ํŒ€๋ผ๋ฆฌ์˜ ๊ฒฝ๊ธฐ์˜ ์กฐํ•ฉ์„ ์œ„ํ•ด itertools์˜ combinations๋ฅผ ์‚ฌ์šฉ.
      game์ด๋ผ๋Š” ๋ณ€์ˆ˜์— 0~5๋ฒˆ์˜ ๊ตญ๊ฐ€๊ฐ€ ๊ฒฝ๊ธฐ๋ฅผ ํ•  ์ˆ˜ ์žˆ๋Š” ์กฐํ•ฉ์„ ๋งŒ๋“ค์–ด ์ €์žฅํ•œ๋‹ค.
    4. solution ํ•จ์ˆ˜์—์„œ ์ด ๋ผ์šด๋“œ๋ฅผ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์ž…๋ ฅ๋ฐ›์œผ๋ฉฐ, ์ดˆ๊ธฐ์˜ ๊ฐ’์€ 0์ด๋‹ค.
    5. ๊ฐ ๋ผ์šด๋“œ๋ฅผ ์ˆœํšŒํ•˜๋ฉด์„œ res ๋ฐฐ์—ด์˜ ๊ฐ’์„ ๋นผ์ฃผ๋ฉด์„œ ์Šน, ๋ฌด, ํŒจ ์˜ ๊ฐ’์ด ๋‚จ์•„์žˆ๋‹ค๋ฉด
      ans = 0์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•ด์„œ ๋ถˆ๊ฐ€๋Šฅํ•œ ๊ฒฝ๊ธฐ๋ผ๊ณ  answer ๋ฆฌ์ŠคํŠธ์— ์ €์žฅํ•˜๋ฉด ๋œ๋‹ค.
    6. ์Šน์— ํ•ด๋‹นํ•˜๋Š” ์›์†Œ๋ฅผ -1 ํ•  ๋•Œ, ํŒจ์— ํ•ด๋‹นํ•˜๋Š” ์›์†Œ๋ฅผ -1 ํ•ด์ค€๋‹ค.
      ๋ฌด์— ํ•ด๋‹นํ•˜๋Š” ์›์†Œ๋ฅผ -1 ํ•  ๋•Œ, ๋ฌด์— ํ•ด๋‹นํ•˜๋Š” ๋‹ค๋ฅธ ์›์†Œ๋ฅผ -1 ํ•ด์ค€๋‹ค.
    7. round ๊ฐ’์ด 15๋ผ์šด๋“œ๊ฐ€ ๋˜์—ˆ๋‹ค๋ฉด ans์˜ ๊ฐ’์„ 1๋กœ ๋ณ€๊ฒฝํ•˜๊ณ  ๊ฒ€์‚ฌ๋ฅผ ์‹œ์ž‘ํ•œ๋‹ค.
      res ๋ณ€์ˆ˜์— ์žˆ๋Š” 0์˜ ๊ฐ’์ด 3๊ฐœ๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด ans ๋ฅผ 0์œผ๋กœ ๋ณ€๊ฒฝ

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

    • ๋ฐฑํŠธ๋ž˜ํ‚น์„ ์œ„ํ•ด ์žฌ๊ท€ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•˜์—ฌ ์กฐ๊ฑด์„ ํ’€์–ด๋‚ด๋Š” ์ผ๋ จ์˜ ๊ณผ์ •์ด ํž˜๊ฒน๋‹ค.
      ์กฐ๊ธˆ ๋” ๋ฐฑํŠธ๋ž˜ํ‚น ๋ฐ ์žฌ๊ท€์— ๊ด€ํ•œ ๋ฌธ์ œ๋ฅผ ํ’€์–ด๋ณด์•„์•ผ๊ฒ ๋‹ค.
    • ๋ฌธ์ œ๋ฅผ ์กฐ๊ธˆ ๋” ๋‚ด๊ฐ€ ์Šค์Šค๋กœ๋„ ๋‚ฉ๋“ํ•˜๊ณ  ์ดํ•ดํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ’€์–ด๋‚ด๋Š” ๋ฐฉ๋ฒ•์„ ์ƒ๊ฐํ•ด๋ด์•ผ๊ฒ ๋‹ค.
    • ๋ฌธ์ œ ๋ฐ ๋กœ์ง์— ๋Œ€ํ•ด ์ƒ๊ฐํ•˜๋Š” ์‹œ๊ฐ„์ด ๋„ˆ๋ฌด ์งง๊ณ , ๋ฌธ์ œ๋ฅผ ํ’€๊ธฐ ์œ„ํ•ด ์†๋ถ€ํ„ฐ ๋‚˜๊ฐ€๋Š” ๋‚˜์œ ์Šต๊ด€์„ ๊ณ ์ณ์•ผ๊ฒ ๋‹ค.
    • ์•„์ด๋””์–ด๋ฅผ ๋– ์˜ฌ๋ฆฌ์ง€ ๋ชปํ•ด ๊ณ ์ƒ์„ ๋งŽ์ด ํ–ˆ๋˜ ๋ฌธ์ œ์ธ ๊ฒƒ ๊ฐ™๋‹ค.
      ๋ธ”๋กœ๊ทธ์— ๊ธ€์„ ํฌ์ŠคํŒ…ํ•˜๋ฉด์„œ ๋‹ค์‹œ ํ•œ ๋ฒˆ ์ •๋ฆฌํ•˜๋‹ˆ ๋„์›€์ด ๋˜๋Š” ๊ฒƒ ๊ฐ™๋‹ค.
    ๋ฐ˜์‘ํ˜•

    ๋Œ“๊ธ€

Designed by Tistory.