ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [이것이 코딩 테스트다] 게임 개발
    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))

    댓글

Designed by black7375.