본문 바로가기

BFS36

[백준] 19238번 - 스타트 택시 (파이썬) # 스타트 택시 from collections import deque import sys input = sys.stdin.readline N, M, fuel = map(int, input().split()) # 지도 arr = [[]] for _ in range(N): arr.append([0]+list(map(int, input().split()))) # 현재 위치 taxi = list(map(int, input().split())) # 사람들 정보 people = [] for _ in range(M): people.append(list(map(int, input().split()))) dx = [0, 1, 0, -1] dy = [1, 0, -1, 0] def find_person(): tx, ty =.. 2022. 5. 14.
[백준] 1039번 - 교환 (파이썬) # 교환 from collections import deque N, K = map(int, input().split()) M = len(str(N)) def bfs(N, K): visited = set() visited.add((N, 0)) q = deque() q.append((N, 0)) answer = 0 while q: n, k = q.popleft() if k == K: answer = max(answer, n) continue n = list(str(n)) for i in range(M-1): for j in range(i+1, M): if i == 0 and n[j] == '0': continue n[i], n[j] = n[j], n[i] nn = int(''.join(n)) if (nn, .. 2022. 5. 4.
[프로그래머스] Lv2 - 전력망을 둘로 나누기 (파이썬) https://programmers.co.kr/learn/courses/30/lessons/86971 코딩테스트 연습 - 전력망을 둘로 나누기 9 [[1,3],[2,3],[3,4],[4,5],[4,6],[4,7],[7,8],[7,9]] 3 7 [[1,2],[2,7],[3,7],[3,4],[4,5],[6,7]] 1 programmers.co.kr from collections import deque import sys def solution(n, wires): answer = sys.maxsize for i in range(len(wires)): new_wires = wires[:i] + wires[i+1:] arr = [[]for _ in range(n+1)] for x, y in new_wires: a.. 2022. 5. 3.
[백준] 16197번 - 두 동전 (파이썬) # 두 동전 import copy from collections import deque N, M = map(int, input().split()) arr = [] money = deque() for i in range(N): arr.append(list(input().strip())) for j in range(M): if arr[i][j] == 'o': money.append((i, j)) arr[i][j] = '.' visited = set() visited.add(''.join(map(str, money))) dx = [0, 1, 0, -1] dy = [1, 0, -1, 0] def bfs(): q = deque() q.append((0, money)) while q: cnt, now = q.pop.. 2022. 4. 20.
[백준] 2234번 - 성곽 (파이썬) # 성곽 import sys from collections import deque from collections import defaultdict input = sys.stdin.readline M, N = map(int, input().split()) arr = [] for _ in range(N): arr.append(list(map(int, input().split()))) # 방향은 8,4,2,1 순서로 남,동,북,서 dx = [1, 0, -1, 0] dy = [0, 1, 0, -1] def bfs(a, b, arr, visited, room_num): q = deque() q.append((a, b)) visited[a][b] = room_num room_size = 1 while q: x, y.. 2022. 4. 17.
[백준] 1726번 - 로봇 (파이썬) # 로봇 import sys from collections import deque input = sys.stdin.readline N, M = map(int, input().split()) arr = [list(map(int, input().split())) for _ in range(N)] sa, sb, sd = map(int, input().split()) ea, eb, ed = map(int, input().split()) dx = [0, 0, 0, 1, -1] dy = [0, 1, -1, 0, 0] def bfs(): q = deque() q.append((0, sa-1, sb-1, sd)) visited = [[[False for _ in range(5)] for _ in range(M)] fo.. 2022. 4. 13.
[백준] 9019번 - DSLR (파이썬) # DSLR from collections import defaultdict from collections import deque def get_result(x, k): if k == 'D': return (x*2) % 10000 if k == 'S': return x-1 if x != 0 else 9999 if k == 'L': return (x % 1000)*10 + x//1000 if k == 'R': return (x % 10)*1000 + x//10 T = int(input()) for _ in range(T): a, b = map(int, input().split()) dict = defaultdict(str) dict[a] = '' q = deque() q.append(a) while q: .. 2022. 4. 11.