알고리즘 공부/백준

(Python)백준 코딩테스트 연습 - 오목(2072)

HRuler 2023. 6. 2. 21:00

1. 문제

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

 

2072번: 오목

19x19크기의 바둑판에, 돌을 놓을 좌표가 주어지면 이 게임이 몇 수만에 끝나는 지를 알아보려고 한다. 사용하고자 하는 바둑판의 모양은 위의 그림과 같으며, (1, 1)이 가장 왼쪽 위의 좌표이고 (19

www.acmicpc.net

2. 풀이

import sys
input = sys.stdin.readline

# n : 놓이는 돌의 개수
n = int(input())

def winCheck(nList, x, y, stoneColor):
    
    stoneCnt = 1
    xPoint = x
    yPoint = y
    while True:
        xPoint -= 1
        yPoint -= 1
        if xPoint <= 0 or yPoint <= 0 or nList[xPoint-1][yPoint-1] != stoneColor:
            break
        else:
            stoneCnt += 1
    xPoint = x
    yPoint = y
    while True:
        xPoint += 1
        yPoint += 1
        if xPoint > 19 or yPoint > 19 or nList[xPoint-1][yPoint-1] != stoneColor:
            break
        else:
            stoneCnt += 1
    if stoneCnt == 5:
        return True
    
    stoneCnt = 1
    xPoint = x
    yPoint = y
    while True:
        xPoint -= 1
        if xPoint <= 0 or nList[xPoint-1][yPoint-1] != stoneColor:
            break
        else:
            stoneCnt += 1
    xPoint = x
    yPoint = y
    while True:
        xPoint += 1
        if xPoint > 19 or nList[xPoint-1][yPoint-1] != stoneColor:
            break
        else:
            stoneCnt += 1
    if stoneCnt == 5:
        return True
    
    stoneCnt = 1
    xPoint = x
    yPoint = y
    while True:
        xPoint -= 1
        yPoint += 1
        if xPoint <= 0 or yPoint > 19 or nList[xPoint-1][yPoint-1] != stoneColor:
            break
        else:
            stoneCnt += 1
    xPoint = x
    yPoint = y
    while True:
        xPoint += 1
        yPoint -= 1
        if xPoint > 19 or yPoint <= 0 or nList[xPoint-1][yPoint-1] != stoneColor:
            break
        else:
            stoneCnt += 1
    if stoneCnt == 5:
        return True
    
    stoneCnt = 1
    xPoint = x
    yPoint = y
    while True:
        yPoint -= 1
        if yPoint <= 0 or nList[xPoint-1][yPoint-1] != stoneColor:
            break
        else:
            stoneCnt += 1
    xPoint = x
    yPoint = y
    while True:
        yPoint += 1
        if yPoint > 19 or nList[xPoint-1][yPoint-1] != stoneColor:
            break
        else:
            stoneCnt += 1
    if stoneCnt == 5:
        return True
    else:
        return False

# nList: 오목판 리스트
nList = [[0 for j in range(19)] for i in range(19)]
stonePoint = []
for i in range(n):
    stonePoint.append(list(map(int, input().split())))
    
winDisable = True
    
for i, v in enumerate(stonePoint):
    if i % 2 == 0:
        stoneColor = -1
    else:
        stoneColor = 1
    
    nList[v[0]-1][v[1]-1] = stoneColor
    
    if winCheck(nList, v[0], v[1], stoneColor):
        print(i+1)
        winDisable = False
        break
        
if winDisable:
    print(-1)