본문 바로가기

Algorithm42

[BaekJoon] 백준 알고리즘 4963번 / 섬의 개수 / DFS / Python import sys sys.setrecursionlimit(10**6) dr = [-1, -1, -1, 0, 0, 1, 1, 1] dc = [-1, 0, 1, -1, 1, -1, 0, 1] def dfs(a, b): global island, visited visited[a][b] = 1 for dir in range(8): nr = dr[dir] + a nc = dc[dir] + b if nr >= 0 and nr = 0 and nc < w: if arr[nr][nc] == 1 and visited[nr][nc] == 0: dfs(nr,nc) while True: island = 0 w, h = map(int,input().split()) if w == 0 and h == 0 : .. 2023. 3. 25.
[BaekJoon] 백준 알고리즘 14502번 / 연구소 / dfs / Python import sys from collections import deque from itertools import combinations import copy input = sys.stdin.readline N,M = map(int,input().split()) board = [list(map(int,input().split())) for _ in range(N)] # 준비 1 safe_zone = [] virus = [] res = 0 dx = [-1,0,1,0] dy = [0,-1,0,1] # 감염 def BFS(): global res cnt = len(safe_zone)-3 ch_virus = deque([]) for x,y in virus: ch_virus.append((x,y)) while ch.. 2023. 3. 18.
[BaekJoon] 백준 알고리즘 1966번 / 프린터 큐 / 시뮬레이션 / Python import sys sys.stdin = open( "input.txt", "r") t=int(input()) for i in range(t): cnt=0 n,m=map(int, input().split()) #문서의 개수,몇번째에 놓여있는지 pr=list(map(int, input().split())) #중요도 m_pr = [0 for i in range(n)] m_pr[m]=1#중요도가 궁금한 문서(m번째 문서)의 인덱스를 임의로 1로 표시 while True:#큐의 첫번째 값이 내가 찾는 값(m)일때까지 반복 if pr[0]==max(pr): #중요도가 제일 큰 값이면 cnt에 1을 더해준다 cnt+=1 if m_pr[0]!=1: #원하는 문서가 아니라면 del pr[0] #중요도의 첫번째 값 삭제.. 2023. 3. 17.
[BaekJoon] 백준 알고리즘 11724번 / 연결 요소의 개수 / DFS / Python """ 1. 시나리오 - dfs를 돌면서 빠져나오면 +1을 해준다. 2. 시간 복잡도 - 정점: 1,000개 - 간선: (1,000 x 999) / 2 - 500,500 < 2억개 가능! 3. 자료구조 - [False] * N - adj[[]] """ import sys sys.setrecursionlimit(10**6) input = sys.stdin.readline def dfs(c): if v[c] == True: return v[c] = True for i in adj[c]: dfs(i) N,M = map(int, input().split()) adj = [[] for _ in range(N+1) ] for _ in range(M): s, e = map( int, input().split() ) .. 2023. 3. 11.
[BaekJoon] 백준 알고리즘 6603번 / 로또 / 백트래킹(DFS) / Python def dfs( depth, idx ): if len(q) == 6: print( " ".join(map(str,q))) return for i in range( idx, k ): q.append(S[i]) dfs(depth + 1, i + 1) q.pop() while True: global S array = list(map(int, input().split())) k = array[0] S = array[1:] q = [] dfs( 0, 0 ) if k == 0: exit() print() 2023. 3. 10.
[BaekJoon] 백준 알고리즘 15686번 / 치킨 배달 / 백트래킹 / Python import sys from itertools import combinations sys.stdin = open( "input.txt", "r") n, m = map( int, input().split() ) city = list( list(map(int, input().split())) for _ in range( n )) result = 999999 house = [] chicken = [] for i in range(n): for j in range(n): if city[i][j] == 1: house.append( [i, j] ) # 집 좌표 저장 elif city[i][j] == 2: chicken.append( [i, j]) # 치킨집 좌표 저장 def solve( ): global resul.. 2023. 3. 8.