시작이 반

[백준] 2615번번 (python 파이썬) 본문

알고리즘/백준

[백준] 2615번번 (python 파이썬)

G_Gi 2021. 3. 31. 23:02
SMALL

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

 

구현 문제이다.

 

오목을 승리한 돌과 오목을 형성한 돌들의 맨 왼쪽 돌을 출력하는 문제이다.

 

우선 첫행 ~ 마지막행 순으로 검사를 한다. 열은 왼쪽에서 오른쪽으로 검사한다.

 

가로 검사

1. 해당 좌표에서 오른쪽으로 4칸을 확인하며 같은 돌인지 확인

2. 오목을 형성했다면 다음 한칸에도 같은 돌이 아닌지 확인

3. 해당 좌표에서 이전 좌표(왼쪽 1칸)가 같은돌이 아닌지 확인

 

세로, 오른쪽 대각선 아래, 오른쪽 대각선 위도 똑같이 확인

 

 

풀긴 했는데 코드가 너무 더럽다... 다시 풀어보자

graph = [list(map(int, input().split(' '))) for _ in range(19)]


def whoWin(x, y):
    color = graph[x][y]

    rowCnt = 0
    nx, ny = None, None
    for i in range(1, 5):
        nx, ny = x, y + i

        if nx < 0 or nx >= 19 or ny < 0 or ny >= 19:
            break

        if color == graph[nx][ny]:
            rowCnt += 1
        else:
            break
    # 6목 검사
    ny = ny + 1
    px, py = x, y - 1
    if rowCnt == 4:
        if 0 <= nx <= 18 and 0 <= ny <= 18:
            if graph[nx][ny] != color:
                if 0 <= px <= 18 and 0 <= py <= 18:
                    if graph[px][py] == color:
                        return False
                return True
        else:
            if 0 <= px <= 18 and 0 <= py <= 18:
                if graph[px][py] == color:
                    return False
            return True

    colCnt = 0
    nx, ny = None, None
    for i in range(1, 5):
        nx, ny = x + i, y
        if nx < 0 or nx >= 19 or ny < 0 or ny >= 19:
            break
        if color == graph[nx][ny]:
            colCnt += 1
        else:
            break
    # 6목 검사
    nx = nx + 1
    px, py = x - 1, y
    if colCnt == 4:
        if 0 <= nx <= 18 and 0 <= ny <= 18:
            if graph[nx][ny] != color:
                if 0 <= px <= 18 and 0 <= py <= 18:
                    if graph[px][py] == color:
                        return False
                return True
        else:
            if 0 <= px <= 18 and 0 <= py <= 18:
                if graph[px][py] == color:
                    return False
            return True

    rightDown = 0
    nx, ny = None, None
    for i in range(1, 5):
        nx, ny = x + i, y + i
        if nx < 0 or nx >= 19 or ny < 0 or ny >= 19:
            break
        if color == graph[nx][ny]:
            rightDown += 1
        else:
            break
    # 6목 검사
    nx, ny = nx + 1, ny + 1
    px, py = x - 1, y - 1
    if rightDown == 4:
        if 0 <= nx <= 18 and 0 <= ny <= 18:
            if graph[nx][ny] != color:
                if 0 <= px <= 18 and 0 <= py <= 18:
                    if graph[px][py] == color:
                        return False
                return True
        else:
            if 0 <= px <= 18 and 0 <= py <= 18:
                if graph[px][py] == color:
                    return False
            return True

    rightUp = 0
    nx, ny = None, None
    for i in range(1, 5):
        nx, ny = x - i, y + i
        if nx < 0 or nx >= 19 or ny < 0 or ny >= 19:
            break
        if color == graph[nx][ny]:
            rightUp += 1
        else:
            break
    # 6목 검사
    nx, ny = nx - 1, ny + 1
    px, py = x + 1, y - 1
    if rightUp == 4:
        if 0 <= nx <= 18 and 0 <= ny <= 18:
            if graph[nx][ny] != color:
                if 0 <= px <= 18 and 0 <= py <= 18:
                    if graph[px][py] == color:
                        return False
                return True
        else:
            if 0 <= px <= 18 and 0 <= py <= 18:
                if graph[px][py] == color:
                    return False
            return True


for i in range(19):
    for j in range(19):
        if graph[i][j] != 0:
            if whoWin(i, j):
                print(graph[i][j])
                print(i + 1, j + 1)
                exit()

print(0)

 

 

코드 약간 정리

graph = [list(map(int, input().split(' '))) for _ in range(19)]


def whoWin(x, y):
    color = graph[x][y]

    rowCnt = 0
    nx, ny = None, None
    i = 1
    while True:
        nx, ny = x, y + i
        if nx < 0 or nx >= 19 or ny < 0 or ny >= 19:
            break

        if color == graph[nx][ny]:
            rowCnt += 1
        else:
            break
        i += 1

    # 6목 검사
    px, py = x, y - 1
    if rowCnt == 4:
        if 0 <= px <= 18 and 0 <= py <= 18:
            if graph[px][py] == color:
                return False
        return True

    colCnt = 0
    nx, ny = None, None
    i = 1
    while True:
        nx, ny = x + i, y
        if nx < 0 or nx >= 19 or ny < 0 or ny >= 19:
            break
        if color == graph[nx][ny]:
            colCnt += 1
        else:
            break
        i += 1

    # 6목 검사
    px, py = x - 1, y
    if colCnt == 4:
        if 0 <= px <= 18 and 0 <= py <= 18:
            if graph[px][py] == color:
                return False
        return True

    rightDown = 0
    nx, ny = None, None
    i = 1
    while True:
        nx, ny = x + i, y + i
        if nx < 0 or nx >= 19 or ny < 0 or ny >= 19:
            break
        if color == graph[nx][ny]:
            rightDown += 1
        else:
            break
        i += 1

    # 6목 검사
    px, py = x - 1, y - 1
    if rightDown == 4:
        if 0 <= px <= 18 and 0 <= py <= 18:
            if graph[px][py] == color:
                return False
        return True

    rightUp = 0
    nx, ny = None, None
    i = 1
    while True:
        nx, ny = x - i, y + i
        if nx < 0 or nx >= 19 or ny < 0 or ny >= 19:
            break
        if color == graph[nx][ny]:
            rightUp += 1
        else:
            break
        i += 1
    # 6목 검사
    px, py = x + 1, y - 1
    if rightUp == 4:
        if 0 <= px <= 18 and 0 <= py <= 18:
            if graph[px][py] == color:
                return False
        return True


for i in range(19):
    for j in range(19):
        if graph[i][j] != 0:
            if whoWin(i, j):
                print(graph[i][j])
                print(i + 1, j + 1)
                exit()

print(0)
LIST