Publish:

νƒœκ·Έ: , , , , ,

μΉ΄ν…Œκ³ λ¦¬:

img_3.png

문제

문제 링크

img_3.png

μ„€λͺ…

이번 λ¬Έμ œλŠ” κ·Έλž˜ν”„μ— λŒ€ν•œ μ •λ³΄λ§Œ 주어지고 κ·Έ κ·Έλž˜ν”„λ₯Ό νƒμƒ‰ν•˜λŠ” 것이 μ•„λ‹ˆλΌ 주어진 μž…λ ₯κ°’μ—μ„œ κ·Έλž˜ν”„ 탐색이 이루어져야 ν•  뢀뢄을 μ°Ύμ•„μ•Ό ν•œλ‹€. μ•„λž˜ 그림처럼 맨 처음 ν•­λͺ©λΆ€ν„° 1이 적힌 뢀뢄을 탐색해야 ν•œλ‹€.

img_3.png

1 뢀뢄을 λ§Œλ‚˜λ©΄ 이동할 수 μžˆλŠ” κ²½λ‘œλŠ” 상,ν•˜,쒌,우 μš”μ†Œμ— λŒ€ν•΄ λ‹€μ‹œ 탐색을 μˆ˜ν–‰ν•œλ‹€. μ΄λ•Œ dx dy λ°©μ‹μœΌλ‘œ λ°©ν–₯에 λŒ€ν•œ μœ„μΉ˜λ₯Ό 배열에 미리 μ €μž₯ν•΄ 두고 μ‚¬μš©ν•˜λ©΄ μ½”λ“œκ°€ 간결해진닀.

1
2
3
4
5
6
7
8
static int[][] pos = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} }; // μƒν•˜μ’Œμš°
   ...
// μƒν•˜μ’Œμš° 탐색
for (int i = 0; i < pos.length; i++){
  // λ°©ν–₯ 이동 ν›„ μ’Œν‘œ
  int nx = x + pos[i][0];   
  int ny = y + pos[i][1];
}

dfs ν•¨μˆ˜ λ‚΄μ—μ„œ 상,ν•˜,쒌,우 μ’Œν‘œλ‘œ 계속 κ°±μ‹ ν•˜λ©΄μ„œ 각각의 μš”μ†Œμ—μ„œ λ‹€μ‹œ dfs ν•œλ‹€. μ΄λ•Œ 이동 ν–ˆμ„λ•Œμ˜ μ’Œν‘œ μΈλ±μŠ€κ°€ graph λ°°μ—΄μ˜ 인덱슀λ₯Ό λ²—μ–΄λ‚˜λŠ” 경우λ₯Ό λ°©μ§€ν•˜κΈ° μœ„ν•΄ λ²”μœ„ 내에 μžˆμ„ κ²½μš°μ—λ§Œ νƒμƒ‰ν•˜λ„λ‘ ν•œλ‹€.

1
2
3
4
// μ΄λ™ν•œ κ²½λ‘œκ°€ graph λ°°μ—΄ λ²”μœ„ μ•ˆμ— μžˆλŠ”μ§€ 검사
private static boolean isRange(int nx, int ny) {
  return nx >= 0 && nx < N && ny >= 0 && ny < N;
}

풀이 (DFS)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
public class Main {
  static int[][] graph;
  static boolean[][] visited;
  static int N;
  static int[][] pos = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} }; // μƒν•˜μ’Œμš°
  static int count;

  public static void main(String[] args) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    N = Integer.parseInt(br.readLine());
    graph = new int[N][N];
    visited = new boolean[N][N];

    for (int i = 0; i < N; i++) {
      String s = br.readLine();
      for (int j = 0; j < N; j++) {
        graph[i][j] = s.charAt(j) - '0';
      }
    }

    ArrayList<Integer> result = new ArrayList<>();
    // (0,0) λΆ€ν„° λͺ¨λ“  μš”μ†Œμ— λŒ€ν•΄ dfs
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        if (!visited[i][j] && graph[i][j] == 1) {
          count = 0;
          dfs(i, j);
          result.add(count);
        }
      }
    }

    System.out.println(result.size());
    Collections.sort(result);
    for (Integer integer : result) {
      System.out.println(integer);
    }
  }

  private static void dfs(int x, int y) {
    visited[x][y] = true;
    count++;
    // μƒν•˜μ’Œμš° 탐색
    for (int i = 0; i < pos.length; i++) {
      int nx = x + pos[i][0];
      int ny = y + pos[i][1];

      if (isRange(nx, ny) && !visited[nx][ny] && graph[nx][ny] == 1) {
        dfs(nx, ny);
      }
    }
  }

  private static boolean isRange(int nx, int ny) {
    return nx >= 0 && nx < N && ny >= 0 && ny < N;
  }
}

풀이 (BFS)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
public class Main {
  static int[][] graph;
  static boolean[][] visited;
  static int N;
  static int[][] pos = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} }; // μƒν•˜μ’Œμš°
  static int count;

  public static void main(String[] args) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    N = Integer.parseInt(br.readLine());
    graph = new int[N][N];
    visited = new boolean[N][N];

    for (int i = 0; i < N; i++) {
      String s = br.readLine();
      for (int j = 0; j < N; j++) {
        graph[i][j] = s.charAt(j) - '0';
      }
    }

    ArrayList<Integer> result = new ArrayList<>();
    // (0,0) λΆ€ν„° λͺ¨λ“  μš”μ†Œμ— λŒ€ν•΄ dfs
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        if (!visited[i][j] && graph[i][j] == 1) {
          count = 1;
          bfs(i, j);
          result.add(count);
        }
      }
    }

    System.out.println(result.size());
    Collections.sort(result);
    for (Integer integer : result) {
      System.out.println(integer);
    }
  }

  private static void bfs(int x, int y) {
    Queue<int[]> queue = new LinkedList<>();
    queue.offer(new int[] {x, y});
    visited[x][y] = true;

    while (!queue.isEmpty()) {
      int[] current = queue.poll();
      int cx = current[0];
      int cy = current[1];

      // μƒν•˜μ’Œμš° 탐색
      for (int i = 0; i < pos.length; i++) {
        int nx = cx + pos[i][0];
        int ny = cy + pos[i][1];

        if (isRange(nx, ny) && !visited[nx][ny] && graph[nx][ny] == 1) {
          visited[nx][ny] = true;
          queue.offer(new int[] {nx, ny});
          count++;
        }
      }
    }
  }

  private static boolean isRange(int nx, int ny) {
    return nx >= 0 && nx < N && ny >= 0 && ny < N;
  }
}
λ°©λ¬Έν•΄ μ£Όμ…”μ„œ κ°μ‚¬ν•©λ‹ˆλ‹€! λŒ“κΈ€,지적,ν”Όλ“œλ°± μ–Έμ œλ‚˜ ν™˜μ˜ν•©λ‹ˆλ‹€πŸ˜Š

λŒ“κΈ€λ‚¨κΈ°κΈ°