CodingTest/Baekjun Online Judge

[ BOJ / 파이썬 ] 14503 로봇 청소기

EEOOOO 2022. 10. 10. 14:55

나는 ! 아무래도 bfs문제가 좋다 ! ㅋㅋㅋ 재밌어.. ㅎ.ㅎ.. 그런 것치고 정답을 한 큐에 못 내는게 약간 속상할 따름..

 

해당 문제를 q를 비우는 식으로 진행했더니 후진 문제를 해결하지 못 했다. 

https://11-20.tistory.com/2

 

BOJ 14503 로봇 청소기

정말 힘들게 구현했네요 이 망할 로봇청소기 ㅠㅠ 여러 번의 문제를 잘못 이해해서 있는 코드를 살짝 살짝 수정한 결과 굉장히 난잡한 코드가 됐습니다. 혹시나마 저 같은 사람이 있다면 조금이

11-20.tistory.com

 

이 분과 같은 문제 발생..

 

# q를 통해서 문제를 운영해서 후진 부분에서 에러가 남

import sys
input = sys.stdin.readline
from collections import deque

# 0, 1, 2, 3
dx = [-1, 0, 1, 0]
dy = [0, 1, 0, -1]
q = deque([])
n, m = map(int, input().split())
r, c, d = map(int, input().split())
board = [list(map(int, input().split())) for _ in range(n)]
visited = [[0]*n for _ in range(n)]

q.append((r, c, d))
visited[r][c] = 1
cleaned_block_count = 1
while q:
  x, y, d = q.popleft()
  four_direction_cleaned = False
  for i in range(4):
    nx = x + dx[(d+i)%4]
    ny = y + dy[(d+i)%4]
    if 0 <= nx < n and 0 <= ny < n and not visited[nx][ny]:
      if board[nx][ny] == 0: #아직 청소 안 한 칸
        visited[nx][ny] = 1
        q.append((nx, ny, (d+i)%4))
        cleaned_block_count += 1
      four_direction_cleaned = True
      
  if not four_direction_cleaned:
    nx = x - dx[d]
    ny = y - dy[d]
    if 0 <= nx < n and 0 <= ny < n and not visited[nx][ny]:
      if board[nx][ny] == 0:
        visited[nx][ny] = 1
        q.append((nx, ny, d))
        cleaned_block_count += 1
    else:
      break

print('result: ', cleaned_block_count)

 

이게 그 코드  

 

그래서 다른 풀이 참조해서 공부했다.

 

https://hae-sooo97.tistory.com/49

 

[백준 14503번 - 로봇청소기]-파이썬

solved.ac 난이도 : GOLD5 백준 14503번- 파이썬 풀이 <문제> https://www.acmicpc.net/problem/14503 14503번: 로봇 청소기 로봇 청소기가 주어졌을 때, 청소하는 영역의 개수를 구하는 프로그램을 작성하시오...

hae-sooo97.tistory.com