๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐˜ผ๐™ก๐™œ๐™ค๐™ง๐™ž๐™ฉ๐™๐™ข/๐™‹๐™ง๐™ค๐™œ๐™ง๐™–๐™ข๐™ข๐™š๐™ง๐™จ

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค 'ํ˜ผ์ž์„œ ํ•˜๋Š” ํ‹ฑํƒํ† ' ํŒŒ์ด์ฌ ํ’€์ด

by beomcoder 2023. 4. 26.
728x90
๋ฐ˜์‘ํ˜•
https://school.programmers.co.kr/learn/courses/30/lessons/160585
 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

 

def solution(b):
    o, x = sum([arr.count('O') for arr in b]), sum([arr.count('X') for arr in b])
    if 0 <= o-x <= 1:
        rb = [b[0][i]+b[1][i]+b[2][i] for i in range(3)]
        winO, winX = 0, 0
        for i, j in zip(b, rb):
            if 'OOO' in [i,j]: winO += 1
            if 'XXX' in [i,j]: winX += 1
        
        diagonal = [b[0][0] + b[1][1] + b[2][2], b[0][2] + b[1][1] + b[2][0]]
        winO += diagonal.count('OOO')
        winX += diagonal.count('XXX')
        
        if winX and winO: return 0
        if winO and winX == 0 and o==x: return 0
        if winX and winO == 0 and o!=x: return 0
        
        return 1
    
    return 0

"""
ํ‹ฑํƒํ† ๊ฐ€ ๋งž๋Š” ๊ทœ์น™์€ ์–ผ๋งˆ ์—†๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ๋‹ค.

1. O๊ฐ€ ์„ ๊ณต์ด๊ธฐ ๋•Œ๋ฌธ์— X์™€ ๊ฐœ์ˆ˜๊ฐ€ ๊ฐ™๊ฑฐ๋‚˜ O๊ฐ€ 1๊ฐœ ๋” ๋งŽ์•„์•ผ ํ•จ.
2. O์™€ X๊ฐ€ ๋‘˜๋‹ค 3๊ฐœ๊ฐ€ ์—ฐ์†์œผ๋กœ ์žˆ์„ ์ˆ˜ ์—†๋‹ค.
3. O๊ฐ€ ์ด๊ฒผ๋‹ค๋ฉด O์˜ ๊ฐœ์ˆ˜๊ฐ€ 1๊ฐœ ๋” ๋งŽ์•„์•ผ ํ•จ.
4. X๊ฐ€ ์ด๊ฒผ๋‹ค๋ฉด X์˜ ๊ฐœ์ˆ˜๊ฐ€ O์™€ ๊ฐ™์•„์•ผํ•จ.

def solution(b):
    o, x = sum([arr.count('O') for arr in b]), sum([arr.count('X') for arr in b])
    # ๋จผ์ € O์™€ X์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•ด์ฃผ์–ด 1๋ฒˆ์˜ ๊ทœ์น™์„ ํ†ต๊ณผ์‹œํ‚ด.
    
    if 0 <= o-x <= 1:
        rb = [b[0][i]+b[1][i]+b[2][i] for i in range(3)]
        # ๊ฐ€๋กœ๋Š” 'OOO'์œผ๋กœ ๋ณผ ์ˆ˜ ์žˆ์ง€๋งŒ, ์„ธ๋กœ๋Š” ๋ฌธ์ž์—ด๋กœ ํ™•์ธํ•˜๊ธฐ ์–ด๋ ค์›Œ์„œ
        # ๊ฐ€๋กœ์™€ ์„ธ๋กœ๋ฅผ ๋ฐ”๊พผ board๋ฅผ reverse board์˜ ์•ฝ์ž์ธ rb๋กœ ๋‹ค์‹œ ๋งŒ๋“ค์–ด์ฃผ์—ˆ๋‹ค.
        
        # O..      OOO
        # O..  --> ...
        # O..      ...
        
        winO, winX = 0, 0 
        # O๊ฐ€ ์ด๊ธด๊ฒฝ์šฐ์™€ X๊ฐ€ ์ด๊ธด๊ฒฝ์šฐ๋ฅผ ์ฒดํฌํ•ด์ฃผ๊ธฐ ์œ„ํ•ด ๋ณ€์ˆ˜ ์„ ์–ธ
        
        for i, j in zip(b, rb): 
        # ์›๋ž˜ ๋ณด๋“œํŒ๊ณผ ๊ฐ€๋กœ,์„ธ๋กœ๋ฅผ ๋ฐ”๊พผ ๋ณด๋“œํŒ์„ ๋ฌถ์–ด์„œ for๋ฌธ์„ ๋Œ๋ฆผ
            if 'OOO' in [i,j]: winO += 1 # 'OOO'๊ฐ€ ์žˆ๋‹ค๋ฉด O๊ฐ€ ์ด๊ธดํšŸ์ˆ˜ 1์ฆ๊ฐ€
            if 'XXX' in [i,j]: winX += 1 # 'XXX'๊ฐ€ ์žˆ๋‹ค๋ฉด X๊ฐ€ ์ด๊ธดํšŒ์ˆซ 1์ฆ๊ฐ€
        
        diagonal = [b[0][0] + b[1][1] + b[2][2], b[0][2] + b[1][1] + b[2][0]]
        # ์ง์„ ์œผ๋กœ ์žˆ๋Š” ๊ฒฝ์šฐ๋„ ์žˆ์ง€๋งŒ, ๋Œ€๊ฐ์„ ์œผ๋กœ ์ด๊ธด ๊ฒฝ์šฐ๋„ ์žˆ์–ด์„œ ๊ทธ ๊ฒฝ์šฐ๋ฅผ ๋„ฃ์–ด์คŒ.
        # ์˜ˆ์‹œ↓
        #  OXO
        #  XOX
        #  OXO
        
        # ๋Œ€๊ฐ์„ ์œผ๋กœ ์ด๊ธด๊ฒฝ์šฐ ๋”ํ•ด์คŒ
        winO += diagonal.count('OOO') 
        winX += diagonal.count('XXX')
        
        
        if winX and winO: return 0 
        # ๋‘˜๋‹ค ์ด๊ธด ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค๋ฉด 0์„ ๋ฐ˜ํ™˜ (2๋ฒˆ์˜ ๊ฒฝ์šฐ)
        
        if winO and winX == 0 and o==x: return 0 
        # O๊ฐ€ ์ด๊ฒผ๋Š”๋ฐ O์˜ ๊ฐœ์ˆ˜๊ฐ€ ๊ฐ™์€ ๊ฒฝ์šฐ 0 ๋ฐ˜ํ™˜ (3๋ฒˆ์˜ ๊ฒฝ์šฐ)
        
        if winX and winO == 0 and o!=x: return 0 
        # X๊ฐ€ ์ด๊ฒผ๋Š”๋ฐ O์˜ ๊ฐœ์ˆ˜๊ฐ€ ๋” ๋งŽ์€ ๊ฒฝ์šฐ 0 ๋ฐ˜ํ™˜ (4๋ฒˆ์˜ ๊ฒฝ์šฐ)
        
        # ๋‚˜๋จธ์ง€ ๊ฒฝ์šฐ๋ผ๋ฉด ์ •์ƒ์ ์ธ ํ‹ฑํƒํ† ์—์„œ ๋ณด์—ฌ์ง€๋Š” ๊ฒฝ์šฐ๋ผ์„œ 1 ๋ฐ˜ํ™˜
        return 1
    
    # O์™€ X์˜ ๊ฐœ์ˆ˜๊ฐ€ ๋น„์ •์ƒ์ ์ธ ๊ฒฝ์šฐ
    return 0
"""
728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€