[프로그래머스] 게임 맵 최단거리 / 파이썬 / BFS
💡solutions ) 💬 BFS 알고리즘을 통해 최단 경로를 찾는 문제 💬 이때 거리를 찾고, 또 이미 지났던 경로를 다시 방문하지 않기 위해 visit 배열을 만들어서 거리를 저장하기 👨💻code ) from collections import deque def solution(maps): answer = 0 dx, dy = [-1, 1, 0, 0], [0, 0, -1, 1] q = deque([(0, 0)]) visit = [[0]*len(maps[0]) for _ in range(len(maps))] visit[0][0] = 1 maps[0][0] = 0 while q: x, y = q.popleft() for k in range(4): nx, ny = x + dx[k], y + dy[k] if 0
2021. 10. 16.
[프로그래머스] 모의고사 / 파이썬 / Python
👨💻code ) def solution(answers): first = [1,2,3,4,5] second = [2,1,2,3,2,4,2,5] third = [3,3,1,1,2,2,4,4,5,5] res = [[1,0], [2,0], [3,0]] for i in range(len(answers)): if answers[i] == first[i % len(first)]: res[0][1] += 1 if answers[i] == second[i % len(second)]: res[1][1] += 1 if answers[i] == third[i % len(third)]: res[2][1] += 1 target, max_cnt = [], 0 for num, cnt in res: if cnt > max_cnt: ..
2021. 10. 5.