문제 확인하기
2. 정답 코드 (Python, memory: 31120KB, time: 56ms)
m, n = map(int,input().split())
matrix = [[0 for _ in range(n)] for _ in range(m)]
dir = {0:[0,1],1:[1,0],2:[0,-1],3:[-1,0]}
remain = m*n-2
turn = 0
index = 0
x,y = 0,0
matrix[x][y] = "X"
while remain>=0:
flag = False
if 0<=x+dir[index][0]<m and 0<=y+dir[index][1]<n:
if matrix[x+dir[index][0]][y+dir[index][1]] != "X":
matrix[x+dir[index][0]][y+dir[index][1]] = "X"
x,y = x+dir[index][0],y+dir[index][1]
flag = True
if not flag:
turn += 1
index = (index+1)%4
x,y = x+dir[index][0],y+dir[index][1]
matrix[x][y] = "X"
remain -= 1
print(turn)