![[백준 C++] 1012 : 유기농 배추](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdna%2FDLDMk%2FbtsKMj7Yoda%2FAAAAAAAAAAAAAAAAAAAAAGr2Mg0wyaX6wjXsdYrxNMmpXIKTy2eIbl9_TGrpKEUy%2Fimg.png%3Fcredential%3DyqXZFxpELC7KVnFOS48ylbz2pIh7yKj8%26expires%3D1753973999%26allow_ip%3D%26allow_referer%3D%26signature%3DYo8bpMsdAtNfnXJ%252FYG6Wi18eeF4%253D)
https://www.acmicpc.net/problem/1012이 전에 풀었던 단지번호붙이기와 비슷한 유형의 그래프 탐색 문제이다.DFS로 구현하였다.#include #include #include using namespace std;vector> list;vector> visited;vector answer;int dx[4] = { -1, 0, 1, 0 };int dy[4] = { 0, 1, 0, -1 };int M, N;void dfs(int x, int y){ for (int i = 0; i = 0 && nextX = 0 && nextY > M >> N >> K; list = vector>(N, vector(M, 0)); visited = vector>(N, vector(M, false)); fo..
![[백준 C++] 2667 : 단지번호붙이기](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdna%2FPoF2p%2FbtsKL1MsVxx%2FAAAAAAAAAAAAAAAAAAAAAHIDCJAL27OYJE5-tBavuz4UOb17zHkFu69c0EIympBS%2Fimg.png%3Fcredential%3DyqXZFxpELC7KVnFOS48ylbz2pIh7yKj8%26expires%3D1753973999%26allow_ip%3D%26allow_referer%3D%26signature%3DDCvDPECcsqPnwDe8swxQd%252FYWmMA%253D)
https://www.acmicpc.net/problem/2667DFS와 BFS 두 방법으로 구현 가능하다.필자는 DFS로 구현하였다.#include #include #include using namespace std;vector map;vector> visited;int N;int dx[4] = {-1 , 0, 1, 0};int dy[4] = { 0, 1, 0, -1 };int cnt;void dfs(int x, int y){ for (int i = 0; i = 0 && nextX = 0 && nextY answer; cin >> N; map = vector(N); visited = vector>(N, vector(N, false)); for (int i = 0; i > temp; map[i] = ..
![[백준 C++] 24480 : 알고리즘 수업 - 깊이 우선 탐색 2](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdna%2FcXwVjl%2FbtsKDBIZHq5%2FAAAAAAAAAAAAAAAAAAAAABqIYyhqeUS4oQO9TO3tmpOg76ZCcs_0T7IZS34lhMiQ%2Fimg.png%3Fcredential%3DyqXZFxpELC7KVnFOS48ylbz2pIh7yKj8%26expires%3D1753973999%26allow_ip%3D%26allow_referer%3D%26signature%3DomD7zDbOoqHiNTJ5Qwtc9gssENo%253D)
https://www.acmicpc.net/problem/2448024479번 문제와 동일하지만, 내림차순으로 방문한다는 점이 다른 점이다.#include #include #include using namespace std;vector> list;vector visitedOrder;int order = 1;void dfs(int start){ visitedOrder[start] = order; for (int i = 0; i b;}int main(){ int N, M, R = 0; cin >> N >> M >> R; list = vector>(N + 1); visitedOrder = vector(N + 1, 0); for (int i = 0; i > u >> v; list[u].push_back(v)..
![[백준 C++] 24479 : 알고리즘 수업 - 깊이 우선 탐색 1](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdna%2FbE5vyw%2FbtsKDBOxcvc%2FAAAAAAAAAAAAAAAAAAAAAMna5ZS_OFxSgO2TcqvXbZsgitd2eEpKI281U0cBrH00%2Fimg.png%3Fcredential%3DyqXZFxpELC7KVnFOS48ylbz2pIh7yKj8%26expires%3D1753973999%26allow_ip%3D%26allow_referer%3D%26signature%3DeXLaV6xYxSMAI0XBlXKgyz0tIcs%253D)
https://www.acmicpc.net/problem/24479출력이 노드의 순서대로 출력함과 동시에 방문 순서를 적는 형식이다.#include #include #include using namespace std;vector> list;vector visitedOrder;int order = 1;void dfs(int start){ visitedOrder[start] = order; for (int i = 0; i > N >> M >> R; list = vector>(N + 1); visitedOrder = vector(N + 1, 0); for (int i = 0; i > u >> v; list[u].push_back(v); list[v].push_back(u); } for (int i = 0;..
![[백준 C++] 14889 : 스타트와 링크](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdna%2FssOzC%2FbtsKCUgK79l%2FAAAAAAAAAAAAAAAAAAAAACGAwdXyjM2uz3sFdDdhQXpxa5yiBOtIRm7ZQVxijhT0%2Fimg.png%3Fcredential%3DyqXZFxpELC7KVnFOS48ylbz2pIh7yKj8%26expires%3D1753973999%26allow_ip%3D%26allow_referer%3D%26signature%3DOEo1awmhJ3R3ZVVz0kIAEXum6sU%253D)
https://www.acmicpc.net/problem/14889#include #include #include using namespace std;int N;vector> list;vector visited;int answer = INT_MAX;void calculate(){ int startVal = 0; int linkVal = 0; for (int i = 0; i linkVal ? startVal - linkVal : linkVal - startVal); return;}void generateTeam(int num, int idx){ if (num == N / 2) calculate(); for (int i = idx; i > N; list = vector>(N, vector(N, 0))..
![[백준 C++] 14888 : 연산자 끼워넣기](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdna%2FW1tYG%2FbtsKBlfdybu%2FAAAAAAAAAAAAAAAAAAAAAOkpqH5gYrVil3UG8hSZgBN9MZms8HZS1lP4I_Rz06lb%2Fimg.png%3Fcredential%3DyqXZFxpELC7KVnFOS48ylbz2pIh7yKj8%26expires%3D1753973999%26allow_ip%3D%26allow_referer%3D%26signature%3DYsktP0qSx5WgEoXxGHcFkPcDlZg%253D)
https://www.acmicpc.net/problem/14888DFS로 구현하였다.연산자 우선 순위를 무시하는 것, 나눗셈은 정수 나눗셈으로 몫만 취한다 등등 조건 덕분에 풀기 쉬운 문제이다.#include #include #include #include using namespace std;vector list;int maxValue = INT_MIN;int minValue = INT_MAX;int N;void calculate(int add, int sub, int mul, int div, int depth, int value){ if (depth == N) { maxValue = max(maxValue, value); minValue = min(minValue, value); return; ..
![[백준 C++] 1260 : DFS와 BFS](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdna%2FbiCHCT%2FbtsKCvOT9PK%2FAAAAAAAAAAAAAAAAAAAAACQyxFqg9WD_EefJMMVVL7Ris04c8UtCrc6ZcH3HiPqu%2Fimg.png%3Fcredential%3DyqXZFxpELC7KVnFOS48ylbz2pIh7yKj8%26expires%3D1753973999%26allow_ip%3D%26allow_referer%3D%26signature%3DXsqKlwJ2Rg%252FjcL3SMb1UBPQDU8s%253D)
https://www.acmicpc.net/problem/1260주어진 입력에 맞춰 DFS와 BFS로 방문하는 순서 그대로 출력하면 되는 문제이다.#include #include #include #include using namespace std;bool visited[1001];vector graph[1001];void solution_dfs(int start){ visited[start] = true; cout que; que.push(start); visited[start] = true; while (!que.empty()) { int corrent = que.front(); que.pop(); cout > N >> M >> V; for (int i = 0; i > v1 >> v2; gra..