본문 바로가기
알고리즘/백준

14503.로봇 청소기 - python

by 1.5볼트 2023. 4. 27.
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도 회전하는 기능 함수를 만들어서 현재 시작점부터 시작해서 끝나는 조건이 나올때 까지 단계적으로 함수를 실행한다 

 

r,l=map(int,input().split())
x,y,d=map(int,input().split())
t=[]
for i in range(r):
    t.append(list(map(int,input().split())))
def back(x,y,d):
    if d==0:x+=1
    elif d==1:y-=1
    elif d==2:x-=1
    else:y+=1
    if 0<=x <r and 0<=y<l and t[x][y]!=1:
        return x,y
    return False

def turn(d):
    return d-1 if d!=0 else 3

def go(x,y,d):
    if d==0:x-=1
    elif d==1:y+=1
    elif d==2:x+=1
    else:y-=1
    if 0<=x <r and 0<=y<l and t[x][y]==0:
        return x,y
    return False

w=0
while True:
    an=0
    if t[x][y]!=2:w+=1
    t[x][y]=2
    
    for i,j in [[-1,0],[1,0],[0,-1],[0,1]]:
        if 0<=x+i<r and  0<=y+j<l and t[x+i][y+j]==0:
            an=1
    if an:
        while True:
            d=turn(d)
            if go(x,y,d):
                
                x,y=go(x,y,d)
                break
    else:
        if back(x,y,d):
            
            x,y=back(x,y,d)
        else:
            break
print(w)

'알고리즘 > 백준' 카테고리의 다른 글

1874.스택 수열 python  (0) 2023.05.05
1564.팩토리얼5 python  (0) 2023.05.04
1016.제곱 ㄴㄴ 수 - python  (0) 2023.04.25
1644.소수의 연속합 - python  (0) 2023.04.24
1806.부분합  (0) 2023.04.23

댓글