๋‚ด ์ธ์ƒ์—์„œ ๋ฏฟ์„ ๊ฑด ์˜ค์ง ๋‚˜ ์ž์‹ ๋ฟ!

The only one you can truly trust is yourself.

๊ฒŒ์ž„ ํ”„๋กœ๊ทธ๋ž˜๋ฐ/Python ํ”„๋กœ๊ทธ๋ž˜๋ฐ

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ํ˜ผ์ž์„œ ํ•˜๋Š” ํ‹ฑํƒํ† 

๐ŸŽฎinspirer9 2023. 2. 24. 00:37
728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ ์„ค๋ช…

ํ‹ฑํƒํ† ๋Š” ๋‘ ์‚ฌ๋žŒ์ด ํ•˜๋Š” ๊ฒŒ์ž„์œผ๋กœ ์ฒ˜์Œ์— 3x3์˜ ๋นˆ์นธ์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ๊ฒŒ์ž„ํŒ์— ์„ ๊ณต์ด "O", ํ›„๊ณต์ด "X"๋ฅผ ๋ฒˆ๊ฐˆ์•„๊ฐ€๋ฉด์„œ ๋นˆ์นธ์— ํ‘œ์‹œํ•˜๋Š” ๊ฒŒ์ž„์ž…๋‹ˆ๋‹ค. ๊ฐ€๋กœ, ์„ธ๋กœ, ๋Œ€๊ฐ์„ ์œผ๋กœ 3๊ฐœ๊ฐ€ ๊ฐ™์€ ํ‘œ์‹œ๊ฐ€ ๋งŒ๋“ค์–ด์ง€๋ฉด ๊ฐ™์€ ํ‘œ์‹œ๋ฅผ ๋งŒ๋“  ์‚ฌ๋žŒ์ด ์Šน๋ฆฌํ•˜๊ณ  ๊ฒŒ์ž„์ด ์ข…๋ฃŒ๋˜๋ฉฐ 9์นธ์ด ๋ชจ๋‘ ์ฐจ์„œ ๋” ์ด์ƒ ํ‘œ์‹œ๋ฅผ ํ•  ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ์—๋Š” ๋ฌด์Šน๋ถ€๋กœ ๊ฒŒ์ž„์ด ์ข…๋ฃŒ๋ฉ๋‹ˆ๋‹ค.

ํ•  ์ผ์ด ์—†์–ด ํ•œ๊ฐ€ํ•œ ๋จธ์“ฑ์ด๋Š” ๋‘ ์‚ฌ๋žŒ์ด ํ•˜๋Š” ๊ฒŒ์ž„์ธ ํ‹ฑํƒํ† ๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ˜ผ์ž์„œ ํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

  • ํ˜ผ์ž์„œ ์„ ๊ณต๊ณผ ํ›„๊ณต์„ ๋‘˜ ๋‹ค ๋งก๋Š”๋‹ค.
  • ํ‹ฑํƒํ†  ๊ฒŒ์ž„์„ ์‹œ์ž‘ํ•œ ํ›„ "O"์™€ "X"๋ฅผ ํ˜ผ์ž์„œ ๋ฒˆ๊ฐˆ์•„ ๊ฐ€๋ฉด์„œ ํ‘œ์‹œ๋ฅผ ํ•˜๋ฉด์„œ ์ง„ํ–‰ํ•œ๋‹ค.

ํ‹ฑํƒํ† ๋Š” ๋‹จ์ˆœํ•œ ๊ทœ์น™์œผ๋กœ ๊ฒŒ์ž„์ด ๊ธˆ๋ฐฉ ๋๋‚˜๊ธฐ์— ๋จธ์“ฑ์ด๋Š” ํ•œ ๊ฒŒ์ž„์ด ์ข…๋ฃŒ๋˜๋ฉด ๋‹ค์‹œ 3x3 ๋นˆ์นธ์„ ๊ทธ๋ฆฐ ๋’ค ๋‹ค์‹œ ๊ฒŒ์ž„์„ ๋ฐ˜๋ณตํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ฒŒ ํ‹ฑํƒํ†  ์ˆ˜ ์‹ญ ํŒ์„ ํ–ˆ๋”๋‹ˆ ๋จธ์“ฑ์ด๋Š” ๊ฒŒ์ž„ ๋„์ค‘์— ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ทœ์น™์„ ์–ด๊ธฐ๋Š” ์‹ค์ˆ˜๋ฅผ ํ–ˆ์„ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

  • "O"๋ฅผ ํ‘œ์‹œํ•  ์ฐจ๋ก€์ธ๋ฐ "X"๋ฅผ ํ‘œ์‹œํ•˜๊ฑฐ๋‚˜ ๋ฐ˜๋Œ€๋กœ "X"๋ฅผ ํ‘œ์‹œํ•  ์ฐจ๋ก€์ธ๋ฐ "O"๋ฅผ ํ‘œ์‹œํ•œ๋‹ค.
  • ์„ ๊ณต์ด๋‚˜ ํ›„๊ณต์ด ์Šน๋ฆฌํ•ด์„œ ๊ฒŒ์ž„์ด ์ข…๋ฃŒ๋˜์—ˆ์Œ์—๋„ ๊ทธ ๊ฒŒ์ž„์„ ์ง„ํ–‰ํ•œ๋‹ค.

๊ฒŒ์ž„ ๋„์ค‘ ๊ฒŒ์ž„ํŒ์„ ๋ณธ ์–ด๋Š ์ˆœ๊ฐ„ ๋จธ์“ฑ์ด๋Š” ๋ณธ์ธ์ด ์‹ค์ˆ˜๋ฅผ ํ–ˆ๋Š”์ง€ ์˜๋ฌธ์ด ์ƒ๊ฒผ์Šต๋‹ˆ๋‹ค. ํ˜ผ์ž์„œ ํ‹ฑํƒํ† ๋ฅผ ํ–ˆ๊ธฐ์— ๊ฒŒ์ž„ํ•˜๋Š” ๊ณผ์ •์„ ์ง€์ผœ๋ณธ ์‚ฌ๋žŒ์ด ์—†์–ด ์ด๋ฅผ ์•Œ ์ˆ˜๋Š” ์—†์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๊ฒŒ์ž„ํŒ๋งŒ ๋ดค์„ ๋•Œ ์‹ค์ œ๋กœ ํ‹ฑํƒํ†  ๊ทœ์น™์„ ์ง€์ผœ์„œ ์ง„ํ–‰ํ–ˆ์„ ๋•Œ ๋‚˜์˜ฌ ์ˆ˜ ์žˆ๋Š” ์ƒํ™ฉ์ธ์ง€๋Š” ํŒ๋‹จํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™๊ณ  ๋ฌธ์ œ๊ฐ€ ์—†๋‹ค๋ฉด ๊ฒŒ์ž„์„ ์ด์–ด์„œ ํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

๋จธ์“ฑ์ด๊ฐ€ ํ˜ผ์ž์„œ ๊ฒŒ์ž„์„ ์ง„ํ–‰ํ•˜๋‹ค ์˜๋ฌธ์ด ์ƒ๊ธด ํ‹ฑํƒํ†  ๊ฒŒ์ž„ํŒ์˜ ์ •๋ณด๋ฅผ ๋‹ด๊ณ  ์žˆ๋Š” ๋ฌธ์ž์—ด ๋ฐฐ์—ด board๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, ์ด ๊ฒŒ์ž„ํŒ์ด ๊ทœ์น™์„ ์ง€์ผœ์„œ ํ‹ฑํƒํ† ๋ฅผ ์ง„ํ–‰ํ–ˆ์„ ๋•Œ ๋‚˜์˜ฌ ์ˆ˜ ์žˆ๋Š” ๊ฒŒ์ž„ ์ƒํ™ฉ์ด๋ฉด 1์„ ์•„๋‹ˆ๋ผ๋ฉด 0์„ return ํ•˜๋Š” solution ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด ์ฃผ์„ธ์š”.

์ œํ•œ ์‚ฌํ•ญ

  • board์˜ ๊ธธ์ด = board[i]์˜ ๊ธธ์ด = 3
    • board์˜ ์›์†Œ๋Š” ๋ชจ๋‘ "O", "X", "."์œผ๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.
  • board[i][j]๋Š” i + 1ํ–‰ j + 1์—ด์— ํ•ด๋‹นํ•˜๋Š” ์นธ์˜ ์ƒํƒœ๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.
    • "."์€ ๋นˆ์นธ์„, "O"์™€ "X"๋Š” ํ•ด๋‹น ๋ฌธ์ž๋กœ ์นธ์ด ํ‘œ์‹œ๋˜์–ด ์žˆ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ

board result
["O.X", ".O.", "..X"] 1
["OOO", "...", "XXX"] 0
["...", ".X.", "..."] 0
["...", "...", "..."] 1

์ž…์ถœ๋ ฅ ์˜ˆ ์„ค๋ช…

์ž…์ถœ๋ ฅ ์˜ˆ #1

  • ์˜ˆ์ œ 1๋ฒˆ์˜ ๊ฒŒ์ž„ํŒ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.์„ ๊ณต ํ›„๊ณต์ด ๋ฒˆ๊ฐˆ์•„๊ฐ€๋ฉด์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋†“์•˜์„ ๋•Œ ์ด๋Ÿฌํ•œ ๊ฒŒ์ž„ํŒ์ด ๋‚˜์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    • 1ํ–‰ 1์—ด → 1ํ–‰ 3์—ด → 2ํ–‰ 2์—ด → 3ํ–‰ 3์—ด
    • 1ํ–‰ 1์—ด → 3ํ–‰ 3์—ด → 2ํ–‰ 2์—ด → 1ํ–‰ 3์—ด
    • 2ํ–‰ 2์—ด → 1ํ–‰ 3์—ด → 1ํ–‰ 1์—ด → 3ํ–‰ 3์—ด
    • 2ํ–‰ 2์—ด → 3ํ–‰ 3์—ด → 1ํ–‰ 1์—ด → 1ํ–‰ 3์—ด
    ๋ฌผ๋ก  ์œ„์™€ ๋‹ค๋ฅด๊ฒŒ ๋จธ์“ฑ์ด๊ฐ€ 2ํ–‰ 2์—ด์— O, 3ํ–‰ 3์—ด์— X, 1ํ–‰ 3์—ด์— X, 1ํ–‰ 1์—ด์— O ์ˆœ์„œ๋กœ ํ‘œ์‹œ๋ฅผ ํ•ด์„œ ์‹ค์ˆ˜๋ฅผ ํ–ˆ์„ ๊ฐ€๋Šฅ์„ฑ๋„ ์žˆ์ง€๋งŒ "์‹ค์ˆ˜๋ฅผ ํ–ˆ์„ ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ๋Š”๊ฐ€"๋ฅผ ๋ฌป๋Š” ๊ฒŒ ์•„๋‹Œ "์ด ๊ฒŒ์ž„ํŒ์ด ๊ทœ์น™์„ ์ง€์ผœ์„œ ์ง„ํ–‰ํ•œ ํ‹ฑํƒํ† ์—์„œ ๋‚˜์˜ฌ ์ˆ˜ ์žˆ๋Š” ์ƒํ™ฉ์ธ๊ฐ€"๋ฅผ ๋ฌป๋Š” ๋ฌธ์ œ๋ผ๋Š” ๊ฒƒ์— ์œ ์˜ํ•ด์ฃผ์„ธ์š”. ๋”ฐ๋ผ์„œ 1์„ return ํ•ฉ๋‹ˆ๋‹ค.
  • O.X
    .O.
    ..X
    

์ž…์ถœ๋ ฅ ์˜ˆ #2

  • ์˜ˆ์ œ 2๋ฒˆ์˜ ๊ฒŒ์ž„ํŒ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.๊ทœ์น™์„ ์ง€์ผœ์„œ ์ง„ํ–‰ํ•œ ํ‹ฑํƒํ† ๋ผ๋ฉด ์„ ๊ณต๊ณผ ํ›„๊ณต์ด ๋ฒˆ๊ฐˆ์•„๊ฐ€๋ฉด์„œ ๊ฐ๊ฐ 1ํ–‰, 3ํ–‰ ์ค‘ ๋‘ ์นธ์”ฉ์— ํ‘œ์‹œ๋ฅผ ํ•œ ๋’ค 5๋ฒˆ์งธ ์ฐจ๋ก€์— ์„ ๊ณต์ด 1ํ–‰์— ๊ฐ€๋กœ๋กœ 3๊ฐœ์˜ O๋ฅผ ์™„์„ฑํ–ˆ์„ ๋•Œ ์ข…๋ฃŒ๋˜๋ฏ€๋กœ ์ ์–ด๋„ ๋จธ์“ฑ์ด๊ฐ€ ๊ฒŒ์ž„์ด ์ข…๋ฃŒ๋œ ํ›„์—๋„ ๊ณ„์† ์ง„ํ–‰ํ•˜๋Š” ์‹ค์ˆ˜๋ฅผ ํ–ˆ๋‹ค๋Š” ๊ฒƒ์„ ์ถ”๋ก ํ•ด ๋ณผ ์ˆ˜ ์žˆ๊ณ , ์ •์ƒ์ ์ธ ํ‹ฑํƒํ† ์—์„œ๋Š” ์ด๋Ÿฌํ•œ ์ƒํ™ฉ์ด ๋‚˜์˜ฌ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ 0์„ return ํ•ฉ๋‹ˆ๋‹ค.
  • OOO
    ...
    XXX
    

์ž…์ถœ๋ ฅ ์˜ˆ #3

  • ์˜ˆ์ œ 3๋ฒˆ์€ 2ํ–‰ 2์—ด์—๋งŒ X๊ฐ€ ํ‘œ์‹œ๊ฐ€ ๋˜์–ด์žˆ์Šต๋‹ˆ๋‹ค. ์„ ๊ณต O ํ‘œ์‹œ๊ฐ€ ์—†์ด X๋งŒ ์žˆ์œผ๋ฏ€๋กœ ๋จธ์“ฑ์ด๊ฐ€ O๋ฅผ ํ‘œ์‹œํ•ด์•ผ ํ•  ๋•Œ X๋ฅผ ํ‘œ์‹œํ•˜๋Š” ์‹ค์ˆ˜๋ฅผ ํ–ˆ๋‹ค๋Š” ๊ฒƒ์„ ์ถ”๋ก ํ•ด ๋ณผ ์ˆ˜ ์žˆ๊ณ , ๊ทœ์น™์„ ์ง€์ผœ์„œ ์ง„ํ–‰ํ–ˆ์„ ๋•Œ๋Š” ์ด๋Ÿฌํ•œ ์ƒํ™ฉ์ด ๋‚˜์˜ฌ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ 0์„ return ํ•ฉ๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ #4

  • ์˜ˆ์ œ 4๋ฒˆ์€ ๋นˆ 3x3 ๊ฒŒ์ž„ํŒ์ž…๋‹ˆ๋‹ค. ์„ ๊ณต์ด ์•„์ง ๋นˆ์นธ์— ํ‘œ์‹œํ•˜๊ธฐ ์ „์— ์ด๋Ÿฌํ•œ ์ƒํ™ฉ์ด ๋‚˜์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ 1์„ return ํ•ฉ๋‹ˆ๋‹ค.

ํ’€์ด

  • ํ‹ฑํƒํ† ๋ฅผ ์•ˆ์ข‹์•„ํ•ด์„œ ๊ทœ์น™์„ ๋ชฐ๋ผ ํ•œ์ฐธ ๊ทธ๋ ค๊ฐ€๋ฉด์„œ ํ–ˆ๋‹ค. ๋…ธ๊ฐ€๋‹ค ๋ฌธ์ œ๋„ค...
    • ๋น„์ •์ƒ์ธ ์ผ€์ด์Šค 2๊ฐœ, ์ •์ƒ์ธ ์ผ€์ด์Šค 4๊ฐœ๋ฅผ ์ฐพ๊ณ  ๋‚˜๋จธ์ง€๋Š” ๋‹ค ๋น„์ •์ƒ์œผ๋กœ ํ•˜๋ฉด ๋˜๋Š” ๋ฌธ์ œ... ์‚ฝ์ง€๋ฅด...
def solution(board):
    answer = -1
    Cnt_O,Cnt_X,Win_O,Win_X = 0,0,0,0
    
    for b in board:
        Cnt_O += b.count("O")
        Cnt_X += b.count("X")

    if Cnt_O + Cnt_X == 0:
        return 1
    if Cnt_O > Cnt_X + 1:
        return 0
    if Cnt_O < Cnt_X:
        return 0

    for i in range(3):
        if board[0][i] == board[1][i] and board[1][i] == board[2][i]:
            if board[1][i] == "O":
                Win_O += 1
            elif board[1][i] == "X":
                Win_X += 1
        if board[i][0] == board[i][1] and board[i][1] == board[i][2]:
            if board[i][1] == "O":
                Win_O += 1
            elif board[i][1] == "X":
                Win_X += 1
    if board[0][0] == board[1][1] and board[1][1] == board[2][2]:
        if board[1][1] == "O":
            Win_O += 1
        elif board[1][1] == "X":
            Win_X += 1
    if board[2][0] == board[1][1] and board[1][1] == board[0][2]:
        if board[1][1] == "O":
            Win_O += 1
        elif board[1][1] == "X":
            Win_X += 1
    
    if Win_X == Win_O and Win_O == 0:
        return 1
    if Win_X == 0 and Win_O > 0:
        if Cnt_X < Cnt_O:
            return 1
    if Win_X > 0 and Win_O == 0:
        if Cnt_X == Cnt_O:
            return 1
        
    return 0
    
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.4MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.00ms, 10.4MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.4MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (0.00ms, 10.4MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.3MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.3MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (0.00ms, 10.4MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (0.00ms, 10.4MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.4MB)
ํ…Œ์ŠคํŠธ 12 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.3MB)
ํ…Œ์ŠคํŠธ 13 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.3MB)
ํ…Œ์ŠคํŠธ 14 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 15 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 16 ใ€‰	ํ†ต๊ณผ (0.00ms, 10.3MB)
ํ…Œ์ŠคํŠธ 17 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 18 ใ€‰	ํ†ต๊ณผ (0.00ms, 10.4MB)
ํ…Œ์ŠคํŠธ 19 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.3MB)
ํ…Œ์ŠคํŠธ 20 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 21 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.3MB)
ํ…Œ์ŠคํŠธ 22 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 23 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 24 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.4MB)
ํ…Œ์ŠคํŠธ 25 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.4MB)
ํ…Œ์ŠคํŠธ 26 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.3MB)
ํ…Œ์ŠคํŠธ 27 ใ€‰	ํ†ต๊ณผ (0.00ms, 10.3MB)
ํ…Œ์ŠคํŠธ 28 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 29 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 30 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.3MB)
ํ…Œ์ŠคํŠธ 31 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.3MB)
ํ…Œ์ŠคํŠธ 32 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.1MB)
ํ…Œ์ŠคํŠธ 33 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.3MB)
ํ…Œ์ŠคํŠธ 34 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 35 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.4MB)
ํ…Œ์ŠคํŠธ 36 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 37 ใ€‰	ํ†ต๊ณผ (0.00ms, 10.4MB)
ํ…Œ์ŠคํŠธ 38 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 39 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.4MB)
ํ…Œ์ŠคํŠธ 40 ใ€‰	ํ†ต๊ณผ (0.00ms, 10.3MB)
ํ…Œ์ŠคํŠธ 41 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.3MB)
ํ…Œ์ŠคํŠธ 42 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.3MB)
ํ…Œ์ŠคํŠธ 43 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.3MB)
ํ…Œ์ŠคํŠธ 44 ใ€‰	ํ†ต๊ณผ (0.00ms, 10.3MB)
ํ…Œ์ŠคํŠธ 45 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 46 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 47 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 48 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.4MB)
ํ…Œ์ŠคํŠธ 49 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.4MB)
ํ…Œ์ŠคํŠธ 50 ใ€‰	ํ†ต๊ณผ (0.00ms, 10.2MB)
ํ…Œ์ŠคํŠธ 51 ใ€‰	ํ†ต๊ณผ (0.00ms, 10.3MB)
  • ๊ณ ์ˆ˜์˜ ํ’€์ด....
    • ๊ฐ€๋กœ ์„ธ๋กœ ์ฒดํฌํ•˜๋Š” ๋ถ€๋ถ„ ๋„ˆ๋ฌด ๊น”๋”ํ•˜๊ณ  ์ข‹๋‹ค.
# Check if there is a winning row, column, or diagonal
def check_win(player, board):
    # Check rows
    for i in range(3):
        if all(cell == player for cell in board[i]):
            return True

    # Check columns
    for j in range(3):
        if all(board[i][j] == player for i in range(3)):
            return True

    # Check diagonals
    if all(board[i][i] == player for i in range(3)):
        return True
    if all(board[i][2-i] == player for i in range(3)):
        return True

    return False

def solution(board):
    num_x = sum(row.count('X') for row in board)
    num_o = sum(row.count('O') for row in board)

    if num_x - num_o > 0 or abs(num_x - num_o) > 1:
        return 0

    elif (check_win('O', board) and num_x != num_o - 1) or (check_win('X', board) and num_x != num_o):
        return 0

    return 1

์•„... ์–‘๊ถ๋ฌธ์ œ ๋ชป ํ’€์–ด์„œ ๋Œ€์‹  ์‰ฌ์šด ๋ฌธ์ œ ํ’€๊ณ  ์ž๋Š”๋ฐ... ์–‘๊ถ ๋ฌธ์ œ ๋„ˆ๋ฌด ์ฐœ์ฐœํ•˜๋‹ค.

728x90
๋ฐ˜์‘ํ˜•