본문 바로가기

CT15

[Python] 백준 2606번 : 바이러스 https://www.acmicpc.net/problem/2606   BFSfrom collections import dequeimport sysN = int(sys.stdin.readline().rstrip())E = int(sys.stdin.readline().rstrip())graph = [[] for i in range(N+1)]visited = [False] * (N+1)for _ in range(E): a, b = map(int, sys.stdin.readline().split()) graph[a].append(b) graph[b].append(a) for i in graph: i.sort()def bfs(visited, graph, start): que.. 2024. 9. 9.
[Python] 백준 2178번 : 미로 탐색 https://www.acmicpc.net/problem/2178   import sysfrom collections import dequeinput = sys.stdin.readlinegraph = []N, M = map(int, input().split())for _ in range(N): graph.append(list(map(int, input().rstrip()))) def bfs(x, y): # 이동할 방향 정의 dx = [-1,1,0,0] dy = [0,0,-1,1] queue = deque() queue.append((x,y)) # 큐가 빌 때 까지 반복 while queue: x, y = queue.popl.. 2024. 9. 8.
[Python] 백준 1260번 : DFS와 BFS https://www.acmicpc.net/problem/1260    from collections import dequeimport sysN, M, V = map(int, sys.stdin.readline().split())graph = [[] for _ in range(N+1)]visited1 = [False] * (N+1)visited2 = [False] * (N+1)for i in range(M): tmp1, tmp2 = map(int, sys.stdin.readline().split()) graph[tmp1].append(tmp2) graph[tmp2].append(tmp1) for i in graph: i.sort()def dfs(graph, visited, sta.. 2024. 7. 24.
[Python] 백준 2346번 : 풍선 터뜨리기 https://www.acmicpc.net/problem/2346  from collections import dequeimport sysN = int(sys.stdin.readline().rstrip())# 초기 인덱스를 저장해야한다.queue = deque(enumerate(map(int, sys.stdin.readline().split())))result = []while queue: idx,tmp = queue.popleft() print(idx+1, end=" ") if tmp 0: queue.rotate(-tmp+1)  popleft() 시켜도 기존 index를 사용해야 하므로 enumerate()를 사용했다.초기 인덱스를 저장해야 하는 idea.규칙을 이.. 2024. 7. 23.