Skip to content

Commit 9b338bb

Browse files
committed
[BOJ] #2072. 오목 / 실버2 / 180분 / 실패
1 parent ccd038e commit 9b338bb

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import sys
2+
input = sys.stdin.readline
3+
4+
5+
def check_winner(x, y, color):
6+
# 오목 4개 방향
7+
directions = [(-1, 0), (0, -1), (-1, -1), (-1, 1)]
8+
# (-1, 0) - 세로 방향 (위쪽)
9+
# (0, -1) - 가로 방향 (왼쪽)
10+
# (-1, -1) - 좌상향 대각선 방향
11+
# (-1, 1) - 우상향 대각선 방향
12+
13+
for dx, dy in directions:
14+
count = 1 # 현재 돌이 포함된 연속된 돌의 개수를 셀 변수 초기화
15+
16+
# 주어진 방향으로 연속된 돌 개수 세기
17+
nx, ny = x + dx, y + dy
18+
while 1 <= nx < 20 and 1 <= ny < 20 and board[nx][ny] == color:
19+
count += 1
20+
# 방향 dx, dy로 이동하며 해당 방향에 같은 색의 돌이 있는 한 계속 탐색
21+
nx += dx
22+
ny += dy
23+
24+
# 반대 방향으로도 연속된 돌 개수 세기
25+
nx, ny = x - dx, y - dy
26+
while 1 <= nx < 20 and 1 <= ny < 20 and board[nx][ny] == color:
27+
count += 1
28+
# 반대 방향으로도 이동하여 연속된 돌의 개수를 추가로 셈
29+
nx -= dx
30+
ny -= dy
31+
32+
# 5개일 경우만 승리로 인정
33+
if count == 5:
34+
return True
35+
36+
return False # 5개의 돌이 연속되지 않을 경우
37+
38+
N = int(input().strip()) # 돌의 개수
39+
board = [[0] * 20 for _ in range(20)] # 19x19 바둑판 / 바둑돌이 놓인 위치 기록 (1,1)~(19,19)
40+
41+
# 입력된 좌표에 돌을 기록
42+
for i in range(N):
43+
x, y = map(int, input().strip().split()) # 각 수의 좌표
44+
color = 1 if i % 2 == 0 else 2 # 홀수 번째 수: 흑돌(1), 짝수 번째 수: 백돌(2)
45+
board[x][y] = color # 해당 좌표에 돌을 놓음 (바둑판 배열에 기록)
46+
47+
if check_winner(x, y, color): # 현재 놓은 돌로 인해 승리가 결정되는지 확인
48+
print(i + 1) # 승리가 결정되면 현재 몇 번째 수인지 출력하고 프로그램 종료
49+
sys.exit()
50+
51+
print(-1) # 모든 돌을 놓았지만 승패가 결정되지 않으면 -1 출력

0 commit comments

Comments
 (0)