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;
  }
}
๋ฐฉ๋ฌธํ•ด ์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค! ๋Œ“๊ธ€,์ง€์ ,ํ”ผ๋“œ๋ฐฑ ์–ธ์ œ๋‚˜ ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค๐Ÿ˜Š

๐Ÿ˜€

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ