-
[이것이 코딩 테스트다] 게임 개발programing/Algorithm 2021. 8. 11. 22:04
게임 개발
이것이 코딩 테스트다 with python
난이도 2
python3
def addCoord(coord1, coord2): [x, y] = coord1 [a, b] = coord2 return [x + a, y + b] def search(coordinate, d, visited, map): boundary = len(map) destination = addCoord(coordinate, d) [destX, destY] = destination isInBoundaryX = 0 <= destX < boundary isInBoundaryY = 0 <= destY < boundary if (not isInBoundaryX) or \ (not isInBoundaryY) or \ destination in visited \ or map[destX][destY] == 1: return False return True def turnHead(head): head -= 1 if head < 0: head = 3 return head def go(coordinate, d, visited, head): coordinate = addCoord(coordinate, d) if coordinate not in visited: visited.append(coordinate) return turnHead(head), coordinate, visited def back(coordinate, d, visited): coordinate = addCoord(coordinate, d) if coordinate not in visited: visited.append(coordinate) def DFS(map, coordinate, head, direction, visited): print(coordinate, head, visited, '\n') for i in range(len(direction)): d = direction[head] isCanGo = search(coordinate, d, visited, map) if(isCanGo): head, coordinate, visited = go(coordinate, d, visited, head) DFS(map, coordinate, head, direction, visited) else: head = turnHead(head) isCanGoBack = search(coordinate, direction[turnHead(head)], visited, map) if isCanGoBack: back(coordinate, direction[turnHead(head)], visited) DFS(map, coordinate, head, direction, visited) def solution(map, coordinate, head): west = [0, -1] north = [-1, 0] east = [0, 1] south = [1, 0] direction = [west, north, east, south] visited = [coordinate] DFS(map, coordinate, head, direction, visited) return len(visited) _map = [ [1, 1, 1, 1], [1, 0, 0, 1], [1, 1, 0, 1], [1, 1, 1, 1], ] print(solution(_map, [1, 1], 0))
'programing > Algorithm' 카테고리의 다른 글
[이것이 코딩테스트다] 미로 탈출 (0) 2021.08.13 [이것이 코딩테스트다] 음료수 얼려 먹기 (0) 2021.08.13 큰 수의 법칙 (0) 2021.07.23 [Programmers] 문자열 압축 (0) 2021.07.04 [Programmers] 로또의 최고 순위와 최저 순위 (0) 2021.05.23 댓글