🚩 코딩테스트/알고리즘
[백준] 14503번: 로봇 청소기
딩딩크롱
2023. 3. 3. 20:26
728x90
문제
https://www.acmicpc.net/problem/14503
14503번: 로봇 청소기
첫째 줄에 방의 크기 $N$과 $M$이 입력된다. $(3 \le N, M \le 50)$ 둘째 줄에 처음에 로봇 청소기가 있는 칸의 좌표 $(r, c)$와 처음에 로봇 청소기가 바라보는 방향 $d$가 입력된다. $d$가 $0$인 경우 북쪽
www.acmicpc.net
풀이
문제에서 주어진 대로 구현하였습니다.
※ 현재 방향이 아닌 반시계 방향으로 90º 회전한 위치부터 확인해야 합니다.
코드
파이썬
import sys
input = sys.stdin.readline
def solution(x, y, d):
answer = 0
dx = [-1, 0, 1, 0]
dy = [0, 1, 0, -1]
visited = [[False] * M for _ in range(N)]
while True:
if not visited[x][y]:
visited[x][y] = True
answer += 1
flag = False
for i in range(d + 3, d - 1, -1):
nd = i % 4
nx = x + dx[nd]
ny = y + dy[nd]
if board[nx][ny] == 0 and not visited[nx][ny]:
flag = True
x = nx
y = ny
d = nd
break
if not flag:
x -= dx[d]
y -= dy[d]
if board[x][y] == 1:
return answer
N, M = map(int, input().split())
r, c, d = map(int, input().split())
board = []
for _ in range(N):
board.append(list(map(int, input().split())))
print(solution(r, c, d))
728x90