링크 : https://www.acmicpc.net/problem/2615
2615번: 오목
오목은 바둑판에 검은 바둑알과 흰 바둑알을 교대로 놓아서 겨루는 게임이다. 바둑판에는 19개의 가로줄과 19개의 세로줄이 그려져 있는데 가로줄은 위에서부터 아래로 1번, 2번, ... ,19번의 번호
www.acmicpc.net
- 문제
- 소요시간:




- 설계하기(접근방법)
1. 오목 리스트 입력받기
2. 구현
전수검사를 해야될 것이다
오목을 만드는 방식은 3 가지가 있다
1) 가로로 5개
2) 대각선 오른쪽 아래로 5개
3) 세로로 5개
4) 대각선 오른쪽 위로 5개
하지만 육목 이상은 오목이 아니기 때문에 조건에서 제외해야 한다
또한 만약 리스트의 범위가 벗어나면
try:
except : IndexError를 통해
에러를 제어해준다
먼저 검사방법은 다음과 같다
1) 가로로 5개
mok[i][j] == mok[i][j + 1] and mok[i][j + 1] == mok[i][j + 2] and mok[i][j + 2] == mok[i][j + 3] and mok[i][j + 3] == mok[i][j + 4]
2) 대각선 오른쪽 아래로 5개
mok[i][j] == mok[i+1][j + 1] and mok[i+1][j + 1] == mok[i+2][j + 2] and mok[i+2][j + 2] == mok[i+3][j + 3] and mok[i+3][j + 3] == mok[i+4][j + 4]
3) 세로로 5개
mok[i][j] == mok[i + 1][j] and mok[i + 1][j] == mok[i + 2][j] and mok[i + 2][j] == mok[i + 3][j] and mok[i + 3][j] == mok[i + 4][j]
4) 대각선 오른쪽 위로 5개
mok[i][j] == mok[i - 1][j + 1] and mok[i - 1][j + 1] == mok[i - 2][j+2] and mok[i - 2][j+2] == mok[i - 3][j+3] and mok[i - 3][j+3] == mok[i - 4][j+4]
추가해야할 조건은 각각의 배열 앞뒤로는 다른 숫자가 와야한다는 것이다 (육목 방지 조건)
2)의 경우
flag = k일 때
mok[i][j] == mok[i-1][j-1]
mok[i+4][j+4] == mok[i+5][j+5]
인 경우 육목에 해당된다.
1),3),4)도 같은 방식으로 육목 방지 조건을 넣어준다
+
이 조건을 통과한 후에는 for문을 break해준다
그리고 행과 열의 번호를 저장해놓는다
3. 출력하기
1) flag= 1 or flag = 2일 때
행과 열을 같이 출력
2) flag = 0일 때(승부가 나지 않음)
0을 출력한다
- 코드(출력)
import sys
input = sys.stdin.readline
mok = []
flag = 0
breaker1 = False
breaker2 = False
for _ in range(19):
mok.append(list(map(int, input().split())))
for k in range(1, 3):
if breaker1 == True:
break
for i in range(19):
if breaker1 == True:
break
for j in range(19):
try:
if k == mok[i][j] and mok[i][j] == mok[i][j + 1] and mok[i][j + 1] == mok[i][j + 2] and mok[i][j + 2] == mok[i][j + 3] and mok[i][j + 3] == mok[i][j + 4]:
flag = k
try:
if flag == k and mok[i][j] == mok[i][j-1]:
flag = 0
except IndexError:
pass
try:
if flag == k and mok[i][j+4] == mok[i][j+5]:
flag = 0
except IndexError:
pass
except IndexError:
pass
if flag == k:
column = i + 1
row = j + 1
breaker1 = True
break
try:
if k == mok[i][j] and mok[i][j] == mok[i+1][j + 1] and mok[i+1][j + 1] == mok[i+2][j + 2] and mok[i+2][j + 2] == mok[i+3][j + 3] and mok[i+3][j + 3] == mok[i+4][j + 4]:
flag = k
try:
if flag == k and mok[i][j] == mok[i-1][j-1]:
flag = 0
except IndexError:
pass
try:
if flag == k and mok[i+4][j+4] == mok[i+5][j+5]:
flag = 0
except IndexError:
pass
except IndexError:
pass
if flag == k:
column = i + 1
row = j + 1
breaker1 = True
break
try:
if k == mok[i][j] and mok[i][j] == mok[i + 1][j] and mok[i + 1][j] == mok[i + 2][j] and mok[i + 2][j] == mok[i + 3][j] and mok[i + 3][j] == mok[i + 4][j]:
flag = k
try:
if flag == k and mok[i][j] == mok[i-1][j]:
flag = 0
except IndexError:
pass
try:
if flag == k and mok[i+4][j] == mok[i+5][j]:
flag = 0
except IndexError:
pass
except IndexError:
pass
if flag == k:
column = i + 1
row = j + 1
breaker1 = True
break
try:
if k == mok[i][j] and mok[i][j] == mok[i - 1][j + 1] and mok[i - 1][j + 1] == mok[i - 2][j+2] and mok[i - 2][j+2] == mok[i - 3][j+3] and mok[i - 3][j+3] == mok[i - 4][j+4]:
flag = k
try:
if flag == k and mok[i][j] == mok[i+1][j-1]:
flag = 0
except IndexError:
pass
try:
if flag == k and mok[i-4][j + 4] == mok[i-5][j+5]:
flag = 0
except IndexError:
pass
except IndexError:
pass
if flag == k:
column = i + 1
row = j + 1
breaker1 = True
break
if flag == 1 or flag == 2:
print(flag)
print(column, row)
else:
print(0)
- 얻어갈 부분
1. 공통의 큰 틀을 짠 후에 각각의 경우의 수를 넣었다
2. try except문을 통해서 index의 범위를 벗어나는 경우를 제어했다
3. 삼중 break문을 통해 break를 한번에 제어했
'알고리즘(백준) > 구현' 카테고리의 다른 글
| [알고리즘/구현] 18311번 : 왕복 - python (1) | 2023.12.20 |
|---|---|
| [알고리즘/구현] 16926번 : 배열 돌리기 - python (0) | 2023.05.09 |
| [알고리즘/구현] 15787번 : 기차 - python (0) | 2023.04.27 |
| [알고리즘/구현] 17276번 : 배열 돌리기 - python (0) | 2023.04.20 |
| [알고리즘/구현] 22858번 : 원상 복구(small) - python (0) | 2023.04.19 |