728x90
반응형
SMALL
방향 때문에 머리가 아팠지만 특별한 부분은 없이 시키는데로 구현만 잘하면 됐던 문제였습니다. :)
import sys
answer = 0
n, m = map(int, sys.stdin.readline().strip().split())
y, x, d = map(int, sys.stdin.readline().strip().split())
dy, dx = [-1, 0, 1, 0], [0, 1, 0, -1]
graph = [list(map(int, sys.stdin.readline().strip().split())) for _ in range(n)]
visited = [[False for _ in range(m)] for _ in range(n)]
check_count = 0
visited[y][x] = True
def validation(ny, nx):
return 0 <= ny < n and 0 <= nx < m
def check_left_and_move():
global y, x, d, graph, visited, dy, dx, check_count
if check_count == 4:
return True
ny = y + dy[(d + 3) % 4]
nx = x + dx[(d + 3) % 4]
if graph[ny][nx] == 1 or visited[ny][nx] == True:
check_count += 1
d = (d + 3) % 4
return False
if graph[ny][nx] != 1 and visited[ny][nx] == False:
d = (d + 3) % 4
visited[ny][nx] = True
y, x = ny, nx
check_count = 0
return True
def move_back():
global y, x, d, graph, visited, dy, dx, check_count
ny = y + dy[(d + 2) % 4]
nx = x + dx[(d + 2) % 4]
if graph[ny][nx] == 1:
return False
else:
visited[ny][nx] = True
y, x = ny, nx
return True
while True:
if not check_left_and_move():
continue
if check_count != 4:
continue
else:
if move_back():
check_count = 0
continue
break
for y in range(len(visited)):
for x in range(len(visited[y])):
if visited[y][x]:
answer += 1
print(answer)
728x90
반응형
LIST
'코테' 카테고리의 다른 글
PCCP 05.21 후기 (0) | 2023.05.21 |
---|---|
CosPro, PCCP 준비 (0) | 2023.05.05 |
방화벽 설치하기 (0) | 2023.03.28 |
외주 수익 최대화하기 (0) | 2023.03.28 |
14500 - 테트로미노 (1) | 2023.03.28 |