[백준] 14503번 : 로봇청소기 (Python3)
2024. 3. 19. 15:14ㆍ알고리즘
from collections import deque
N, M = map(int, input().split())
r, c, d = map(int, input().split())
rooms = [list(map(int, input().split())) for _ in range(N)]
visited = [[False] * M for _ in range(N)]
dx = [-1, 0, 1, 0]
dy = [0, 1, 0, -1]
cnt = 0
def bfs(x, y, d):
global cnt
q = deque()
q.append((x, y))
visited[x][y] = True
cnt += 1
while q:
x, y = q.popleft()
turn = 0
for i in range(4):
d = (d + 3) % 4
nx, ny = x + dx[d], y + dy[d]
if 0 <= nx < N and 0 <= ny < M and not rooms[nx][ny]:
if not visited[nx][ny]:
visited[nx][ny] = True
q.append((nx, ny))
turn = 1
cnt += 1
break
if turn == 0:
bx, by = x - dx[d], y - dy[d]
if rooms[bx][by] == 1:
print(cnt)
break
else:
q.append((bx, by))
bfs(r, c, d)
'알고리즘' 카테고리의 다른 글
[백준] 15685번: 드래곤 커브 (Python3) (0) | 2024.04.26 |
---|---|
[Softeer] [한양대 HCPC 2023] X marks the Spot (Python) (0) | 2024.03.22 |
[LeetCode] Keys and Rooms (Python) (0) | 2024.03.09 |
[LeetCods] 01Matrix (Python) (0) | 2024.03.09 |
[LeetCode] Flood Fill (Python) (0) | 2024.03.09 |