[๋ฐฑ์ค/1012/์ ๊ธฐ๋ ๋ฐฐ์ถ]
https://www.acmicpc.net/problem/1012
1012๋ฒ: ์ ๊ธฐ๋ ๋ฐฐ์ถ
์ฐจ์ธ๋ ์๋์ธ ํ๋๋ ๊ฐ์๋ ๊ณ ๋ญ์ง์์ ์ ๊ธฐ๋ ๋ฐฐ์ถ๋ฅผ ์ฌ๋ฐฐํ๊ธฐ๋ก ํ์๋ค. ๋์ฝ์ ์ฐ์ง ์๊ณ ๋ฐฐ์ถ๋ฅผ ์ฌ๋ฐฐํ๋ ค๋ฉด ๋ฐฐ์ถ๋ฅผ ํด์ถฉ์ผ๋ก๋ถํฐ ๋ณดํธํ๋ ๊ฒ์ด ์ค์ํ๊ธฐ ๋๋ฌธ์, ํ๋๋ ํด์ถฉ ๋ฐฉ์ง์
www.acmicpc.net
<<ํ์ด>>
์ฐ์ ์ด ๋ฌธ์ ๋ DFS๋ก ํ์ด์ผ ํ๋ค๋ ์๊ฐ์ด ๋ค์๋ค. ๊ทธ๋์ ์ด์ฐจ์ ๋ฐฐ์ด์ ํ์ฉํด์
DFSํํ๋ก ๊ตฌ์์ ํ๋ค.
๋ง์ฝ ํด๋น ๋ฐฐ์ด์ ๊ฐ์ด 1์ด๋ผ๋ฉด dx, dy๋ก ์ํ ์ข์ฐ์ 1์ธ์ง ์๋์ง ์ต์ข ์ ์ผ๋ก ์์ ๋ ๊น์ง ๊น๊ฒ ๋ค์ด๊ฐ๋ค.
๊ทธ๋ฆฌ๊ณ ์ด๋ฏธ ๋ค์ด๊ฐ ๊ฐ์ ch์ ๊ฐ์ ์ขํ์ 1์ ์ถ๊ฐํจ์ผ๋ก์จ ์ค๋ณต๊ณ์ฐ ๋๋ ๊ฒ์ ๋ง์ผ๋ ค ํ๋ค.
๋ ผ๋ฆฌ์ ์ผ๋ก๋ ํ๋ฆฐ๊ฒ ์์ด๋ณด์ด์ง๋ง..
ํ๋ ธ๋ค ใ
์ค๊น?
import java.util.Scanner;
class Main {
public static int[][] ch;
public static int count;
public static int[] dx = {-1, 1, 0, 0};
public static int[] dy = {0, 0, -1, 1};
private void DFS(int n, int m, int[][] arr, int N, int M) {
if (N >= 0 && N < n && M >= 0 && M < m && arr[N][M] == 1 && ch[N][M] != 1) {
ch[N][M] = 1;
DFS(n, m, arr, n+dx[0], m+dy[0]);
DFS(n, m, arr, n+dx[1], m+dy[1]);
DFS(n, m, arr, n+dx[2], m+dy[2]);
DFS(n, m, arr, n+dx[3], m+dy[3]);
} else return;
}
public static void main(String[] args) {
Main T = new Main();
Scanner in = new Scanner(System.in);
int test = in.nextInt();
for (int i = 0; i < test; i++) {
int m = in.nextInt();
int n = in.nextInt();
int where = in.nextInt();
int[][] arr = new int[n][m];
ch = new int[n][m];
for (int j = 0; j < where; j++) {
int b = in.nextInt();
int a = in.nextInt();
arr[a][b] = 1;
}
for (int j = 0; j < n; j++) {
for (int k = 0; k < m; k++) {
if (arr[j][k] == 1 && ch[j][k] != 1) {
ch[j][k] = 1;
T.DFS(n, m, arr, j+dx[0], k+dy[0]);
T.DFS(n, m, arr, j+dx[1], k+dy[1]);
T.DFS(n, m, arr, j+dx[2], k+dy[2]);
T.DFS(n, m, arr, j+dx[3], k+dy[3]);
count++;
}
}
}
System.out.println(count);
ch = new int[n][m];
count = 0;
}
}
}
ใ ใ ์๊ณ ๋ณด๋๊น ๊ฐ์ ์๋ชป ๋์ ํ๋ค ใ ใ ..
์ ์๋ ์ฝ๋๋ฅผ ์ ๋ณด๋ฉด N์ด ๋ค์ด๊ฐ ์๋ฆฌ๋ฅผ n์ผ๋ก ํ๊ณ M์ m์ ๋ฃ์ ์ ์ด๋ค.
์ฝ๋ฉ์ด ๊ธธ์ด์ง๊ณ ๋ฃจ์ฆํด์ง๋ค ๋ณด๋๊น ํท๊ฐ๋ ธ๋๋ณด๋ค !!
์ด๋์ ์ฝ๋ฉ์ ํ ๋ ์ด๋ฆ ์ ์ ๋ ์ค์ํ ๋ฌธ์ ์ธ ๊ฒ์ด๋ค.
์์ฑํ ๋ ๊ท์ฐฎ๋๋ผ๋ ์ ๋๋ก ์์ฑํ๊ณ ํท๊ฐ๋ฆฌ์ง ์๊ฒ ํด์ผ๊ฒ ๋ค.
๋ฐ๊พธ๋ ๋ฐ๋ก ์ ๋ต์ด๋ค ~!
import java.util.ArrayList;
import java.util.Scanner;
class Main {
public static int[][] ch;
public static int count;
public static int[] dx = {-1, 1, 0, 0};
public static int[] dy = {0, 0, -1, 1};
private void DFS(int n, int m, int[][] arr, int N, int M) {
if (N >= 0 && N < n && M >= 0 && M < m && arr[N][M] == 1 && ch[N][M] != 1) {
ch[N][M] = 1;
DFS(n, m, arr, N+dx[0], M+dy[0]);
DFS(n, m, arr, N+dx[1], M+dy[1]);
DFS(n, m, arr, N+dx[2], M+dy[2]);
DFS(n, m, arr, N+dx[3], M+dy[3]);
} else return;
}
public static void main(String[] args) {
Main T = new Main();
Scanner in = new Scanner(System.in);
int test = in.nextInt();
ArrayList<Integer> answer = new ArrayList<>();
for (int i = 0; i < test; i++) {
int m = in.nextInt();
int n = in.nextInt();
int where = in.nextInt();
int[][] arr = new int[n][m];
ch = new int[n][m];
for (int j = 0; j < where; j++) {
int b = in.nextInt();
int a = in.nextInt();
arr[a][b] = 1;
}
for (int j = 0; j < n; j++) {
for (int k = 0; k < m; k++) {
if (arr[j][k] == 1 && ch[j][k] != 1) {
ch[j][k] = 1;
T.DFS(n, m, arr, j+dx[0], k+dy[0]);
T.DFS(n, m, arr, j+dx[1], k+dy[1]);
T.DFS(n, m, arr, j+dx[2], k+dy[2]);
T.DFS(n, m, arr, j+dx[3], k+dy[3]);
count++;
}
}
}
answer.add(count);
ch = new int[n][m];
count = 0;
}
for(int x : answer) System.out.println(x);
}
}
์๋ ๋ฐฉ์์ผ๋ก ์ฝ๋๋ฅผ ์ง๋ ์ ๋ต์ด๋ค.
์๊ฐํด๋ณด๋ฉด ์ ๋ ๊ฒ ์ฒ์์ ์์ํด๋ ๋ฌธ์ ๊ฐ ์๋ค.
์์์ ์ด ๋ค๋ฅธ ๊ฒ ๋ฟ์ด๋ค.
์์ง ์ต์ํ์ง ์์ผ๋ ์ฝ๋๊ฐ ๋ณต์กํด์ง๊ณ ์ธ๋ฐ ์์ด ๊ธธ์ด์ง๋ ์ํฉ์ธ ๊ฒ์ด๋ค.
๊พธ์คํ ์ฐ์ตํด์ ์ด๋ฌํ ๋ถ๋ถ ๋ํ ๋ณด์ํด ๋๊ฐ์ !!
import java.util.ArrayList;
import java.util.Scanner;
class Main {
public static int[][] ch;
public static int count;
public static int[] dx = {-1, 1, 0, 0};
public static int[] dy = {0, 0, -1, 1};
private void DFS(int n, int m, int[][] arr, int N, int M) {
if (N >= 0 && N < n && M >= 0 && M < m && arr[N][M] == 1 && ch[N][M] != 1) {
ch[N][M] = 1;
DFS(n, m, arr, N+dx[0], M+dy[0]);
DFS(n, m, arr, N+dx[1], M+dy[1]);
DFS(n, m, arr, N+dx[2], M+dy[2]);
DFS(n, m, arr, N+dx[3], M+dy[3]);
} else return;
}
public static void main(String[] args) {
Main T = new Main();
Scanner in = new Scanner(System.in);
int test = in.nextInt();
ArrayList<Integer> answer = new ArrayList<>();
for (int i = 0; i < test; i++) {
int m = in.nextInt();
int n = in.nextInt();
int where = in.nextInt();
int[][] arr = new int[n][m];
ch = new int[n][m];
for (int j = 0; j < where; j++) {
int b = in.nextInt();
int a = in.nextInt();
arr[a][b] = 1;
}
for (int j = 0; j < n; j++) {
for (int k = 0; k < m; k++) {
if (arr[j][k] == 1 && ch[j][k] != 1) {
T.DFS(n, m, arr, j, k);
count++;
}
}
}
answer.add(count);
ch = new int[n][m];
count = 0;
}
for(int x : answer) System.out.println(x);
}
}
์ด ๋ฌธ์ ๋ฅผ ํตํด์ dx, dy๋ฅผ ํ์ฉํด๋ณด์๊ณ , DFS์ ๋ํด ๊ณต๋ถํ๋ ์ข์ ๊ณ๊ธฐ๊ฐ ๋์๋ค.
์์ง์ ์ต์ํ์ง ์์ DFS BFS ์ต์ํด์ง ์ ์๋๋ก ๊ณ์ ์ฐ์ตํ์.