본문 바로가기

Algorithm/Python

[Algorithm|Python] 백준 2615번 / 99클럽 14일차 TIL

import sys

board = [list(map(int, sys.stdin.readline().split())) for _ in range(19)]
move= [[1,0],[1,1],[0,1],[-1,1]]
N = 19
result = 0

for i in range(N):
    for j in range(N):
        if board[i][j] != 0:
            stone = board[i][j]
            
            for dy, dx in move: 
                ny, nx, cnt = i + dy, j + dx, 1
                
                while 0 <= ny < N and 0 <= nx < N and board[ny][nx] == stone:
                    cnt += 1
                    
                    if cnt == 5:
                        if 0 <= i - dy < N and 0 <= j - dx < N and board[i-dy][j-dx] == stone:
                            break
                        if 0 <= ny + dy < N and 0 <= nx + dx < N and board[ny+dy][nx+dx] == stone:
                            break
                        if stone == 1:
                            result = 1
                        if stone == 2:
                            result = 2
                        y, x = i, j
                        
                    ny += dy
                    nx += dx
                    
if result > 0:
    print(result)
    print(y+1,x+1)
else:
    print(0)