
백준 1926 그림 (파이썬, 자바)
2022. 12. 18. 17:30
백준
풀이 및 해설(주석) [파이썬] import sys; from collections import deque def bfs(r, c, visit): Q = deque() Q.append((r, c)) visit.add((r, c)) tmp_rst = 1 while Q: r, c = Q.popleft() for di, dj in [[1, 0], [0, 1], [-1, 0], [0, -1]]: nr, nc = r + di, c + dj # 범위 체크, 방문 체크, 1인지 체크 if 0 = M) continue; if (visit[nr][nc] == 0 && arr[nr][nc] == 1) { tmpRst += 1; Q.add(nr); Q.add(nc); visit[nr][nc] = 1; } } } rst = ..

백준 1461 도서관 (파이썬)
2022. 12. 15. 19:07
백준
풀이 및 해설(주석) import sys def main(): # 일단 정렬 books.sort() # 가장 긴 거리 모두 음수일 수 있으므로 둘 다 abs처리 max_dist = max(abs(books[0]), abs(books[-1])) # 음수, 양수 따로 반복문을 돌 것 이기때문에 기준 점 정하기 turn_idx = N # 이 때 모두 음수라면 turn_idx는 그냥 N일 것이니까 양수 일 때의 반복문을 돌지 않음 for i in range(N): if books[i] > 0: turn_idx = i break rst = 0 for i in range(N-1, turn_idx-1, -M): rst += books[i] * 2 for i in range(0, turn_idx, M): rst += ..

백준 3015 오아시스 재결합 (파이썬)
2022. 12. 14. 16:56
백준
풀이 및 해설(주석) import sys N = int(sys.stdin.readline().strip()) S = [] rst = 0 for _ in range(N): hei = int(sys.stdin.readline().strip()) # 스택이 있고 그 스택의 마지막의 키가 나보다 작으면 그 수를 pop과 rst에 + # pop하는 경우는 나와 이어지는 경우임 while S and S[-1][0] < hei: rst += S.pop()[1] # 스택이 빈 경우 그냥 넣어주기 if not S: S.append([hei, 1]) # 스택의 마지막 키가 나와 같은 경우, 일단 pop해서 그 수를 rst에 더해주고 elif S and S[-1][0] == hei: cnt = S.pop()[1] rst +..

백준 2504 괄호의 값 (파이썬)
2022. 12. 13. 19:45
백준
풀이 및 해설(주석) import sys # 먼저 괄호의 짝이 제대로 맞는지 확인부터 하는 함수 # main함수에서 확인 할 수 있지만 그렇게 하면 코드가 너무 복잡하고 지저분해짐 def is_right(): stack = [] for s in st: if s == '(' or s == '[': stack.append(s) elif s == ')': if not stack: return False elif stack[-1] == '(': stack.pop() else: return False elif s == ']': if not stack: return False elif stack[-1] == '[': stack.pop() else: return False if stack: return False e..

백준 9012 괄호 (자바)
2022. 12. 12. 21:54
백준
풀이 import java.io.*; import java.util.*; public class Main { public static StringBuilder sb = new StringBuilder(); public static BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); public static void main(String[] args) throws IOException { int N = Integer.parseInt(bf.readLine()); for (int i = 0; i < N; i++) { solve(N); } System.out.println(sb); } public static void solve(i..

백준 6603 로또 (파이썬, 자바)
2022. 12. 12. 21:30
백준
풀이 1 (재귀 함수) import sys # N과 M을 풀었다면 똑같이 풀면 됨 def solve(depth, K): if depth == 6: print(*ans) return for i in S: if visited[i]: continue if not ans: ans.append(i) visited[i] = 1 solve(depth + 1, K) ans.pop() visited[i] = 0 elif i > ans[-1]: ans.append(i) visited[i] = 1 solve(depth+1, K) ans.pop() visited[i] = 0 while 1: K, *S = map(int, sys.stdin.readline().split()) if K == 0: break visited = [0..

백준 2477 참외밭 (파이썬, 자바)
2022. 12. 12. 17:14
백준
풀이 (파이썬) N = int(input()) arr = [list(map(int, input().split())) for _ in range(6)] max_w = 0 max_h = 0 max_w_idx = 0 max_h_idx = 0 for i in range(6): if arr[i][0] == 1 or arr[i][0] == 2: if max_w < arr[i][1]: max_w = arr[i][1] max_w_idx = i elif arr[i][0] == 3 or arr[i][0] == 4: if max_h < arr[i][1]: max_h = arr[i][1] max_h_idx = i min_r = abs(arr[(max_w_idx - 1) % 6][1] - arr[(max_w_idx + 1)%6..

백준 17144 미세먼지 안녕! (파이썬)
2022. 12. 8. 15:39
백준
풀이 및 해설(주석) import sys; from collections import deque # 먼지 확산 함수 def spread(): # 먼지 담기 munji = deque() for i in range(N): for j in range(M): if room[i][j] >= 1: munji.append((i, j, room[i][j])) # 먼지 확산 while munji: r, c, amount = munji.popleft() d_count = 0 for di, dj in [[1, 0], [0, 1], [-1, 0], [0, -1]]: nr, nc = r + di, c + dj if 0