κΉμ΄ μ°μ νμ(Depth First Search)κ³Ό λλΉ μ°μ νμ(Breadth First Search)
κΉμ΄ μ°μ νμμ ν λ°©ν₯μΌλ‘ κ³μ κ°λ€κ° λ μ΄μ κ° μ μμΌλ©΄ λΆλͺ¨ λ Έλλ‘ λμμ λ€λ₯Έ λ°©ν₯μΌλ‘ νμνλ€.
- μ£Όλ‘ Stackμ΄λ μ¬κ· ν¨μλ₯Ό μ΄μ©ν΄ ꡬννλ€.
- λͺ¨λ λ Έλλ₯Ό λ°©λ¬Ένκ±°λ κ°μ€μΉ, μ΄λ κ³Όμ μ μ μ½μ΄ μμ κ²½μ°μ μ¬μ©νλ©΄ μ’λ€.
- μκΈ° μμ μ νΈμΆνλ μν μκ³ λ¦¬μ¦μ ννλ₯Ό κ°κ³ μλ€.
- BFSλ³΄λ€ μ’ λ κ°λ¨νλ λ¨μ κ²μ μλ μ체λ BFSλ³΄λ€ λ리λ€.
λλΉ μ°μ νμμ νμ¬ λ Έλμμ κ°μ₯ κ°κΉμ΄ λ Έλλ₯Ό λ¨Όμ λ°©λ¬Έν΄ νμνλ€.
- μ£Όλ‘ Queueλ₯Ό μ΄μ©ν΄ ꡬννλ€.
- λ λ Έλ μ¬μ΄μ μ΅λ¨ κ²½λ‘ νΉμ μμμ κ²½λ‘λ₯Ό μ°Ύλ κ²½μ°μ μ¬μ©νλ©΄ μ’λ€.
κΈ°λ³Έ μ리
https://www.acmicpc.net/problem/1260
1260λ²: DFSμ BFS
첫째 μ€μ μ μ μ κ°μ N(1 ≤ N ≤ 1,000), κ°μ μ κ°μ M(1 ≤ M ≤ 10,000), νμμ μμν μ μ μ λ²νΈ Vκ° μ£Όμ΄μ§λ€. λ€μ Mκ°μ μ€μλ κ°μ μ΄ μ°κ²°νλ λ μ μ μ λ²νΈκ° μ£Όμ΄μ§λ€. μ΄λ€ λ μ μ μ¬μ΄μ μ¬λ¬ κ°μ κ°μ μ΄ μμ μ μλ€. μ λ ₯μΌλ‘ μ£Όμ΄μ§λ κ°μ μ μλ°©ν₯μ΄λ€.
www.acmicpc.net
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
vector<int> graph[1001]; // μ°κ²° κ·Έλν
bool checked[1001]; // λ°©λ¬Έ 체ν¬
void DFS(int start)
{
checked[start] = true;
cout << start << " ";
for (int i = 0; i < graph[start].size(); i++)
if (!checked[graph[start][i]])
DFS(graph[start][i]);
}
void BFS(int start)
{
queue<int> q;
q.push(start);
checked[start] = true;
while (!q.empty()) {
int val = q.front();
cout << val << " ";
q.pop();
for (int i = 0; i < graph[val].size(); i++)
if (!checked[graph[val][i]]) {
q.push(graph[val][i]);
checked[graph[val][i]] = true;
}
}
}
int main()
{
int N, M, V;
cin >> N >> M >> V;
for (int i = 0; i < M; i++) {
int a, b;
cin >> a >> b;
graph[a].push_back(b);
graph[b].push_back(a);
}
for (int i = 1; i <= N; i++)
sort(graph[i].begin(), graph[i].end());
fill_n(checked, N + 1, false);
DFS(V);
cout << "\n";
fill_n(checked, N + 1, false);
BFS(V);
}
λ¬Έμ : DFS
https://www.acmicpc.net/problem/2667
2667λ²: λ¨μ§λ²νΈλΆμ΄κΈ°
<κ·Έλ¦Ό 1>κ³Ό κ°μ΄ μ μ¬κ°ν λͺ¨μμ μ§λκ° μλ€. 1μ μ§μ΄ μλ κ³³μ, 0μ μ§μ΄ μλ κ³³μ λνλΈλ€. μ² μλ μ΄ μ§λλ₯Ό κ°μ§κ³ μ°κ²°λ μ§λ€μ λͺ¨μμΈ λ¨μ§λ₯Ό μ μνκ³ , λ¨μ§μ λ²νΈλ₯Ό λΆμ΄λ € νλ€. μ¬κΈ°μ μ°κ²°λμλ€λ κ²μ μ΄λ€ μ§μ΄ μ’μ°, νΉμ μλμλ‘ λ€λ₯Έ μ§μ΄ μλ κ²½μ°λ₯Ό λ§νλ€. λκ°μ μμ μ§μ΄ μλ κ²½μ°λ μ°κ²°λ κ²μ΄ μλλ€. <κ·Έλ¦Ό 2>λ <κ·Έλ¦Ό 1>μ λ¨μ§λ³λ‘ λ²νΈλ₯Ό λΆμΈ κ²μ΄λ€. μ§λλ₯Ό μ λ ₯νμ¬ λ¨μ§μλ₯Ό μΆλ ₯νκ³ , κ° λ¨μ§μ μνλ μ§μ μ
www.acmicpc.net
#include <iostream>
#include <set>
using namespace std;
int n;
bool map[25][25], checked[25][25];
int DFS(int y, int x)
{
if (0 > y || y >= n || 0 > x || x >= n)
return 0;
if (checked[y][x] || !map[y][x])
return 0;
checked[y][x] = true;
return 1 + DFS(y, x + 1) + DFS(y, x - 1) + DFS(y + 1, x) + DFS(y - 1, x);
}
int main()
{
cin >> n;
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
scanf("%1d", &map[i][j]);
multiset<int> s;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (map[i][j] && !checked[i][j])
s.insert(DFS(i, j));
cout << s.size() << "\n";
for (int i : s)
cout << i << "\n";
}
https://www.acmicpc.net/problem/2606
2606λ²: λ°μ΄λ¬μ€
첫째 μ€μλ μ»΄ν¨ν°μ μκ° μ£Όμ΄μ§λ€. μ»΄ν¨ν°μ μλ 100 μ΄νμ΄κ³ κ° μ»΄ν¨ν°μλ 1λ² λΆν° μ°¨λ‘λλ‘ λ²νΈκ° 맀겨μ§λ€. λμ§Έ μ€μλ λ€νΈμν¬ μμμ μ§μ μ°κ²°λμ΄ μλ μ»΄ν¨ν° μμ μκ° μ£Όμ΄οΏ½οΏ½
www.acmicpc.net
#include <iostream>
#include <vector>
using namespace std;
int ans = -1;
vector<int> graph[101];
bool checked[101];
void DFS(int start) {
ans++;
checked[start] = true;
for (int i = 0; i < graph[start].size(); i++)
if (!checked[graph[start][i]])
DFS(graph[start][i]);
}
int main()
{
int n, m;
cin >> n >> m;
for (int i = 0, a, b; i < m; i++) {
cin >> a >> b;
graph[a].push_back(b);
graph[b].push_back(a);
}
DFS(1);
cout << ans;
}
https://www.acmicpc.net/problem/2636
2636λ²: μΉμ¦
μλ <κ·Έλ¦Ό 1>κ³Ό κ°μ΄ μ μ¬κ°ν μΉΈλ€λ‘ μ΄λ£¨μ΄μ§ μ¬κ°ν λͺ¨μμ νμ΄ μκ³ , κ·Έ μμ μμ μΉμ¦(νμμΌλ‘ νμλ λΆλΆ)κ° λμ¬ μλ€. νμ κ°μ₯μ리(<κ·Έλ¦Ό 1>μμ λ€λͺ¨ μΉΈμ XμΉ λΆλΆ)μλ μΉμ¦κ° λ
www.acmicpc.net
#include <iostream>
using namespace std;
int N, M;
int time, piece, cnt;
bool map[100][100], check[100][100];
int dir[4][2]{ {1,0},{-1,0},{0,1},{0,-1} };
void DFS(int x, int y)
{
check[y][x] = true;
for (int i = 0; i < 4; i++) {
int nx = x + dir[i][0], ny = y + dir[i][1];
if (nx < 0 || nx >= M || ny < 0 || ny >= N)
continue;
if (check[ny][nx])
continue;
if (map[ny][nx]) {
cnt--;
map[ny][nx] = false;
check[ny][nx] = true;
}
else
DFS(nx, ny);
}
}
int main()
{
cin >> N >> M;
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++) {
cin >> map[i][j];
if (map[i][j])
cnt++;
}
while (cnt) {
time++;
piece = cnt;
DFS(0, 0);
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++)
check[i][j] = false;
}
cout << time << "\n" << piece;
}
https://www.acmicpc.net/problem/14888
14888λ²: μ°μ°μ λΌμλ£κΈ°
첫째 μ€μ μμ κ°μ N(2 ≤ N ≤ 11)κ° μ£Όμ΄μ§λ€. λμ§Έ μ€μλ A1, A2, ..., ANμ΄ μ£Όμ΄μ§λ€. (1 ≤ Ai ≤ 100) μ μ§Έ μ€μλ ν©μ΄ N-1μΈ 4κ°μ μ μκ° μ£Όμ΄μ§λλ°, μ°¨λ‘λλ‘ λ§μ (+)μ κ°μ, λΊμ (-)μ κ°μ, οΏ½οΏ½
www.acmicpc.net
#include <iostream>
using namespace std;
int n; int num[11]; int oper[4];
int mmin = 1000000000, mmax = -1000000000;
void DFS(int idx, int result, int hap, int cha, int gop, int na)
{
if (hap < 0 || cha < 0 || gop < 0 || na < 0) return;
if (idx == n) {
if (result > mmax) mmax = result;
if (result < mmin) mmin = result;
return;
}
DFS(idx + 1, result + num[idx], hap - 1, cha, gop, na); // +μ°μ°
DFS(idx + 1, result - num[idx], hap, cha - 1, gop, na); // -μ°μ°
DFS(idx + 1, result * num[idx], hap, cha, gop - 1, na); // *μ°μ°
DFS(idx + 1, result / num[idx], hap, cha, gop, na - 1); // /μ°μ°
}
int main()
{
cin >> n;
for (int i = 0; i < n; ++i)
cin >> num[i];
for (int i = 0; i < 4; ++i)
cin >> oper[i];
DFS(1, num[0], oper[0], oper[1], oper[2], oper[3]);
cout << mmax << "\n" << mmin;
}
https://www.acmicpc.net/problem/9466
9466λ²: ν νλ‘μ νΈ
λ¬Έμ μ΄λ² κ°μνκΈ°μ 'λ¬Έμ ν΄κ²°' κ°μλ₯Ό μ μ²ν νμλ€μ ν νλ‘μ νΈλ₯Ό μνν΄μΌ νλ€. νλ‘μ νΈ νμ μμλ μ νμ΄ μλ€. μ¬μ§μ΄ λͺ¨λ νμλ€μ΄ λμΌν νμ νμμΈ κ²½μ°μ κ°μ΄ ν νλ§
www.acmicpc.net
#include <iostream>
#include <vector>
using namespace std;
int team;
vector<int> v;
vector<bool> check, visited; // λ°©λ¬Έ, μ¬λ°©λ¬Έ
void DFS(int start, int index)
{
if (visited[index]) {
team++;
int temp = v[index];
while (temp != index) {
team++;
temp = v[temp];
}
}
else {
visited[index] = true;
if (!check[v[index]]) {
DFS(start, v[index]);
check[index] = true;
}
}
}
int main()
{
int T;
cin >> T;
while (T--) {
int N;
cin >> N;
team = 0;
v = vector<int>(N + 1, 0);
check = vector<bool>(N + 1, false);
visited = vector<bool>(N + 1, false);
for (int i = 1; i <= N; i++)
cin >> v[i];
for (int i = 1; i <= N; i++) {
if (!check[i]) {
visited = vector<bool>(N + 1, false);
DFS(i, i);
}
}
cout << N - team << "\n";
}
}
λ¬Έμ : BFS
https://www.acmicpc.net/problem/2178
2178λ²: λ―Έλ‘ νμ
첫째 μ€μ λ μ μ N, M(2 ≤ N, M ≤ 100)μ΄ μ£Όμ΄μ§λ€. λ€μ Nκ°μ μ€μλ Mκ°μ μ μλ‘ λ―Έλ‘κ° μ£Όμ΄μ§λ€. κ°κ°μ μλ€μ λΆμ΄μ μ λ ₯μΌλ‘ μ£Όμ΄μ§λ€.
www.acmicpc.net
#include <iostream>
#include <queue>
using namespace std;
bool map[100][100], ck[100][100];
int dir[4][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
int main()
{
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
scanf("%1d", &map[i][j]);
int ans = 0;
queue<pair<int, int>> q;
q.push(make_pair(0, 0));
ck[0][0] = true;
while (!q.empty()) {
ans++;
int s = q.size();
while (s--) {
int tx = q.front().second, ty = q.front().first;
q.pop();
if (tx == m - 1 && ty == n - 1) {
cout << ans;
return 0;
}
for (int i = 0; i < 4; i++) {
int nx = tx + dir[i][0], ny = ty + dir[i][1];
if (nx < 0 || nx >= m || ny < 0 || ny >= n)
continue;
if (map[ny][nx] && !ck[ny][nx]) {
q.push({ ny, nx });
ck[ny][nx] = true;
}
}
}
}
}
https://www.acmicpc.net/problem/2206
2206λ²: λ²½ λΆμκ³ μ΄λνκΈ°
N×Mμ νλ ¬λ‘ ννλλ λ§΅μ΄ μλ€. λ§΅μμ 0μ μ΄λν μ μλ κ³³μ λνλ΄κ³ , 1μ μ΄λν μ μλ λ²½μ΄ μλ κ³³μ λνλΈλ€. λΉμ μ (1, 1)μμ (N, M)μ μμΉκΉμ§ μ΄λνλ € νλλ°, μ΄λ μ΅λ¨ κ²½λ‘λ‘ μ΄λνλ € νλ€. μ΅λ¨κ²½λ‘λ λ§΅μμ κ°μ₯ μ μ κ°μμ μΉΈμ μ§λλ κ²½λ‘λ₯Ό λ§νλλ°, μ΄λ μμνλ μΉΈκ³Ό λλλ μΉΈλ ν¬ν¨ν΄μ μΌλ€. λ§μ½μ μ΄λνλ λμ€μ ν κ°μ λ²½μ λΆμκ³ μ΄λνλ κ²μ΄ μ’ λ κ²½λ‘κ° μ§§μμ§λ€λ©΄, λ²½μ ν κ° κΉμ§ λΆμκ³ μ΄λ
www.acmicpc.net
#include <iostream>
#include <queue>
using namespace std;
bool map[1000][1000], checked[1000][1000][2];
int main()
{
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
scanf("%1d", &map[i][j]);
int answer = 0;
queue<pair<int, pair<int, int>>> q;
q.push({ 0, { 0, 0 } });
checked[0][0][0] = true;
while (!q.empty()) {
answer++;
int size = q.size();
while (size--) {
int tx = q.front().second.second;
int ty = q.front().second.first;
int crash = q.front().first;
q.pop();
if (tx == m - 1 && ty == n - 1) {
cout << answer;
return 0;
}
int dir[4][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
for (int i = 0; i < 4; i++) {
int nx = tx + dir[i][0], ny = ty + dir[i][1];
if (nx < 0 || nx >= m || ny < 0 || ny >= n)
continue;
if (checked[ny][nx][crash])
continue;
if (map[ny][nx]) {
if (crash)
continue;
q.push({ 1,{ny, nx} });
}
else
q.push({ crash,{ny, nx} });
checked[ny][nx][crash] = true;
}
}
}
cout << -1;
}
https://www.acmicpc.net/problem/7569
7569λ²: ν λ§ν
첫 μ€μλ μμμ ν¬κΈ°λ₯Ό λνλ΄λ λ μ μ M,Nκ³Ό μμμ¬λ €μ§λ μμμ μλ₯Ό λνλ΄λ Hκ° μ£Όμ΄μ§λ€. Mμ μμμ κ°λ‘ μΉΈμ μ, Nμ μμμ μΈλ‘ μΉΈμ μλ₯Ό λνλΈλ€. λ¨, 2 ≤ M ≤ 100, 2 ≤ N ≤ 100,
www.acmicpc.net
#include <iostream>
#include <queue>
using namespace std;
int map[100][100][100]{};
bool check[100][100][100]{};
int main()
{
int M, N, H;
cin >> M >> N >> H;
queue<pair<int, pair<int, int>>> q;
for (int i = 0; i < H; ++i)
for (int j = 0; j < N; ++j)
for (int k = 0; k < M; ++k) {
cin >> map[i][j][k];
if (map[i][j][k] == 1) {
q.push({ i,{j,k} });
check[i][j][k] = true;
}
}
int answer = -1;
while (!q.empty()) {
++answer;
int size = q.size();
while (size--) {
int tz = q.front().first;
int ty = q.front().second.first;
int tx = q.front().second.second;
q.pop();
int dir[9][3] = { {-1, 0, 0}, {1, 0, 0}, {0, -1, 0}, {0, 1, 0}, {0, 0, -1}, {0, 0, 1} };
for (int i = 0; i < 9; i++) {
int nx = tx + dir[i][0], ny = ty + dir[i][1], nz = tz + dir[i][2];
if (nx < 0 || nx >= M || ny < 0 || ny >= N || nz < 0 || nz >= H)
continue;
if (check[nz][ny][nx] || map[nz][ny][nx])
continue;
q.push({ nz,{ny, nx} });
check[nz][ny][nx] = true;
map[nz][ny][nx] = 1;
}
}
}
for (int k = 0; k < H; k++)
for (int j = 0; j < N; j++)
for (int i = 0; i < M; i++)
if (!map[k][j][i]) {
cout << -1;
return 0;
}
cout << answer;
}
https://www.acmicpc.net/problem/1697
1697λ²: μ¨λ°κΌμ§
λ¬Έμ μλΉμ΄λ λμκ³Ό μ¨λ°κΌμ§μ νκ³ μλ€. μλΉμ΄λ νμ¬ μ N(0 ≤ N ≤ 100,000)μ μκ³ , λμμ μ K(0 ≤ K ≤ 100,000)μ μλ€. μλΉμ΄λ κ±·κ±°λ μκ°μ΄λμ ν μ μλ€. λ§μ½, μλΉμ΄μ μμΉκ° XμΌ λ κ±·λλ€λ©΄ 1μ΄ νμ X-1 λλ X+1λ‘ μ΄λνκ² λλ€. μκ°μ΄λμ νλ κ²½μ°μλ 1μ΄ νμ 2*Xμ μμΉλ‘ μ΄λνκ² λλ€. μλΉμ΄μ λμμ μμΉκ° μ£Όμ΄μ‘μ λ, μλΉμ΄κ° λμμ μ°Ύμ μ μλ κ°μ₯ λΉ λ₯Έ μκ°μ΄ λͺ μ΄ νμΈμ§
www.acmicpc.net
#include <iostream>
#include <queue>
using namespace std;
bool checked[100001];
int main(void)
{
int n, k;
cin >> n >> k;
int ans = -1;
queue<int> q;
q.push(n);
checked[n] = true;
while (!q.empty()) {
ans++;
int s = q.size();
while (s--) {
int x = q.front();
q.pop();
if (x == k) {
cout << ans;
return 0;
}
int dx[3] = { x + 1, x - 1, x * 2 };
for (int i = 0; i < 3; i++)
if (0 <= dx[i] && dx[i] <= 100000 && !checked[dx[i]]) {
q.push(dx[i]);
checked[dx[i]] = true;
}
}
}
}
https://www.acmicpc.net/problem/1726
1726λ²: λ‘λ΄
λ§μ 곡μ₯μμ λ‘λ΄μ΄ μ΄μ©λκ³ μλ€. μ°λ¦¬ μλ 곡μ₯μ λ‘λ΄μ λ°λΌλ³΄λ λ°©ν₯μΌλ‘ κΆ€λλ₯Ό λ°λΌ μμ§μ΄λ©°, μμ§μ΄λ λ°©ν₯μ λ, μ, λ¨, λΆ κ°μ΄λ° νλμ΄λ€. λ‘λ΄μ μ΄λμ μ μ΄νλ λͺ λ Ήμ΄λ
www.acmicpc.net
#include <iostream>
#include <queue>
using namespace std;
int M, N;
int startX, startY, startDir;
int endX, endY, endDir;
int map[101][101], check[101][101][5];
int dir[4][2] = { {1, 0}, {-1, 0}, {0, 1}, {0, -1} };
void Input()
{
cin >> M >> N;
for (int i = 1; i <= M; ++i)
for (int j = 1; j <= N; ++j)
cin >> map[i][j];
cin >> startY >> startX >> startDir >> endY >> endX >> endDir;
}
int Solution()
{
int answer = 10e9;
queue<pair<pair<int, int>, int>> q;
q.push({ {startY, startX}, startDir });
while (!q.empty()) {
int ty = q.front().first.first, tx = q.front().first.second, td = q.front().second;
q.pop();
if (ty == endY && tx == endX && td == endDir)
{
answer = min(answer, check[endY][endX][endDir]);
continue;
}
for (int k = 1; k <= 3; ++k)
{
int ny = ty + dir[td - 1][0] * k, nx = tx + dir[td - 1][1] * k;
if (ny <= 0 || ny > M || nx <= 0 || nx > N || map[ny][nx])
break;
if (check[ny][nx][td] == 0 || check[ny][nx][td] > check[ty][tx][td] + 1)
{
check[ny][nx][td] = check[ty][tx][td] + 1;
q.push({ {ny,nx},td });
}
}
for (int nd = 1; nd <= 4; ++nd)
{
int rot = 1;
if (nd == td) rot = 0;
else if (nd == 1 && td == 2) rot = 2;
else if (nd == 2 && td == 1) rot = 2;
else if (nd == 3 && td == 4) rot = 2;
else if (nd == 4 && td == 3) rot = 2;
if (check[ty][tx][nd] == 0 || check[ty][tx][nd] > check[ty][tx][td] + rot)
{
check[ty][tx][nd] = check[ty][tx][td] + rot;
q.push({ {ty,tx},nd });
}
}
}
return answer;
}
int main()
{
cin.tie(NULL);
cout.tie(NULL);
ios::sync_with_stdio(false);
Input();
cout << Solution();
}