728x90
14503번: 로봇 청소기
로봇 청소기가 주어졌을 때, 청소하는 영역의 개수를 구하는 프로그램을 작성하시오. 로봇 청소기가 있는 장소는 N×M 크기의 직사각형으로 나타낼 수 있으며, 1×1크기의 정사각형 칸으로 나누어
www.acmicpc.net
문제 설명
- 벽1과 빈 공간0으로 이뤄진 2차원 array를 입력으로 받고 주어진 규칙에 따라 빈 영역을 탐색하는 문제
고민한 부분
- 구현 할 때 청소를 마친 부분을 하나의 숫자로 채울 경우 디버깅 과정에서 청소 순서를 파악하기 어려웠음
- line 24에서
clean_tag
를 2로 선언하고 청소를 할 때마다 1씩 증가시켜 청소되는 순서를 파악할 수 있게 구현
- line 24에서
- 청소기가 회전하는 방향을 잘못 파악한 것을 늦게 알아채 오래 걸렸던 문제
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
def rollr,c,d: | |
global table, DIR | |
def check_4_dirr,c,d: | |
nonlocal sol, clean_tag | |
for i in range1,5: ## 방향 돌리는 순서가 잘못됨 | |
nd = d+i%4 | |
nr, nc= r + DIR[nd][0], c + DIR[nd][1] | |
# 청소 안했으면 이동하고 청소 | |
if table[nr][nc] == 0: | |
clean_tag += 1 | |
r,c,d =nr, nc, nd | |
table[r][c] = clean_tag | |
sol += 1 | |
return r, c, d | |
# 벽도 없고, 4방향 청소 다 했으면 뒤로 한칸, 방향 유지 | |
r, c = r - DIR[d][0], c - DIR[d][1] | |
return r,c,d | |
clean_tag = 2 | |
sol = 1 #시작 위치 청소로 시작 | |
table[r][c] = clean_tag # 청소 표시는 2로 | |
while 1: | |
# 4방향 탐색 시작 | |
r,c,d = check_4_dirr,c,d | |
# 만약 새로운 위치가 boundary 밖이나 벽이면 종료 | |
if table[r][c] == 1: | |
return sol | |
if __name__ == "__main__": | |
R, C = listmap(int,sys.stdin.readline(.split)) | |
r, c, d = listmap(int,sys.stdin.readline(.split)) | |
table = [listmap(int,sys.stdin.readline(.split)) for _ in rangeR] | |
if d == 1: | |
d = 3 | |
elif d == 3: | |
d = 1 | |
#r,c / 북 동 남 서 | |
DIR = [−1,0, | |
0,−1, | |
1,0, | |
0,1] | |
sol = rollr,c,d | |
printsol | |
'알고리즘 공부 > 백준 문제풀이' 카테고리의 다른 글
[백준 python] 14501번 퇴사 0 | 2022.08.05 |
---|---|
[백준 python] 14889번 스타트와 링크 0 | 2022.08.05 |
[백준 python] 14891번 톱니바퀴 0 | 2022.08.05 |
[백준 python] 14502번 연구소 0 | 2022.08.02 |
[백준 python] 14888번 연산자 끼워넣기 0 | 2022.07.31 |