728x90

알고리즘 36

11763.전자키트11763.전자키트

12345678910111213141516171819202122232425262728293031323334353637'''130 18 3448 0 5518 7 0TSP문제'''def perm(n, k, cursum): # 원소의 수 뎁스 global ans #가지치기하려면 여기다 넣어야함 # if ans cursum: ans = cursum else: for i in range(n): if visited[i+1] : continue t[k+1] = a[i+1] visited[i+1] = True perm(n, k+1, cursum + dist[t[k]][t[k+1]]) visited[i+1] = False T=int(input())for tc in range(1, T+1): ans = 987654321 ..

1227. 미로2

123456789101112131415161718192021222324252627282930313233343536373839404142import syssys.stdin = open("미로2_input.txt") def find_start(maze): # 시작점을 찾아주는 함수 for i in range(N): for j in range(N): if maze[i][j] == 2:# 완전탐색을 이용하여 출발지부터 먼저 찾아준다. return i, j # 찾은 출발지 i, j 를 리턴 시켜준다. def bfs(x, y): Q = [] Q.append((x, y)) # enQ visited[x][y] = 1 # 방문체크 while Q: x, y = Q.pop(0) # deQ # 3인지 체크 return 1 i..

알고리즘/SWAE 2021.04.07

5174. subtree, 5176 이진탐색, 5177. 이진 힙, 5178. 노드의 합

서브트리 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 T = int(input()) def search(N): global result result += 1 for i in node_t[N]: search(i) for tc in range(1,T+1): E, N = map(int,input().split()) node = list(map(int,input().split())) # 부모 + 자식 노드 번호 상 node_t = [[] for _ in range(E + 2)] idx = 1 for i in node[::2]: node_t[i].append(node[idx]) idx += 2 result = 0 search(N) print("#{} {}".forma..

알고리즘/SWAE 2021.04.06
728x90