1. ๋ฌธ์ : https://www.acmicpc.net/problem/1743
2. ํ์ด
์ ์ฒด ํ์ํ๋ฉด์ ๋ญ์ณฅ์๋ ์ฐ๋ ๊ธฐ ์ฐพ๋ ๋ฌธ์ ์ด๊ธฐ ๋๋ฌธ์ dfs๋ก ํ์๋ค.
3. ์ฝ๋
import java.io.IOException;
import java.util.*;
public class Main {
static int N, M;
static int K;
static int[][] maps;
static boolean[][] visited;
static int[] dx = {0, -1, 0, 1};
static int[] dy = {1, 0, -1, 0};
static int maxVal;
static int temp;
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
N = sc.nextInt();
M = sc.nextInt();
K = sc.nextInt();
maps = new int[N][M];
visited = new boolean[N][M];
int x = 0;
int y = 0;
for (int i = 0; i < K; i++) {
x = sc.nextInt();
y = sc.nextInt();
maps[x-1][y-1] = 1;
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
temp = 0;
if (!visited[i][j] && maps[i][j] == 1) {
dfs(i, j);
}
maxVal = Math.max(maxVal, temp);
}
}
System.out.println(maxVal);
}
public static void dfs(int x, int y) {
temp++;
visited[x][y] = true;
for (int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (nx < 0 || ny < 0 || nx >= N || ny >= M) continue;
if (!visited[nx][ny] && maps[nx][ny] == 1) {
visited[nx][ny] = true;
dfs(nx, ny);
}
}
}
}
'๐ป Coding Problems Solving > DFS | BFS | Backtracking' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[BOJ 17086] ์ด๋ชจํฐ์ฝ (0) | 2023.05.16 |
---|---|
[BOJ 12851] ์จ๋ฐ๊ผญ์ง2 (java) (0) | 2023.04.18 |
[BOJ 2178] ๋ฏธ๋ก ํ์ (java) (0) | 2023.04.11 |
[BOJ 1062] ๊ฐ๋ฅด์นจ (java) (0) | 2023.04.04 |
[ํ๋ก๊ทธ๋๋จธ์ค] LV.2 ๊ฒ์ ๋งต ์ต๋จ๊ฑฐ๋ฆฌ(java) (0) | 2023.03.31 |
์ต๊ทผ๋๊ธ