Algorithm89 [백준] 10451번 순열 사이클 간단한 탐색이기 때문에 DFS로 풀면 금방 한다 아주 간단한 재귀로 금방 풀 수 있는 문제 import sys N = int(input()) def DFS(i): visited[i] = True for ni in graph[i]: if visited[ni] is False: DFS(ni) for _ in range(N): counter = 0 M = int(input()) graph = [[] for _ in range(M + 1)] visited = [False] * (M + 1) data = list(map(int, sys.stdin.readline().split())) for i in range(1, len(data) + 1): graph[i].append(data[i - 1]) for i in r.. 2020. 9. 21. [백준] 11724번 연결 요소의 개수 아... 처음에 이 코드로 풀었는데 시간초과가 떳다.. from collections import deque def BFS(): counter = 0 nodes = list(range(1, N + 1)) visited = [] que = deque() while len(nodes) != 0: que.append(nodes[0]) while que: node = que.popleft() if node in visited: continue nodes.remove(node) if node not in visited: visited.append(node) que.extend(sorted(graph[node])) counter += 1 return counter N, M = map(int, input().spli.. 2020. 9. 20. [백준] 16953번 A → B 숨바꼭질을 풀었다면 쉽게 풀 수 있는 문제 from queue import Queue LIMIT = 1000000001 def BFS(): flag = 0 counter = 1 que = Queue() que.put([A, counter]) while not que.empty(): tx, tc = que.get() counter = tc if tx == B: flag = 1 break for nx in (tx * 2, int(str(tx)+'1')): if 0 < nx < LIMIT: que.put([nx, tc + 1]) if flag: return counter return -1 A, B = map(int, input().split()) print(BFS()) 2020. 9. 20. [백준] 1967번 숨바꼭질 처음 코드는 너무 성능이 느렸는데 개선해서 굉장히 빨라졌다.. 약간 for문을 다양하게 쓰는 방법을 익힌거 같아서 기분이 좋고 좀 더 개선이 가능할 것으로 생각되서 개선은 내일.. # 1697 from collections import deque LIMIT = 100001 N, K = map(int, input().split()) def find(): counter = 0 que = deque([N]) qc = deque([0]) while que: temp = que.popleft() counter = qc.popleft() if temp == K: break for nx in (temp+1, temp-1, temp*2): if 0 2020. 9. 15. 이전 1 ··· 14 15 16 17 18 19 20 ··· 23 다음