๐ ๊ณต๋ถํ๋ ์ง์ง์ํ์นด๋ ์ฒ์์ด์ง?
[์ด๊ฒ์ด ์ฝ๋ฉ ํ ์คํธ๋ค with Python]_20_DFS & BFS ๋ฌธ์ ํ์ด ๋ณธ๋ฌธ
[์ด๊ฒ์ด ์ฝ๋ฉ ํ ์คํธ๋ค with Python]_20_DFS & BFS ๋ฌธ์ ํ์ด
์ง์ง์ํ์นด 2022. 2. 2. 01:41220202 ์์ฑ
<๋ณธ ๋ธ๋ก๊ทธ๋ ใ์ด๊ฒ์ด ์ทจ์ ์ ์ํ ์ฝ๋ฉ ํ ์คํธ๋คใ ์ youtube๋ฅผ ์ฐธ๊ณ ํด์ ๊ณต๋ถํ๋ฉฐ ์์ฑํ์์ต๋๋ค>
https://www.youtube.com/watch?v=e7_H8SLZlHY&list=PLVsNizTWUw7H9_of5YCB0FmsSc-K44y81&index=20
< ๋ฌธ์ 1 > ์๋ฃ์ ์ผ๋ ค ๋จน๊ธฐ
: N X M ํฌ๊ธฐ์ ์ผ์ ํ
: ๊ตฌ๋ฉ์ด ๋ซ๋ฆฐ ๋ถ๋ถ์ 0, ์นธ๋ง์ด ์กด์ฌ ๋ถ๋ถ์ 1
: ๊ตฌ๋ฉ ๋ซ๋ฆฐ ๋ถ๋ถ๋ผ๋ฆฌ ์, ํ, ์ข, ์ฐ๋ก ๋ถ์ด์๋ ๊ฒฝ์ฐ ์๋ก ์ฐ๊ฒฐ๋๋ ๊ฒ์ผ๋ก ๊ฐ์ฃผ!
: ์ผ์ ํ์ ๋ชจ์์ด ์ฃผ์ด์ก์ ๋ ์์ฑ๋๋ ์ด ์์ด์คํฌ๋ฆผ์ ๊ฐ์ ๊ตฌํ๊ธฐ
: ๋ฐฉ๋ฌธ ์ฒ๋ฆฌ ๋ ๋ถ๋ถ๋ง ์ฐ๊ฒฐํ๊ธฐ!
1) ํน์ ์ง์ ์ ์, ํ, ์ข, ์ฐ ์ดํด๋ณด๊ณ ์ฃผ๋ณ ์ง์ ์ด 0 ์ด๋ฉด ํด๋น ์ง์ ๋ฐฉ๋ฌธ
2) ๋ฐฉ๋ฌธ ์งํ ๊ณผ์ ๋ฐ๋ณตํ๋ฉด, ์ฐ๊ฒฐ๋ ๋ชจ๋ ์ง์ ๋ฐฉ๋ฌธ ๊ฐ๋ฅ
3) 1~2 ๊ณผ์ ๋ฐ๋ณตํ์ฌ, ๋ฐฉ๋ฌธํ์ง ์์ ์ง์ ์ ์ ์นด์ดํธ!
- python
# DFS ๋ก ํน์ ๋
ธ๋ ๋ฐฉ๋ฌธ, ์ฐ๊ฒฐ๋ ๋ชจ๋ ๋
ธ๋๋ค ๋ฐฉ๋ฌธ
def dfs(x, y) :
# ์ฃผ์ด์ง ๋ฒ์ ๋ฒ์ด๋๋ฉด ์ข
๋ฃ
if x <= -1 or x >= n or y <= -1 or y >= m :
return False
# ํ์ฌ ๋
ธ๋ ์์ง ๋ฐฉ๋ฌธ ์ํ๋ค๋ฉด
if graph[x][y] == 0 :
# ํด๋น ๋
ธ๋ ๋ฐฉ๋ฌธ ์ฒ๋ฆฌ
graph[x][y] = 1
# ์, ํ, ์ข, ์ฐ ์์น๋ค ๋ชจ๋ ์ฌ๊ท์ ํธ์ถ
dfs(x -1, y)
dfs(x, y-1)
dfs(x+1, y)
dfs(x, y+1)
return True
return False
n, m = map(int, input().split())
graph = []
for i in range(n) :
graph.append(list(map(int, input())))
# ๋ชจ๋ ๋
ธ๋์ ๋ํด ์๋ฃ์ ์ฑ์ฐ๊ธฐ
result = 0
for i in range(n) :
for j in range(m) :
# ํ์ฌ ์์น์์ DFS ์ํ
if dfs(i, j) == True :
result += 1
print(result)
# result
# 5 4
# 11100
# 00011
# 11100
# 00000
# 11111
3
- c++
#include <bits/stdc++.h>
using namespace std;
int n, m;
int graph[1000][1000];
// DFS๋ก ํน์ ๋
ธ๋๋ฅผ ๋ฐฉ๋ฌธํ๊ณ ์ฐ๊ฒฐ๋ ๋ชจ๋ ๋
ธ๋๋ค๋ ๋ฐฉ๋ฌธ
bool dfs(int x, int y) {
// ์ฃผ์ด์ง ๋ฒ์๋ฅผ ๋ฒ์ด๋๋ ๊ฒฝ์ฐ์๋ ์ฆ์ ์ข
๋ฃ
if (x <= -1 || x >=n || y <= -1 || y >= m) {
return false;
}
// ํ์ฌ ๋
ธ๋๋ฅผ ์์ง ๋ฐฉ๋ฌธํ์ง ์์๋ค๋ฉด
if (graph[x][y] == 0) {
// ํด๋น ๋
ธ๋ ๋ฐฉ๋ฌธ ์ฒ๋ฆฌ
graph[x][y] = 1;
// ์, ํ, ์ข, ์ฐ์ ์์น๋ค๋ ๋ชจ๋ ์ฌ๊ท์ ์ผ๋ก ํธ์ถ
dfs(x - 1, y);
dfs(x, y - 1);
dfs(x + 1, y);
dfs(x, y + 1);
return true;
}
return false;
}
int main() {
// N, M์ ๊ณต๋ฐฑ์ ๊ธฐ์ค์ผ๋ก ๊ตฌ๋ถํ์ฌ ์
๋ ฅ ๋ฐ๊ธฐ
cin >> n >> m;
// 2์ฐจ์ ๋ฆฌ์คํธ์ ๋งต ์ ๋ณด ์
๋ ฅ ๋ฐ๊ธฐ
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
scanf("%1d", &graph[i][j]);
}
}
// ๋ชจ๋ ๋
ธ๋(์์น)์ ๋ํ์ฌ ์๋ฃ์ ์ฑ์ฐ๊ธฐ
int result = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
// ํ์ฌ ์์น์์ DFS ์ํ
if (dfs(i, j)) {
result += 1;
}
}
}
cout << result << '\n'; // ์ ๋ต ์ถ๋ ฅ
}
- java
import java.util.*;
public class Main {
public static int n, m;
public static int[][] graph = new int[1000][1000];
// DFS๋ก ํน์ ๋
ธ๋๋ฅผ ๋ฐฉ๋ฌธํ๊ณ ์ฐ๊ฒฐ๋ ๋ชจ๋ ๋
ธ๋๋ค๋ ๋ฐฉ๋ฌธ
public static boolean dfs(int x, int y) {
// ์ฃผ์ด์ง ๋ฒ์๋ฅผ ๋ฒ์ด๋๋ ๊ฒฝ์ฐ์๋ ์ฆ์ ์ข
๋ฃ
if (x <= -1 || x >=n || y <= -1 || y >= m) {
return false;
}
// ํ์ฌ ๋
ธ๋๋ฅผ ์์ง ๋ฐฉ๋ฌธํ์ง ์์๋ค๋ฉด
if (graph[x][y] == 0) {
// ํด๋น ๋
ธ๋ ๋ฐฉ๋ฌธ ์ฒ๋ฆฌ
graph[x][y] = 1;
// ์, ํ, ์ข, ์ฐ์ ์์น๋ค๋ ๋ชจ๋ ์ฌ๊ท์ ์ผ๋ก ํธ์ถ
dfs(x - 1, y);
dfs(x, y - 1);
dfs(x + 1, y);
dfs(x, y + 1);
return true;
}
return false;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// N, M์ ๊ณต๋ฐฑ์ ๊ธฐ์ค์ผ๋ก ๊ตฌ๋ถํ์ฌ ์
๋ ฅ ๋ฐ๊ธฐ
n = sc.nextInt();
m = sc.nextInt();
sc.nextLine(); // ๋ฒํผ ์ง์ฐ๊ธฐ
// 2์ฐจ์ ๋ฆฌ์คํธ์ ๋งต ์ ๋ณด ์
๋ ฅ ๋ฐ๊ธฐ
for (int i = 0; i < n; i++) {
String str = sc.nextLine();
for (int j = 0; j < m; j++) {
graph[i][j] = str.charAt(j) - '0';
}
}
// ๋ชจ๋ ๋
ธ๋(์์น)์ ๋ํ์ฌ ์๋ฃ์ ์ฑ์ฐ๊ธฐ
int result = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
// ํ์ฌ ์์น์์ DFS ์ํ
if (dfs(i, j)) {
result += 1;
}
}
}
System.out.println(result); // ์ ๋ต ์ถ๋ ฅ
}
}
< ๋ฌธ์ 2 > ๋ฏธ๋ก ํ์ถ
: N X M ํฌ๊ธฐ์ ์ง์ฌ๊ฐํ ํํ ๋ฏธ๋ก์ ๊ฐํ
: ์ฌ๋ฌ ๋ง๋ฆฌ ๊ดด๋ฌผ ํผํด์ผ๋จ
: ๋๋ ์์น (1, 1), ๋ฏธ๋ก์ ์ถ๊ตฌ๋ (N, M)
: ๊ดด๋ฌผ์ด ์๋ ๊ณณ์ 0, ์๋ ๊ณณ์ 1
: ํ์ถํ๊ธฐ ์ํด ์์ง์ฌ์ผ ํ๋ ์ต์์ ์นธ ๊ฐ์ ๊ตฌํ๋ผ ( ์์ ์นธ, ๋ง์ง๋ง ์นธ ๋ชจ๋ ํฌํจ )
: BFS ๋ก ์์ ์ง์ ์์ ๊ฐ์ฅ ๊ฐ๊น์ด ๋ ธ๋๋ถํฐ ์ฐจ๋ก๋๋ก ๊ทธ๋ํ์ ๋ชจ๋ ๋ ธ๋ ํ์ !
: ๋ชจ๋ ๋ ธ๋์ ์ต๋จ ๊ฑฐ๋ฆฌ ๊ฐ์ ๊ธฐ๋กํ์ฌ ํด๊ฒฐ !
+) ๋์ ์๋ฆฌ
- ์ฒ์์๋ (1, 1) ์์ ์์
- ์, ํ, ์ข, ์ฐ ํ์ ํ๋ฉด์ ๋ฐ๋ก ์ ๋ ธ๋ (1, 2) ๋ฐฉ๋ฌธํ๊ณ ์๋ก์ด ๋ฐฉ๋ฌธ ์์น๋ฅผ 2๋ก ๋ฐ๊ฟ
- ์ต๋จ ๊ฒฝ๋ก์ ๊ฐ๋ค์ด 1์ฉ ์ฆ๊ฐํ ํํ๋ก ๋ณ๊ฒฝ
- python
# BFS FH
def bfs(x, y) :
queue = deque()
queue.append((x, y))
# ํ ๋น ๋๊น์ง ๋ฐ๋ณต
while queue :
x, y = queue.popleft()
# ํ์ฌ ์์น์์ 4๊ฐ์ง ๋ฐฉํฅ์ผ๋ก ์์น ํ์ธ
for i in range(4) :
nx = x + dx[i]
ny = y + dy[i]
# ๋ฏธ๋ก ์ฐพ๊ธฐ ๊ณต๊ฐ ๋ฒ์ด๋๋ฉด ๋ฌด์
if nx < 0 or nx >= n or ny < 0 or ny >= m :
continue
# ๋ฒฝ์ด๋ฉด ๋ฌด์
if graph[nx][ny] == 0:
continue
# ํด๋น ๋
ธ๋ ์ฒจ ๋ฐฉ๋ฌธํ๋ฉด ์ต๋จ ๊ฑฐ๋ฆฌ ๊ธฐ๋ก
if graph[nx][ny] == 1:
graph[nx][ny] = graph[x][y] + 1
queue.append((nx, ny))
# ๊ฐ์ฅ ์ค๋ฅธ์ชฝ ์๋ ๊น์ง์ ์ต๋จ ๊ฑฐ๋ฆฌ ๋ฐํ
return graph[n-1][m-1]
from collections import deque
n, m = map(int, input().split())
graph = []
for i in range(n) :
graph.append(list(map(int, input())))
dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]
print(bfs(0, 0))
# result
# 5 6
# 111000
# 110000
# 111111
# 000111
# 011000
# 0
- c++
#include <bits/stdc++.h>
using namespace std;
int n, m;
int graph[201][201];
// ์ด๋ํ ๋ค ๊ฐ์ง ๋ฐฉํฅ ์ ์ (์, ํ, ์ข, ์ฐ)
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};
int bfs(int x, int y) {
// ํ(Queue) ๊ตฌํ์ ์ํด queue ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ฌ์ฉ
queue<pair<int, int> > q;
q.push({x, y});
// ํ๊ฐ ๋น ๋๊น์ง ๋ฐ๋ณตํ๊ธฐ
while(!q.empty()) {
int x = q.front().first;
int y = q.front().second;
q.pop();
// ํ์ฌ ์์น์์ 4๊ฐ์ง ๋ฐฉํฅ์ผ๋ก์ ์์น ํ์ธ
for (int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
// ๋ฏธ๋ก ์ฐพ๊ธฐ ๊ณต๊ฐ์ ๋ฒ์ด๋ ๊ฒฝ์ฐ ๋ฌด์
if (nx < 0 || nx >= n || ny < 0 || ny >= m) continue;
// ๋ฒฝ์ธ ๊ฒฝ์ฐ ๋ฌด์
if (graph[nx][ny] == 0) continue;
// ํด๋น ๋
ธ๋๋ฅผ ์ฒ์ ๋ฐฉ๋ฌธํ๋ ๊ฒฝ์ฐ์๋ง ์ต๋จ ๊ฑฐ๋ฆฌ ๊ธฐ๋ก
if (graph[nx][ny] == 1) {
graph[nx][ny] = graph[x][y] + 1;
q.push({nx, ny});
}
}
}
// ๊ฐ์ฅ ์ค๋ฅธ์ชฝ ์๋๊น์ง์ ์ต๋จ ๊ฑฐ๋ฆฌ ๋ฐํ
return graph[n - 1][m - 1];
}
int main(void) {
// N, M์ ๊ณต๋ฐฑ์ ๊ธฐ์ค์ผ๋ก ๊ตฌ๋ถํ์ฌ ์
๋ ฅ ๋ฐ๊ธฐ
cin >> n >> m;
// 2์ฐจ์ ๋ฆฌ์คํธ์ ๋งต ์ ๋ณด ์
๋ ฅ ๋ฐ๊ธฐ
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
scanf("%1d", &graph[i][j]);
}
}
// BFS๋ฅผ ์ํํ ๊ฒฐ๊ณผ ์ถ๋ ฅ
cout << bfs(0, 0) << '\n';
return 0;
}
- java
import java.util.*;
class Node {
private int x;
private int y;
public Node(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return this.x;
}
public int getY() {
return this.y;
}
}
public class Main {
public static int n, m;
public static int[][] graph = new int[201][201];
// ์ด๋ํ ๋ค ๊ฐ์ง ๋ฐฉํฅ ์ ์ (์, ํ, ์ข, ์ฐ)
public static int dx[] = {-1, 1, 0, 0};
public static int dy[] = {0, 0, -1, 1};
public static int bfs(int x, int y) {
// ํ(Queue) ๊ตฌํ์ ์ํด queue ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ฌ์ฉ
Queue<Node> q = new LinkedList<>();
q.offer(new Node(x, y));
// ํ๊ฐ ๋น ๋๊น์ง ๋ฐ๋ณตํ๊ธฐ
while(!q.isEmpty()) {
Node node = q.poll();
x = node.getX();
y = node.getY();
// ํ์ฌ ์์น์์ 4๊ฐ์ง ๋ฐฉํฅ์ผ๋ก์ ์์น ํ์ธ
for (int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
// ๋ฏธ๋ก ์ฐพ๊ธฐ ๊ณต๊ฐ์ ๋ฒ์ด๋ ๊ฒฝ์ฐ ๋ฌด์
if (nx < 0 || nx >= n || ny < 0 || ny >= m) continue;
// ๋ฒฝ์ธ ๊ฒฝ์ฐ ๋ฌด์
if (graph[nx][ny] == 0) continue;
// ํด๋น ๋
ธ๋๋ฅผ ์ฒ์ ๋ฐฉ๋ฌธํ๋ ๊ฒฝ์ฐ์๋ง ์ต๋จ ๊ฑฐ๋ฆฌ ๊ธฐ๋ก
if (graph[nx][ny] == 1) {
graph[nx][ny] = graph[x][y] + 1;
q.offer(new Node(nx, ny));
}
}
}
// ๊ฐ์ฅ ์ค๋ฅธ์ชฝ ์๋๊น์ง์ ์ต๋จ ๊ฑฐ๋ฆฌ ๋ฐํ
return graph[n - 1][m - 1];
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// N, M์ ๊ณต๋ฐฑ์ ๊ธฐ์ค์ผ๋ก ๊ตฌ๋ถํ์ฌ ์
๋ ฅ ๋ฐ๊ธฐ
n = sc.nextInt();
m = sc.nextInt();
sc.nextLine(); // ๋ฒํผ ์ง์ฐ๊ธฐ
// 2์ฐจ์ ๋ฆฌ์คํธ์ ๋งต ์ ๋ณด ์
๋ ฅ ๋ฐ๊ธฐ
for (int i = 0; i < n; i++) {
String str = sc.nextLine();
for (int j = 0; j < m; j++) {
graph[i][j] = str.charAt(j) - '0';
}
}
// BFS๋ฅผ ์ํํ ๊ฒฐ๊ณผ ์ถ๋ ฅ
System.out.println(bfs(0, 0));
}
}
'๐ฆฅ ์ฝํ > ์ด๊ฒ์ด ์ฝ๋ฉ ํ ์คํธ๋ค with python' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[์ด๊ฒ์ด ์ฝ๋ฉ ํ ์คํธ๋ค with Python]_22_์ฝ์ ์ ๋ ฌ (0) | 2022.02.08 |
---|---|
[์ด๊ฒ์ด ์ฝ๋ฉ ํ ์คํธ๋ค with Python]_21_์ ํ์ ๋ ฌ (0) | 2022.02.08 |
[์ด๊ฒ์ด ์ฝ๋ฉ ํ ์คํธ๋ค with Python]_19_BFS (0) | 2022.02.02 |
[์ด๊ฒ์ด ์ฝ๋ฉ ํ ์คํธ๋ค with Python]_18_DFS (0) | 2022.02.02 |
[์ด๊ฒ์ด ์ฝ๋ฉ ํ ์คํธ๋ค with Python]_17_์ฌ๊ท ํจ์ (0) | 2022.02.02 |