백준 1927. 최소 힙
·
백준
https://www.acmicpc.net/problem/1927import heapq, sysinput = sys.stdin.readlinen = int(input())heap = []for _ in range(n): x = int(input()) if x == 0: print(heapq.heappop(heap)) if heap else print(0) else: heapq.heappush(heap, x)
백준 12919. A와 B 2
·
백준
https://www.acmicpc.net/problem/12919S = input().rstrip()T = input().rstrip()res = 0def dfs(word): global res if word == S: res = 1 return if len(word) == 0: return if word[-1] == 'A': dfs(word[:-1]) if word[0] == 'B': dfs(word[1:][::-1])dfs(T)print(res)[체크포인트]S->T로 변경 가능한지 검색보다T->S로 가능한지로 해야 시간과 메모리 절약이 되면서 시간초과가 나지 않는다!그러면 if 문 2개로 재귀함수를 돌리는데,1..
백준 20310. 타노스
·
백준
https://www.acmicpc.net/problem/20310'''1은 앞에서부터 지우고0은 뒤에서부터 지우기'''import sysinput = sys.stdin.readlinest = list(input().rstrip())one, zero = st.count('1') // 2, st.count('0') // 2def one_remove(): cnt = 0 for i in range(len(st)): if st[i] == '1': st[i] = 'x' cnt += 1 if cnt == one: returndef zero_remove(): cnt = 0 for i in range..
백준 9328. 열쇠
·
백준
https://www.acmicpc.net/problem/9328import sysfrom collections import dequeinput = sys.stdin.readlinedef bfs(h, w, building, keys): directions = [(-1, 0), (1, 0), (0, -1), (0, 1)] queue = deque() visited = [[False] * w for _ in range(h)] key_set = set(keys) # 열쇠를 저장할 집합(중복 방지를 위해 set 사용) door_dict = {chr(i): [] for i in range(ord('A'), ord('Z') + 1)} # 대문자 문과 위치 저장 doc_count ..
백준 11967. 불켜기
·
백준
https://www.acmicpc.net/problem/11967import sysfrom collections import dequeinput = sys.stdin.readlinen, m = map(int,input().split())switches = [[[] for _ in range(n)] for _ in range(n)] # 스위치 정보 기록for _ in range(m): si, sj, fi, fj = map(int,input().split()) switches[si-1][sj-1].append((fi-1,fj-1)) # 0-based로 변경visited = [[0]*n for _ in range(n)] # 방문했는지 기록lights = [[0]*n for _ in range(n)..
백준 14442. 벽 부수고 이동하기 2
·
백준
https://www.acmicpc.net/problem/14442import sysfrom collections import dequeinput = sys.stdin.readlinen, m, k = map(int,input().split()) # 세로, 가로, 벽 부수기 횟수arr = [list(input().rstrip()) for _ in range(n)]visited = [[[0] * m for _ in range(n)] for _ in range(k+1)] # (벽 부술 수 있는 횟수, 행, 열)def bfs(): q = deque([(k, 0, 0)]) # (남은 벽 부수기 횟수, x, y) visited[k][0][0] = 1 # 시작 지점 방문 처리 (최초 거리 1) ..