T-coding
백준 14503 로봇 청소기 / Python 본문
728x90
728x90
https://www.acmicpc.net/problem/14503
14503번: 로봇 청소기
첫째 줄에 방의 크기 $N$과 $M$이 입력된다. $(3 \le N, M \le 50)$ 둘째 줄에 처음에 로봇 청소기가 있는 칸의 좌표 $(r, c)$와 처음에 로봇 청소기가 바라보는 방향 $d$가 입력된다. $d$가 $0$인 경우 북쪽
www.acmicpc.net

코드
더보기
n, m = map(int, input().split())
r, c, d = map(int, input().split())
arr = []
visited = [[False] * m for _ in range(n)]
dy = [-1, 0, 1, 0]
dx = [0, 1, 0, -1]
for _ in range(n):
arr.append(list(map(int, input().split())))
visited[r][c] = True
ans = 1
while 1:
flag = False
for _ in range(4):
ny = r + dy[(d + 3) % 4]
nx = c + dx[(d + 3) % 4]
d = (d + 3) % 4
if 0 <= ny < n and 0 <= nx < m and arr[ny][nx] == 0 and not visited[ny][nx]:
visited[ny][nx] = True
ans += 1
r = ny
c = nx
flag = True
break
if not flag:
if arr[r - dy[d]][c - dx[d]] == 1:
print(ans)
break
else:
r = r - dy[d]
c = c - dx[d]
728x90
300x250
'Baekjoon' 카테고리의 다른 글
백준 14499 주사위 굴리기 / Python (0) | 2023.03.21 |
---|---|
백준 3190 뱀 / Python (0) | 2023.03.21 |
백준 5567 결혼식 / Python (0) | 2023.03.21 |
백준 5397 키로거 / Python (0) | 2023.03.20 |
백준 4198 열차정렬 / Python (0) | 2023.03.20 |