Created
November 6, 2019 16:30
-
-
Save inspirit941/70c8e1820eed27f448fd218594862677 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
| from collections import deque | |
| from copy import deepcopy | |
| n, m = map(int, sys.stdin.readline().split()) | |
| dirs = [(0,-1), (-1,0), (0,1), (1,0)] | |
| back = {0:(1, 0), 1:(0,-1), 2:(-1,0),3:(0,1)} | |
| # a = {0:"북",1:'동',2:'남',3:"서"} | |
| # 현재 청소기 좌표 | |
| y, x, head = map(int, sys.stdin.readline().split()) | |
| maps = [] | |
| for _ in range(n): | |
| maps.append(list(map(int, sys.stdin.readline().split()))) | |
| def bfs(start, maps): | |
| queue = deque() | |
| queue.append(start) | |
| # 청소기가 청소한 횟수 | |
| cnt = 0 | |
| while queue: | |
| y, x, head = queue.popleft() | |
| if maps[y][x] == 0: | |
| # 청소하지 않은 곳은 0이고, 벽은 1이기 때문에 | |
| # 둘 모두와 겹치지 않는 2를 '청소 완료한 좌표'로 정의 | |
| maps[y][x] = 2 | |
| cnt += 1 | |
| # 바라보는 곳 기준 왼쪽으로 이동하기 위한 좌표 | |
| dy, dx = dirs[head] | |
| # 왼쪽으로 이동했을 때의 좌표 | |
| ny, nx = y + dy, x + dx | |
| # 바라보는 곳 기준, 후진해야 할 때의 좌표 | |
| by, bx = back[head] | |
| # 더 이상 움직일 수 없는 경우 | |
| cant_move = True | |
| for ty, tx in dirs: | |
| # 청소할 곳이 남아있으면, 그곳으로 움직일 수 있음 | |
| if maps[y+ty][x+tx] == 0: | |
| cant_move = False | |
| break | |
| if 0 <= ny < len(maps) and 0 <= nx < len(maps[0]): | |
| # a조건 | |
| if maps[ny][nx] == 0: | |
| new_head = (head - 1) % 4 | |
| queue.append((ny, nx, new_head)) | |
| elif cant_move: | |
| # d조건. | |
| if maps[y+by][x+bx] == 1: | |
| break | |
| else: | |
| # c조건 | |
| queue.append((y+by, x+bx, head)) | |
| else: | |
| # 'b조건' | |
| new_head = (head - 1) % 4 | |
| queue.append((y, x, new_head)) | |
| else: | |
| # b조건 | |
| new_head = (head - 1) % 4 | |
| queue.append((y, x, new_head)) | |
| return cnt | |
| print(bfs((y,x,head), maps)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment