https://www.acmicpc.net/problem/1012
import java.util.*;
class Main {
// https://www.acmicpc.net/problem/1012
static int[] dx = { -1, 0, 1, 0 };
static int[] dy = { 0, 1, 0, -1 }; // ์ฌ๋ฐฉ ๋๊ธฐ
static boolean[][] v;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int tc = sc.nextInt();
StringBuilder sb = new StringBuilder();
for (int t = 0; t < tc; t++) {
int m = sc.nextInt();
int n = sc.nextInt();
int baechu = sc.nextInt();
int[][] map = new int[m][n];
v = new boolean[m][n];
for (int i = 0; i < baechu; i++) {
int x = sc.nextInt();
int y = sc.nextInt();
map[x][y] = 1;
}
// ์ฌ๊ธฐ๊น์ง ๋งต ์ง๋ ๊ฑฐ
int sum = 0;
// bfs ์ง๊ธฐ
for (int x = 0; x < m; x++) {
for (int y = 0; y < n; y++) {
if (map[x][y] == 1 && !v[x][y]) {
sum++;
Queue<Integer> q = new LinkedList<>();
q.offer(map[x][y]);
v[x][y] = true;
while (!q.isEmpty()) {
q.poll();
for (int i = 0; i < 4; i++) {
int nx = dx[i] + x;
int ny = dy[i] + y;
if (!v[nx][ny] && nx < m && nx >= 0 && ny < n && ny >= 0) {
q.offer(map[nx][ny]);
v[nx][ny] = true;
}
}
}
}
}
}
sb.append(sum).append("\n");
}
System.out.println(sb);
sc.close();
}
}
์ค๋ต๋ ธํธ(ํธ๋ ๋์ ์ฌ๊ณ ๊ณผ์ ๋ฐ ๊ฐ์ ์ )
์ ๊ทผ์ ๋ง์์ผ๋ BFS ๋๋ฆด ๋๊ฐ ์๋ชป๋๋ค. ์ขํ๋ฅผ ๋ฐ์์ ๊ทธ๊ฑธ ๊ธฐ์ ์ผ๋ก bfs๋ฅผ ๋๋ ค์ผ ๋๋๋ฐ ์ด๋ ๊ฒํ๋ฉด ์ํ๋ ๋๋ก ๋์ง ์๋๋ค.
ํ์คํ๊ฒ ๊ตฌ์ญ์ด ๋๋ ์ง ๊ฐ์๋ฅผ ์ธ๋ ๊ฒ์ด๋ผ์ ์ ํ์ ์ธ bfs, dfs ๋ฌธ์ ๋ค. ์๋ง dfs๋ก ํ์์ผ๋ฉด ํ๋ฒ์ ํ๋ ธ์ ๊ฒ์ด๋ค.
์ ๋ต์ฝ๋
import java.util.*;
class Main {
// https://www.acmicpc.net/problem/1012
static int[] dx = { -1, 0, 1, 0 };
static int[] dy = { 0, 1, 0, -1 }; // ์ฌ๋ฐฉ ๋๊ธฐ
static int n;
static int m;
static int[][] map;
static boolean[][] v;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int tc = sc.nextInt();
StringBuilder sb = new StringBuilder();
for (int t = 0; t < tc; t++) {
m = sc.nextInt();
n = sc.nextInt();
int baechu = sc.nextInt();
// nxm ์ด ์์ฐ์ค๋ฝ๋ค.
map = new int[n][m];
v = new boolean[n][m];
int sum = 0;
for (int i = 0; i < baechu; i++) {
int c = sc.nextInt();
int r = sc.nextInt();
map[r][c] = 1;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (!v[i][j] && map[i][j] == 1) {
sum++;
bfs(i, j);
}
}
}
sb.append(sum).append("\n");
}
System.out.println(sb);
sc.close();
}
public static void bfs(int x, int y) {
Queue<int[]> q = new LinkedList<>();
q.offer(new int[] { x, y });
v[x][y] = true;
while (!q.isEmpty()) {
int[] curr = q.poll();
int cx = curr[0];
int cy = curr[1];
for (int k = 0; k < 4; k++) {
int nx = cx + dx[k];
int ny = cy + dy[k];
if (nx >= 0 && nx < n && ny >= 0 && ny < m && !v[nx][ny] && map[nx][ny] == 1) {
q.offer(new int[] { nx, ny });
v[nx][ny] = true;
}
}
}
}
}
์ ์ฒด ์ ๋ต ์ฝ๋๋ค. bfs๋ง ๋ ๋ผ๊ฐ์ง๊ณ ์ฒ์ฒํ ๋ณด์.
public static void bfs(int x, int y) {
Queue<int[]> q = new LinkedList<>();
q.offer(new int[] { x, y });
v[x][y] = true;
while (!q.isEmpty()) {
int[] curr = q.poll();
int cx = curr[0];
int cy = curr[1];
for (int k = 0; k < 4; k++) {
int nx = cx + dx[k];
int ny = cy + dy[k];
if (nx >= 0 && nx < n && ny >= 0 && ny < m && !v[nx][ny] && map[nx][ny] == 1) {
q.offer(new int[] { nx, ny });
v[nx][ny] = true;
}
}
}
}
Queue์ Typed์ผ๋ก int๋ฐฐ์ด์ ๋ฃ๊ณ , ํ ์์ํ ๋ ํ์ฌ ์์น(1์ด ๊ฒ์ถ๋) ์ขํ๋ฅผ ํฌ๊ธฐ 2์ง๋ฆฌ ์ด๊ธฐํ๋ int๋ฐฐ์ด๋ก ๋ฃ์ด์ค๋ค.
์ด๊ฒ C++์ด๋ ์ฝํ๋ฆฐ์ด์๋ค๋ฉด pair๋ฅผ ์ฌ์ฉํ์ผ๋ฉด ๋ ํ
์ง๋ง ์๋ฐ๋ ๊ทธ๋ฐ๊ฒ ์๋ค. ํด๋์ค ๋ง๋ค์ด์ ์ฐ๋ ๋ฐฉ๋ฒ์ ์์ง ์ต์ํ์ง์์์ ํฌ๊ธฐ 2์ง๋ฆฌ ๋ฐฐ์ด๋ก ๋์ ํ๋ค.
์ด ๋ฐฐ์ด๋ก ์ขํ๋ฅผ ๋ฐ๋ ๊ฒ์ ์ ์ธํ๋ฉด ์๋ ์ง ์ฝ๋์์ ๋ค๋ฅผ๊ฒ ์๋ค.
๋์์ด ๋๋ค๋ฉด ๋๊ธ์ด๋ ๊ณต๊ฐ ๋ฒํผ ํ ๋ฒ์ฉ ๋๋ฅด๊ณ ๊ฐ์ฃผ์ธ์! ๋ก๊ทธ์ธ ์ํด๋ ๋ฉ๋๋ค ^_^
'OJ๐ผ > ์ค๋ต๋ ธํธ๐' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[BOJ][JAVA] 14267๋ฒ: ํ์ฌ ๋ฌธํ 1 (0) | 2024.02.12 |
---|---|
[BOJ][JAVA] 11725๋ฒ: ํธ๋ฆฌ์ ๋ถ๋ชจ์ฐพ๊ธฐ (0) | 2024.02.10 |
[BOJ][JAVA] 15988: 1, 2, 3 ๋ํ๊ธฐ 3 (0) | 2024.01.28 |
[BOJ][JAVA] 2910๋ฒ: ๋น๋์ ๋ ฌ (0) | 2024.01.27 |
[BOJ][JAVA] 1181: ๋จ์ด์ ๋ ฌ (0) | 2024.01.23 |