반응형

dfs 11

[파이썬, Python] 백준 4963: 섬의 개수

문제 https://www.acmicpc.net/problem/4963 4963번: 섬의 개수 입력은 여러 개의 테스트 케이스로 이루어져 있다. 각 테스트 케이스의 첫째 줄에는 지도의 너비 w와 높이 h가 주어진다. w와 h는 50보다 작거나 같은 양의 정수이다. 둘째 줄부터 h개 줄에는 지도 www.acmicpc.net 코드 import sys sys.setrecursionlimit(10**6) dx = [1, 1, -1, -1, 1, -1, 0, 0] dy = [0, 1, 0, 1, -1, -1, 1, -1] def dfs(x, y): land[x][y] = 0 for i in range(8): nx = x + dx[i] ny = y + dy[i] if 0

[파이썬, Python] 백준 6603: 로또

문제 https://www.acmicpc.net/problem/6603 6603번: 로또 입력은 여러 개의 테스트 케이스로 이루어져 있다. 각 테스트 케이스는 한 줄로 이루어져 있다. 첫 번째 수는 k (6 < k < 13)이고, 다음 k개 수는 집합 S에 포함되는 수이다. S의 원소는 오름차순으로 www.acmicpc.net 코드 from collections import deque while True: nums = deque(map(int, input().split())) k = nums.popleft() if k == 0: break s = [] def dfs(): if len(s) == 6: print(' '.join(map(str,s))) return for i in sorted(nums): i..

[파이썬, Python] 백준 10026: 적록색약

문제 https://www.acmicpc.net/problem/10026 10026번: 적록색약 적록색약은 빨간색과 초록색의 차이를 거의 느끼지 못한다. 따라서, 적록색약인 사람이 보는 그림은 아닌 사람이 보는 그림과는 좀 다를 수 있다. 크기가 N×N인 그리드의 각 칸에 R(빨강), G(초록) www.acmicpc.net 코드 from collections import deque def bfs(x, y): q.append((x, y)) dx, dy = [-1, 1, 0, 0], [0, 0, -1, 1] visited[x][y] = 1 while q: x, y = q.popleft() for i in range(4): nx = dx[i] + x ny = dy[i] + y if 0

[파이썬, Python] 백준 2644: 촌수계산

문제 https://www.acmicpc.net/problem/2644 2644번: 촌수계산 사람들은 1, 2, 3, …, n (1 ≤ n ≤ 100)의 연속된 번호로 각각 표시된다. 입력 파일의 첫째 줄에는 전체 사람의 수 n이 주어지고, 둘째 줄에는 촌수를 계산해야 하는 서로 다른 두 사람의 번호가 주어 www.acmicpc.net 코드 import sys sys.setrecursionlimit(10**7) n = int(input()) a, b = map(int, input().split()) m = int(input()) graph = [[] for _ in range(n+1)] visited = [False] * (n+1) res = list() for _ in range(m): x, y = m..

[파이썬, Python] 백준 15652: N과 M (4)

문제 https://www.acmicpc.net/problem/15652 15652번: N과 M (4) 한 줄에 하나씩 문제의 조건을 만족하는 수열을 출력한다. 중복되는 수열을 여러 번 출력하면 안되며, 각 수열은 공백으로 구분해서 출력해야 한다. 수열은 사전 순으로 증가하는 순서로 출력해 www.acmicpc.net 코드 import sys input = sys.stdin.readline n, m = map(int, input().split()) s = [] def dfs(): if len(s) == m: print(' '.join(map(str,s))) return for i in range(1, n+1): if s: if s[-1] > i: continue s.append(i) dfs() s.pop..

[파이썬, Python] 백준 15651: N과 M (3)

문제 https://www.acmicpc.net/problem/15651 15651번: N과 M (3) 한 줄에 하나씩 문제의 조건을 만족하는 수열을 출력한다. 중복되는 수열을 여러 번 출력하면 안되며, 각 수열은 공백으로 구분해서 출력해야 한다. 수열은 사전 순으로 증가하는 순서로 출력해 www.acmicpc.net 코드 n, m = map(int, input().split()) s = [] def dfs(): if len(s) == m: print(' '.join(map(str,s))) return for i in range(1, n+1): s.append(i) dfs() s.pop() dfs() 설명 앞서 풀었던 문제와 굉장히 유사하다. https://looancheong.tistory.com/14..

[파이썬, Python] 백준 15650: N과 M (2)

문제 https://www.acmicpc.net/problem/15650 15650번: N과 M (2) 한 줄에 하나씩 문제의 조건을 만족하는 수열을 출력한다. 중복되는 수열을 여러 번 출력하면 안되며, 각 수열은 공백으로 구분해서 출력해야 한다. 수열은 사전 순으로 증가하는 순서로 출력해 www.acmicpc.net 코드 n, m = map(int, input().split()) s = [] def dfs(start): if len(s) == m: print(' '.join(map(str,s))) return for i in range(start, n+1): if i not in s: s.append(i) dfs(i+1) s.pop() dfs(1) 설명 https://looancheong.tistory..

[파이썬, Python] 백준 1260: DFS와 BFS

문제 https://www.acmicpc.net/problem/1260 1260번: DFS와 BFS 첫째 줄에 정점의 개수 N(1 ≤ N ≤ 1,000), 간선의 개수 M(1 ≤ M ≤ 10,000), 탐색을 시작할 정점의 번호 V가 주어진다. 다음 M개의 줄에는 간선이 연결하는 두 정점의 번호가 주어진다. 어떤 두 정점 사 www.acmicpc.net 코드 from collections import deque import sys input = sys.stdin.readline n, m, v = map(int,input().split()) graph = [[]for _ in range(n + 1)] for _ in range(m): a, b = map(int, input().split()) graph[a..

[파이썬, Python] 백준 2606: 바이러스

문제 https://www.acmicpc.net/problem/2606 2606번: 바이러스 첫째 줄에는 컴퓨터의 수가 주어진다. 컴퓨터의 수는 100 이하이고 각 컴퓨터에는 1번 부터 차례대로 번호가 매겨진다. 둘째 줄에는 네트워크 상에서 직접 연결되어 있는 컴퓨터 쌍의 수가 주어 www.acmicpc.net 코드 import sys input = sys.stdin.readline c = int(input()) n = int(input()) visited = [False for _ in range(c + 1)] graph = [[]for _ in range(c + 1)] for _ in range(n): a, b = map(int, input().split()) graph[a].append(b) gra..

[파이썬, Python] 백준 24480: 알고리즘 수업 - 깊이 우선 탐색 2

문제 https://www.acmicpc.net/problem/24480 24480번: 알고리즘 수업 - 깊이 우선 탐색 2 첫째 줄에 정점의 수 N (5 ≤ N ≤ 100,000), 간선의 수 M (1 ≤ M ≤ 200,000), 시작 정점 R (1 ≤ R ≤ N)이 주어진다. 다음 M개 줄에 간선 정보 u v가 주어지며 정점 u와 정점 v의 가중치 1인 양 www.acmicpc.net 코드 import sys input = sys.stdin.readline sys.setrecursionlimit(10**9) n, m, r = map(int,input().split()) graph = [[] for _ in range(n+1)] visited = [0] * (n+1) cnt = 1 def dfs(gra..

반응형