![[백준 C++] 1012 : 유기농 배추](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FDLDMk%2FbtsKMj7Yoda%2FtlzXdaC4qxSvl5RwKQCHkK%2Fimg.png)
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%2Fdn%2FPoF2p%2FbtsKL1MsVxx%2FSpO3qeLCB46kqsE4Yob2v1%2Fimg.png)
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%2Fdn%2FcXwVjl%2FbtsKDBIZHq5%2FLwT7ohEHKGzPhKOrbIJQ7K%2Fimg.png)
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%2Fdn%2FbE5vyw%2FbtsKDBOxcvc%2FS1G91V3RyFDkhviUQf0jXk%2Fimg.png)
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%2Fdn%2FssOzC%2FbtsKCUgK79l%2FT8MHwed8krHCx0vomczYi1%2Fimg.png)
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%2Fdn%2FW1tYG%2FbtsKBlfdybu%2F4KxAFkVUZTl6qAaLjcmLek%2Fimg.png)
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%2Fdn%2FbiCHCT%2FbtsKCvOT9PK%2FlZfdxnKuhkoqf9ON5UznE0%2Fimg.png)
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..