2615_오목 (Python)

서정준·2022년 6월 6일
0

BAEKJOON

목록 보기
5/5
post-thumbnail

1. 아이디어

  • 먼저, 아래 코드로 구현한 이 문제의 시간 복잡도는 다음과 같다.
NMKNMK \\
 NM=배열의  크기,  K=방향(N=19,M=19,K=4)\ NM = 배열의\;크기, \;K = 방향 \quad (N = 19, M = 19, K = 4) \\
  • 가장 왼쪽에 있는 바둑알의 가로줄 번호와, 세로줄 번호를 출력하라라고 했기 때문에 4개의 방향에(→, ↓, ↗, ↘)대한 visit함수를 만들어 주었다.

2. 코드

import sys
input = sys.stdin.readline
# → ↓ ↗ ↘
dx = [0, 1, -1, 1]
dy = [1, 0, 1, 1]


def check(x, y, s, idx):
    visit[x][y][i] = True
    cnt = 1

    while True:
        x, y = x+dx[idx], y+dy[idx]
        if 0 <= x < 19 and 0 <= y < 19:
            if a[x][y] == s and not visit[x][y][idx]:
                visit[x][y][idx] = True
                cnt += 1
            else:
                break
        else:
            break
    if cnt == 5:
        return True


a = [list(map(int, input().split())) for _ in range(19)]
visit = [[[False]*4 for _ in range(19)] for _ in range(19)]

for y in range(19):
    for x in range(19):
        for i in range(4):
            if a[x][y] != 0 and not visit[x][y][i]:
                if check(x, y, a[x][y], i):
                    print(a[x][y])
                    print(x+1, y+1)
                    sys.exit()

print(0)
문제 출처

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

profile
통통통통

0개의 댓글