BOJ80

★BFS&DFS Upper-Intermediate I - 15 Solved★ ☝️ 중상급의 BFS/DFS 유형은 그래프 연결 요소 / 연결 요소 내의 노드 개수 / 최단 거리(Easy) 3개의 알고리즘으로 분류할 수 있다.★ 11724 연결 요소의 개수 ★☝️ DFSimport sysinput=sys.stdin.readlinesys.setrecursionlimit(10000)N,M=map(int,input().split())#dfsdef dfs(graph,start,visited): visited[start] = True for node in graph[start]: if not visited[node]: dfs(graph,node,visited)#graphgraph=[[] for _ in range(N+1)]for _ in range(.. BOJ/🥈 2024. 5. 12.
★Math & Geometry Advanced I - 1 Solved★ ★ 1011 Fly me to the Alpha Centauri ★import sysinput=sys.stdin.readlinefor _ in range(int(input())): x,y=map(int,input().split()) diff=y-x closest_root=int(diff**(1/2)) if diff == (closest_root**2): print(closest_root*2-1) elif (diff-(closest_root**2)) 🍋 kn은 kn-1 / kn / kn+1 중 한 개만 가능 + 맨 앞과 맨 뒤의 거리 step은 1만 가능하다는 조건 하에 수학 규칙성을 찾는 문제→ y-x를 diff라고 한다면, 중복된 step 간격 없이 오름차순 -.. BOJ/🥇 2024. 4. 27.
★Implementation&Simluation Upper-Advanced I - 1 Solved★ ★ 19237 어른 상어 ★ import sys input=sys.stdin.readline from collections import deque N,M,k=map(int,input().split()) #NxNsize / M sharks / smell limit k trace=[] smell=[[0]*N for _ in range(N)] sharks=deque() shark_ds_priorities=[] #shark_ds dx,dy=['?',-1,1,0,0],['?',0,0,-1,1] num_of_sharks=M for i in range(N): l=list(map(int,input().split())) trace.append(l) for j in range(N): if l[j] != 0: sharks... BOJ/🥇 2024. 3. 3.
★Stack & Queue & Deque Advanced I - 1 Solved★ ★ 9935 문자열 폭발 ★ import sys input=sys.stdin.readline s=list(input().rstrip()) explode=list(input().rstrip()) explode_length=len(explode) ans=[] for x in range(len(s)): ans.append(s[x]) if s[x] == explode[-1]: if explode_length BOJ/🥇 2024. 2. 24.
★Two-Pointers Advanced I - 9 Solved★ ★ 15961 회전 초밥 ★ import sysinput=sys.stdin.readlinefrom collections import defaultdictN,d,k,c=map(int,input().split())kinds=[int(input()) for _ in range(N)]y,ans,cnt,freq=0,0,0,defaultdict(int)for x in range(N): while cnt 🧦 x와 y 투 포인터 모두 첫 초밥부터 시작하되, k개의 묶음마다 진행하므로(슬라이딩 윈도우 포스팅 참조), x가 전체 초밥 x(1)부터 x(8)까지 움직일 때 y의 끝은 y(1) 부터 y(11)까지 (k=4일 경우) 움직인다 / dictionary 활용해 freq로 종류 개수 update하며 freq.ke.. BOJ/🥇 2024. 2. 11.
★Two-Pointers Upper-Intermediate I - 5 Solved★ ★ 3273 두 수의 합 ★ import sys input=sys.stdin.readline n=int(input()) l=list(map(int,input().split())) X=int(input()) #array l.sort() #two-pointers x,y=0,n-1 ans=0 while x X y-=1 print(ans) 🍡 오름차순 정렬 뒤, x를 배열 왼쪽 끝 / y를 배열 오른쪽 끝에 배치. l[x]+l[y]가 X보다 클 경우, x를 아무리 오른쪽으로 이동해도 X보다 클 수 밖에 없음. 따라서 y를 왼쪽으로 이동 / l[x]+l[y]가 X라면 정답 찾음! ans+=1 한 뒤, l[x]와 l[y]는 이제 X 나오는 연산에 필요 없으므로 x 오른쪽 이동, y 왼쪽 이동 / l[x]+l[y]가.. BOJ/🥈 2024. 2. 11.
★Implementation&Simluation Advanced I - 5 Solved★ ★ 21610 마법사 상어와 비바라기 ★ import sys input=sys.stdin.readline from collections import deque N,M=map(int,input().split()) baskets=[] for _ in range(N): baskets.append(list(map(int,input().split()))) moves=[] for _ in range(M): moves.append(tuple(map(int,input().split()))) clouds=deque([[N-2,0],[N-2,1],[N-1,0],[N-1,1]]) #(1) clouds moving dx,dy=[0,-1,-1,-1,0,1,1,1],[-1,-1,0,1,1,1,0,-1] for move in mo.. BOJ/🥇 2024. 1. 28.
★Backtracking Upper-Advanced I - 1 Solved★ ★ 9663 N-Queen ★ #n-queens #place n queens (not placed in the same row / col / diagonal dir) def track(x): global ans if x == N: #finished ans+=1 return else: for ny in range(N): if v1[ny] == v2[x+ny] == v3[x-ny] == False: v1[ny] = v2[x+ny] = v3[x-ny] = True track(x+1) v1[ny] = v2[x+ny] = v3[x-ny] = False N=int(input()) ans=0 v1=[False]*(N) #horizontal 0~(N-1) v2=[False]*(2*N-1) #diagonal (right.. BOJ/🥇 2024. 1. 24.
★Backtracking Advanced I - 3 Solved★ ★ 7490 0 만들기 ★ import sys input=sys.stdin.readline def track(nums,exp,start): if start == len(nums)+1: res=eval(exp) if res == 0: ans=[] for i in range(len(exp)): if i>=1 and exp[i-1].isdigit() and exp[i].isdigit(): ans.extend([' ',exp[i]]) else: ans.append(exp[i]) print(*ans,sep='') else: #needs to be added for x in [' ','+','-']: if x == ' ': exp+=str(start) track(nums,exp,start+1) exp=exp[:-1.. BOJ/🥇 2024. 1. 21.
★Backtracking Upper-Intermediate I - 9 Solved★ ★ 15663 N과 M (9) ★ import sys input=sys.stdin.readline N,M=map(int,input().split()) nums=list(map(int,input().split())) nums.sort() num,visited=[],[False]*N def track(): if len(num)==M: print(*num) else: last=0 for i in range(len(nums)): if not visited[i] and last!=nums[i]: visited[i]=True num.append(nums[i]) last=nums[i] track() num.pop() visited[i]=False track() ★ 15664 N과 M (10) ★ import sys .. BOJ/🥈 2024. 1. 20.
★Backtracking Intermediate I - 10 Solved★ ★ 15649 N과 M (1) ★ N,M=map(int,input().split()) ans=[] def track(): if len(ans) == M: #completed print(*ans) else: for x in range(1,N+1): if len(ans) == 0 or x not in ans: ans.append(x) track() ans.pop() #backtrack track() 🙃 backtracking 포스팅 참조 ★ 15650 N과 M (2) ★ N,M=map(int,input().split()) ans=[] def track(): if len(ans) == M: #completed print(*ans) else: for x in range(1,N+1): if len(ans) == .. BOJ/🥈 2024. 1. 18.
★Implementation&Simulation Upper-Intermediate I - 3 Solved★ ★ 2564 경비원 ★ import sys input=sys.stdin.readline w,h=map(int,input().split()) diameter = (w+h)*2 loc=[] ans=0 for _ in range(int(input())): d,l=map(int,input().split()) if d == 3: loc.append(l) elif d == 2: loc.append(l+h) elif d == 4: loc.append(h*2+w-l) else: #d==1 loc.append(h*2+w*2-l) d,l=map(int,input().split()) if d == 3: me = l elif d == 2: me = l+h elif d == 4: me = h*2+w-l else: #d==1 m.. BOJ/🥈 2024. 1. 8.