01-10 04:27
Recent Posts
Recent Comments
๊ด€๋ฆฌ ๋ฉ”๋‰ด

miinsun

[BAEKJOON] ๋ฐฑ์ค€ ๊ทธ๋ž˜ํ”„ 2667 :: ๋‹จ์ง€๋ฒˆํ˜ธ๋ถ™์ด๊ธฐ ๋ณธ๋ฌธ

Algorithm/Baekjoon

[BAEKJOON] ๋ฐฑ์ค€ ๊ทธ๋ž˜ํ”„ 2667 :: ๋‹จ์ง€๋ฒˆํ˜ธ๋ถ™์ด๊ธฐ

miinsun 2022. 4. 2. 23:15

 

๐Ÿ’ฌ  ๋ฌธ์ œ ์„ค๋ช…

<๊ทธ๋ฆผ 1>๊ณผ ๊ฐ™์ด ์ •์‚ฌ๊ฐํ˜• ๋ชจ์–‘์˜ ์ง€๋„๊ฐ€ ์žˆ๋‹ค. 1์€ ์ง‘์ด ์žˆ๋Š” ๊ณณ์„, 0์€ ์ง‘์ด ์—†๋Š” ๊ณณ์„ ๋‚˜ํƒ€๋‚ธ๋‹ค.

์ฒ ์ˆ˜๋Š” ์ด ์ง€๋„๋ฅผ ๊ฐ€์ง€๊ณ  ์—ฐ๊ฒฐ๋œ ์ง‘์˜ ๋ชจ์ž„์ธ ๋‹จ์ง€๋ฅผ ์ •์˜ํ•˜๊ณ , ๋‹จ์ง€์— ๋ฒˆํ˜ธ๋ฅผ ๋ถ™์ด๋ ค ํ•œ๋‹ค.
์—ฌ๊ธฐ์„œ ์—ฐ๊ฒฐ๋˜์—ˆ๋‹ค๋Š” ๊ฒƒ์€ ์–ด๋–ค ์ง‘์ด ์ขŒ์šฐ, ํ˜น์€ ์•„๋ž˜์œ„๋กœ ๋‹ค๋ฅธ ์ง‘์ด ์žˆ๋Š” ๊ฒฝ์šฐ๋ฅผ ๋งํ•œ๋‹ค.
๋Œ€๊ฐ์„ ์ƒ์— ์ง‘์ด ์žˆ๋Š” ๊ฒฝ์šฐ๋Š” ์—ฐ๊ฒฐ๋œ ๊ฒƒ์ด ์•„๋‹ˆ๋‹ค.

<๊ทธ๋ฆผ 2>๋Š” <๊ทธ๋ฆผ 1>์„ ๋‹จ์ง€๋ณ„๋กœ ๋ฒˆํ˜ธ๋ฅผ ๋ถ™์ธ ๊ฒƒ์ด๋‹ค.
์ง€๋„๋ฅผ ์ž…๋ ฅํ•˜์—ฌ ๋‹จ์ง€์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜๊ณ , ๊ฐ ๋‹จ์ง€์— ์†ํ•˜๋Š” ์ง‘์˜ ์ˆ˜๋ฅผ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜์—ฌ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

 

๐Ÿ”จ  ์ž…์ถœ๋ ฅ ์˜ˆ

์ž…๋ ฅ 

  • ์ฒซ ๋ฒˆ์งธ ์ค„์—๋Š” ์ง€๋„์˜ ํฌ๊ธฐ N(์ •์‚ฌ๊ฐํ˜•์ด๋ฏ€๋กœ ๊ฐ€๋กœ์™€ ์„ธ๋กœ์˜ ํฌ๊ธฐ๋Š” ๊ฐ™์œผ๋ฉฐ 5≤N≤25)์ด ์ž…๋ ฅ๋˜๊ณ ,
  • ๊ทธ ๋‹ค์Œ N์ค„์—๋Š” ๊ฐ๊ฐ N๊ฐœ์˜ ์ž๋ฃŒ(0ํ˜น์€ 1)๊ฐ€ ์ž…๋ ฅ๋œ๋‹ค.

 

์ถœ๋ ฅ

  • ์ฒซ ๋ฒˆ์งธ ์ค„์—๋Š” ์ด ๋‹จ์ง€์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜์‹œ์˜ค.
  • ๊ทธ๋ฆฌ๊ณ  ๊ฐ ๋‹จ์ง€๋‚ด ์ง‘์˜ ์ˆ˜๋ฅผ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜์—ฌ ํ•œ ์ค„์— ํ•˜๋‚˜์”ฉ ์ถœ๋ ฅํ•˜์‹œ์˜ค.

 

์˜ˆ์ œ ์ž…๋ ฅ 1)

7
0110100
0110101
1110101
0000111
0100000
0111110
0111000

 

์˜ˆ์ œ ์ถœ๋ ฅ 1)

3
7
8
9
 

 

โ€‹

๐Ÿ’ป  Main.java

  • DFS๋ฅผ ํ™œ์šฉํ–ˆ๋‹ค
  • ๋‹จ์ง€์˜ ์ˆ˜๋Š” check์— ์ €์žฅ๋˜๋Š”๋ฐ, checkํฌ๊ธฐ๋Š” n*n์•ˆ์— ๋“ค์–ด๊ฐ„๋‹ค๋Š” ๊ฒƒ์„ ์œ ์˜ํ•˜์ž
  • ๋ฌธ์ œ ํ’€์ดํ•  ๋•Œ, ๋ฌธ์ œ๋ฅผ ์ž˜๋ชป์ดํ•ดํ•ด์„œ ํ‘ธ๋Š”๋ฐ ๋งŽ์€ ์‹œ๊ฐ„์ด ์†Œ์š”๋๋˜ ๋ฌธ์ œ์ด๋‹ค.
  • ๊ผญ ๋‹จ์ง€๋‚ด ์ง‘์˜ ์ˆ˜๋ฅผ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜์ž
/* ๋ฐฑ์ค€ ๊ทธ๋ž˜ํ”„ - 2667 :: ๋‹จ์ง€๋ฒˆํ˜ธ๋ถ™์ด๊ธฐ */
import java.io.*;
import java.util.*;

public class Main {
	static int [][] board;
	static int[] check;
	static int n;

	// ์•ž ๋’ค ์–‘์˜† ์ขŒํ‘œ ์ •๋ณด 
	static int[] dx = {1, 0, -1, 0}; 
	static int[] dy = {0, 1, 0, -1};

	static void DFS(int x, int y, int order) {
		check[order]++;
		board[x][y] = 0;
	
		for(int i = 0; i < 4; i++) { 
			// ๋‹ค์Œ์— ์˜ฌ ์ขŒํ‘œ
			int nx = x + dx[i]; 
			int ny = y + dy[i]; 
			
			// ์ง€ํ‘œ ๊ฒฝ๊ณ„ ๊ฐ’ ๊ฒ€์‚ฌ and ๋‹จ์ง€ ํ™•์ธ
			if(nx >= 0 && ny >= 0 && nx < n && ny < n && board[nx][ny] == 1) { 
				DFS(nx, ny, order); 
			}
		}
	}
	
	public static void main(String[] args) throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringBuilder sb = new StringBuilder();
		
		// ์ง€๋„ ์ •๋ณด ์ž…๋ ฅ
		n = Integer.parseInt(br.readLine());
		board = new int [n][n];
		check = new int [n * n];
		for(int i = 0; i < n; i++) {
			char[] c = br.readLine().toCharArray();
			for(int j = 0; j < n; j++) {
				board[i][j] = c[j] - '0';
			}
		}
		
		// DFS๋ฅผ ์ด์šฉํ•ด ๋‹จ์ง€ ๋ฒˆํ˜ธ ๋ถ™์ด๊ธฐ
		int order = 0;
		for(int i = 0; i < n; i++) {
			for(int j = 0; j < n; j++) {
				if(board[i][j] == 1) {
					order++;
					DFS(i, j, order);
				}
			}
		}
				
		// ์ถœ๋ ฅ
		sb.append(order).append('\n');
		
		// ๋‹จ์ง€ ์ˆ˜ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ
		int[] tmp = new int[order];
		for(int i = 1; i <= order; i++) {
			tmp[i - 1] = check[i];
		}
		Arrays.sort(tmp);
		
		for(int i: tmp) {
			sb.append(i).append('\n');
		}
		
		System.out.println(sb);
		br.close();
	}
}
Comments