[코딩테스트][백준] 🔥 백준 14503번 "로봇 청소기" 문제: Python으로 완벽 해결하기! 🔥

김상욱·2024년 8월 14일
post-thumbnail

문제 링크

https://www.acmicpc.net/problem/14503

🕒 Python 풀이시간: 20분

n,m=map(int,input().split())
r,c,d=map(int,input().split())

dx=[-1,0,1,0]
dy=[0,1,0,-1]

board=[list(map(int,input().split())) for _ in range(n)]
ans=0
visited=[[False]*m for _ in range(n)]

while True:
    go=False
    if not visited[r][c]:
        visited[r][c]=True
        ans+=1
    turn=0
    for i in range(4):
        d=(d-1)%4
        nx=r+dx[d]
        ny=c+dy[d]
        turn+=1
        if nx<0 or ny<0 or nx>=n or ny>=m:
            continue
        if board[nx][ny]==1:
            continue
        if visited[nx][ny]:
            continue
        r,c=nx,ny
        go=True
        break
        
    if turn==4 and not go:
        nx=r+dx[d]*(-1)
        ny=c+dy[d]*(-1)
        if nx<0 or ny<0 or nx>=n or ny>=m:
            break
        if board[nx][ny]==1:
            break
        r,c=nx,ny

print(ans)

로봇청소기의 움직임 시뮬레이션 구현 🚀🤖

로봇청소기의 움직임을 문제에서 주어진대로 구현하면 되는 시뮬레이션 문제이다.

뒤로 움직이는 조건만 확실히 하면 문제 없이 구현할 수 있다. 4방향을 전부 찾고 그 찾았을 경우 break 시키며 못찾았을 경우에는 벽이 있으면 종료하고 벽이 없으면 현재 방향에서 -1을 곱해서 진행하면 된다.

이렇게 Python으로 백준의 "로봇 청소기" 문제를 해결해보았습니다. 코드와 개념 설명을 참고하여 문제를 해결하는 데 도움이 되셨길 바랍니다! 😊

0개의 댓글