데이터사이언스 기록기📚

[백준/Python] 14503번(구현, 시뮬레이션)_로봇 청소기 본문

Coding Test/백준(Python)

[백준/Python] 14503번(구현, 시뮬레이션)_로봇 청소기

syunze 2023. 5. 12. 16:19

📌문제 유형

구현,시뮬레이션 (골드5)

 

📌문제

 

14503번: 로봇 청소기

첫째 줄에 방의 크기 $N$과 $M$이 입력된다. $(3 \le N, M \le 50)$  둘째 줄에 처음에 로봇 청소기가 있는 칸의 좌표 $(r, c)$와 처음에 로봇 청소기가 바라보는 방향 $d$가 입력된다. $d$가 $0$인 경우 북쪽

www.acmicpc.net

 

📌나의 문제풀이

- 실패

  • 방향 회전 오른쪽으로 진행(문제는 왼쪽으로 회전)
  • 청소 되었는지, 아닌지 여부를 알 수 없었음
  • 청소된 곳 바로 2로 고치고, cnt += 1 하기!!
n, m = map(int,input().split())
x,y,d = map(int,input().split())
maps = []

for _ in range(n):
    maps.append(list(map(int,input().split())))

dx = [-1,0,1,0]
dy = [0,1,0,-1]
dd = [0,1,2,3]

cnt = 0
while True:
    if maps[x][y] == 2:
        continue
    elif maps[x][y] == 0:
        maps[x][y] = 2
        cnt += 1
    
    before_d = d
    # 주변 4칸 확인 빈 칸이 있는 경우
    for _ in range(4):
        # 방향부터 확인
        if dd.index(d) == 3:
            d = dd[0]
        else:
            d = dd[dd.index(d) + 1]

        # 위치 옮기기
        nx = x + dx[d]
        ny = y + dy[d]

    
        if maps[nx][ny] == 0:
            x,y = nx, ny
            break
        else:
            continue
    
    # 4칸 중 청소되지 않은 빈 칸이 없는 경우
    if before_d == 0:
        x += 1
        if maps[x][y] == 1:
            break
    elif before_d == 1:
        y -= 1
        if maps[x][y] == 1:
            break
    elif before_d == 2:
        x -= 1
        if maps[x][y] == 1:
            break
    elif before_d == 3:
        y += 1
        if maps[x][y] == 1:
            break

print(cnt)

- 성공(다른 사람 풀이 참고하여 수정)

n, m = map(int,input().split())
x,y,d = map(int,input().split())
maps = []

for _ in range(n):
    maps.append(list(map(int,input().split())))

dx = [-1,0,1,0]
dy = [0,1,0,-1]
dd = [0,1,2,3]

maps[x][y] = 2
cnt = 1
while True:
    flag = 0
    before_d = d

    # 주변 4칸 확인 빈 칸이 있는 경우
    for _ in range(4):
        # 방향부터 확인
        if d == 0:
            d = dd[3]
        else:
            d = dd[d - 1]

        # 위치 옮기기
        nx = x + dx[d]
        ny = y + dy[d]

        if 0 <= nx < n and 0 <= ny < m :
            if maps[nx][ny] == 0:
                x,y = nx, ny
                maps[x][y] = 2
                cnt += 1
                flag = 1
                break
        
    
    # 4칸 중 청소되지 않은 빈 칸이 없는 경우
    if flag == 0:
        if before_d == 0:
            x += 1
            if maps[x][y] == 1:
                break
        elif before_d == 1:
            y -= 1
            if maps[x][y] == 1:
                break
        elif before_d == 2:
            x -= 1
            if maps[x][y] == 1:
                break
        elif before_d == 3:
            y += 1
            if maps[x][y] == 1:
                break
 
print(cnt)

 

📌 다른사람의 문제풀이

## 북, 동, 하, 서 ( 시계방향 )
dr = [-1, 0, 1, 0]
dc = [0, 1, 0, -1]

n, m = map(int, input().split())
r, c, d = map(int, input().split())
arr = [list(map(int, input().split())) for _ in range(n)]
## 방문 쳌
visited = [[0]*m for _ in range(n)]

## 시작지 방문쳌 and 카운트!
visited[r][c] = 1
cnt = 1

while True:
    flag = 0            ## 아직 아무것도 청소 안했음!
    for _ in range(4):  ## 4방향을 돈다!
        d = (d+3) % 4   ## 왼쪽방향으로 한 칸 돌린다! 중요!!!!!1
        nr = r + dr[d]
        nc = c + dc[d]

        ## 범위 안에 들고, 빈 칸이고, 청소할 수 있다면!
        ## 들려서 청소하고, 카운트하고, 현재 위치를 갱신하고, flag 변경!
        if 0 <= nr < n and 0 <= nc < m and arr[nr][nc] == 0:
            if visited[nr][nc] == 0:
                visited[nr][nc] = 1
                cnt += 1
                r = nr
                c = nc
                flag = 1        ## 청소 했다는 뜻
                break

    if flag == 0:               ## 위의 for문에 들어가지 못했을 때
        ## 즉 네 방향 모두 청소를 할 수 없을 때
        ## 후진 했을 때 벽이면 break
        ## 만약 뒤가 벽이 아니라면! 그 위치를 다시 갱신!!!
        if arr[r-dr[d]][c-dc[d]] == 1:
            print(cnt)
            break
        else:
            r, c = r-dr[d], c-dc[d]

 

📌 리뷰 

- 사소한 조건 하나하나 잘 체크하기

- 청소 여부 확인, 방향 회전 체크하기 

 

728x90
Comments