목록깊이 우선 탐색 (10)
정화 코딩
https://www.acmicpc.net/problem/1987 #include #include using namespace std;int r, c;vector m;bool vst[26];int maxc = 0;int di[] = {1, -1, 0, 0};int dj[] = {0, 0, 1, -1};void dfs(int curi, int curj, int cnt) { if (cnt > maxc) maxc = cnt; for (int k = 0; k = 0 && nxti = 0 && nxtj > r >> c; m = vector(r); for (int i = 0; i > m[i]; } vst[m[0][0] - 'A'] = true; dfs(0, 0, 1); ..
https://www.acmicpc.net/problem/1967 #include #include using namespace std;vector>> g;vector vst;int maxc = 0;void dfs(int cur, int cnt) { vst[cur] = true; if (cnt > maxc) maxc = cnt; for (auto nxt: g[cur]) { if (!vst[nxt.first]) dfs(nxt.first, cnt + nxt.second); }}int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; g = vector..
https://www.acmicpc.net/problem/2638 #include #include #include using namespace std;int dx[] = {1, -1, 0, 0};int dy[] = {0, 0, 1, -1};int n, m;vector> g;bool bfs() { vector> vst(n, vector(m, false)); vector> out(n, vector(m, false)); bool finish = true; queue> q; q.emplace(0, 0); vst[0][0] = true; while (!q.empty()) { int curi = q.front().first; int curj = q.fr..
https://www.acmicpc.net/problem/1012 #include #include #include using namespace std;int dn[4] = {0, 0, -1, 1};int dm[4] = {-1, 1, 0, 0};int m, n, k;int cnt = 0;vector> g;void bfs(vector start) { queue> q; cnt++; q.push(start); g[start[0]][start[1]] = 0; while (!q.empty()) { vector cur = q.front(); q.pop(); for (int i = 0; i = 0 && tn = 0 && tm > tn; for (in..
5/12. 바이러스 (백준 2606번) https://www.acmicpc.net/problem/2606 //C++#include #include #include using namespace std;vector visited;vector> g;int n;int bfs(int start) { queue q; int cnt = 0; q.push(start); visited[start] = true; while (!q.empty()) { int cur = q.front(); q.pop(); for (int nxt: g[cur]) { if (!visited[nxt]) { q.push(nxt); ..
09-1. 트리 알아보기 067. 트리의 부모 찾기 (백준 11725번) https://www.acmicpc.net/problem/11725 from sys import stdin import sys sys.setrecursionlimit(10 ** 6) n = int(stdin.readline()) g = [[] for _ in range(n+1)] parent = [0] * (n+1) def dfs(pre, cur): for nxt in g[cur]: if nxt != pre: parent[nxt] = cur dfs(cur, nxt) for _ in range(n-1): a, b = map(int, stdin.readline().split()) g[a].append(b) g[b].append(a) d..
08-6. 플로이드-워셜 061. 플로이드 (백준 11404번) https://www.acmicpc.net/problem/11404 from sys import stdin import sys n = int(stdin.readline()) m = int(stdin.readline()) g = [[sys.maxsize for _ in range(n+1)] for _ in range(n+1)] for i in range(n+1): g[i][i] = 0 for _ in range(m): a, b, c = map(int, stdin.readline().split()) if c < g[a][b]: g[a][b] = c for i in range(1, n+1): # 경유지 i에 관해 for j in range(1, ..
A. 여러분의 다리가 되어 드리겠습니다! (백준 17352번) https://www.acmicpc.net/problem/17352 #python from sys import stdin import sys sys.setrecursionlimit(100000) def find(a): if parent[a] == a: return a else: parent[a] = find(parent[a]) return parent[a] def union(a, b): a = find(a) b = find(b) if a != b: parent[b] = a n = int(stdin.readline()) parent = [i for i in range(n+1)] for _ in range(n-2): a, b = map(int, ..