본문 바로가기

Algorithm/BFS, DFS33

[백준] 12761번 돌다리 쉬운 문제 from collections import deque LIMIT = 100001 visted = [0] * LIMIT A, B, N, M = map(int, input().split()) que = deque() que.append([N, 0]) ans = 0 while que: cx, cc = que.popleft() if cx == M: ans = cc break if visted[cx] == 1: continue visted[cx] = 1 for val in [cx + 1, cx - 1, cx + A, cx + B, cx - A, cx - B, cx * A, cx * B, cx - (cx * A), cx - (cx * B)]: if -1 < val < LIMIT and not visted.. 2021. 2. 4.
[백준] 12851번 숨바꼭질 2 from collections import deque def find(N): que = deque() que.append(N) mapData[N][0] = 0 mapData[N][1] = 1 while que: temp = que.popleft() for nx in (temp + 1, temp - 1, temp * 2): if 0 2020. 12. 4.
[백준] 2206번 벽 부수고 이동하기 말이 되고픈 원숭이 문제를 풀었다면 아주 쉬운 문제 import sys from collections import deque def bfs(): visited[0][0][1] = 1 que = deque() que.append([0, 0, 1]) while que: x, y, k = que.popleft() if x == N - 1 and y == M - 1: return visited[x][y][k] for i in range(len(direction)): nx = x + direction[i][0] ny = y + direction[i][1] if 0 2020. 9. 24.
[백준] 7562번 나이트의 이동 쉬운문제.. 그냥 한칸씩 이동에서 범위만 조금 넓어진 문제 from collections import deque kDirection = [[-1, -2], [-2, -1], [-2, 1], [-1, 2], [1, 2], [2, 1], [2, -1], [1, -2]] def BFS(startX, startY, endX, endY, visited): que = deque() que.append([startX, startY, 0]) visited[startX][startY] = 1 while que: cx, cy, cc = que.popleft() if cx == endX and cy == endY: print(cc) break for i in range(len(kDirection)): nx = cx + kD.. 2020. 9. 23.