링크 : https://www.acmicpc.net/problem/14503
14503번: 로봇 청소기
첫째 줄에 방의 크기 $N$과 $M$이 입력된다. $(3 \le N, M \le 50)$ 둘째 줄에 처음에 로봇 청소기가 있는 칸의 좌표 $(r, c)$와 처음에 로봇 청소기가 바라보는 방향 $d$가 입력된다. $d$가 $0$인 경우 북쪽
www.acmicpc.net
- 문제
- 소요시간: 47분 구현, 디버깅 12분(성공?)




- 설계하기(접근방법)
1. 입력받기
크게 다른 점은 없다
2. 구현하기
구현할 내용이 상당히 많다
문제의 요구사항에 맞게 구현만 하면 된다
이중 배열과 While 문, break, continue를 통해 흐름을 제어해준다
3. 출력하기
청소한 구역의 횟수를 출력한다
- 코드(출력)
n , m = map(int, input().split())
x, y, direction = map(int, input().split())
room_condition = []
clean_count = 0
for i in range(n):
room_condition.append(list(map(int,input().split())))
while True:
if room_condition[x][y] == 0:
room_condition[x][y] = -1
clean_count += 1
if room_condition[x - 1][y] != 0 and room_condition[x][y - 1] != 0 and room_condition[x + 1][y] != 0 and room_condition[x][y + 1] != 0:
if direction == 0 and room_condition[x + 1][y] != 1:
x += 1
continue
elif direction == 1 and room_condition[x][y - 1] != 1:
y -= 1
continue
elif direction == 2 and room_condition[x - 1][y] != 1:
x -= 1
continue
elif direction == 3 and room_condition[x][y + 1] != 1:
y += 1
continue
else:
break
if room_condition[x - 1][y] == 0 or room_condition[x][y - 1] == 0 or room_condition[x + 1][y] == 0 or room_condition[x][y + 1] == 0:
while True:
if direction == 3 or direction == 2 or direction == 1:
direction -= 1
elif direction == 0 :
direction = 3
if direction == 0 and room_condition[x - 1][y] == 0:
x -= 1
break
elif direction == 1 and room_condition[x][y + 1] == 0:
y += 1
break
elif direction == 2 and room_condition[x + 1][y] == 0:
x += 1
break
elif direction == 3 and room_condition[x][y - 1] == 0:
y -= 1
break
print(clean_count)
- 얻어갈 부분
1. 빡구현을 처음해서 힘들었다. 요구사항과 정확히 수행하는데 실수가 들어갔다.
조건문에 ==이 아닌 !=을 하나 넣어버려서
디버깅하기 힘들었다
2. 디버깅을 할 때
trial 횟수와
print('---------------------------') 로 구분하면 어디서
무한루프가 도는지 체크할 수 있다
3. 파이썬에서 영역문제를 풀 때
리스트 내에 방향 혹은 흐름제어를 넣어서 푸는 방법이 있다. 다음에는 꼭 익혀서 풀자
'알고리즘(백준) > 구현' 카테고리의 다른 글
| [알고리즘/구현] 20055번 : 컨베이어 벨트 위의 로봇 - python (0) | 2024.01.02 |
|---|---|
| [알고리즘/구현] 2563번 : 색종이 - python (0) | 2023.12.31 |
| [알고리즘/구현] 1138번 : 한 줄로 서기 - python (0) | 2023.12.29 |
| [알고리즘/구현] 20006번 : 랭킹전 대기열 - python (1) | 2023.12.29 |
| [알고리즘/구현] 2852번 : NBA 농구 - python (1) | 2023.12.29 |