import java.util.*;
/*
백준 2667번 : 단지번호붙이기
*/
public class Main {
int N;
int A[][];
boolean visited[][];
void InputData() {
Scanner in = new Scanner(System.in);
N = in.nextInt();
A = new int[N][N];
for (int i=0; i<N; i++) {
String str = in.next();
char[] ch = str.toCharArray();
for (int j=0; j<N; j++) {
A[i][j] = ch[j] - '0';
}
}
}
int num = 0;
int count = 1;
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
void DFS(int x, int y) {
visited[x][y] = true;
for (int i=0; i<4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (nx < 0 || nx >= N || ny < 0 || ny >= N) continue;
if (visited[nx][ny]) continue;//방문한 정점이면 스킵
if (A[nx][ny] == 0) continue;//값이 0이면 간선이 없으므로 스킵
count++;
DFS(nx, ny);
}
}
void Solve() {
ArrayList<Integer> list = new ArrayList<>();
visited = new boolean[N][N];
for (int i=0; i<N; i++) {
for (int j=0; j<N; j++) {
if (A[i][j] == 0) continue;
if (visited[i][j]) continue;
DFS(i, j);// 새 단지 검색
num++;// 단지 번호 업데이트
list.add(count);// 단지 안의 집의 수 업데이트
count = 1;// 집수 초기화
}
}
Collections.sort(list);// 단지 집의 수를 오름차순 정렬
System.out.println(num);
for (int x : list)
System.out.println(x);
}
public static void main(String[] args) {
Main m = new Main();
m.InputData();
m.Solve();
}
}
<구현 #2>
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.Queue;
import java.util.LinkedList;
import java.util.ArrayList;
import java.util.Collections;
public class Main {
static int N;
static int map[][];
static int dx[] = {-1, 0, 1, 0};
static int dy[] = {0, 1, 0, -1};
static ArrayList<Integer> list = new ArrayList<>();
class Node {
int x, y;
Node (int x, int y) {
this.x = x;
this.y = y;
}
}
void BFS(int x, int y, int id) {
Queue<Node> Q = new LinkedList<>();
map[x][y] = id;
int count = 1;
Q.offer(new Node(x, y));
while (!Q.isEmpty()) {
Node now = Q.poll();
for (int i=0; i<4; i++) {
int nx = now.x + dx[i];
int ny = now.y + dy[i];
if (nx < 0 || nx >= N || ny < 0 || ny >= N) continue;
if (map[nx][ny] != 1) continue;
map[nx][ny] = id;
count++;
Q.offer(new Node(nx, ny));
}
}
list.add(count);
}
void Solve() {
int group_count = 1;
for (int i=0; i<N; i++) {
for (int j=0; j<N; j++) {
if (map[i][j] == 1) {
group_count++;
BFS(i, j, group_count);
}
}
}
StringBuilder sb = new StringBuilder();
sb.append(group_count-1).append('\n');
Collections.sort(list);
for (int x : list) {
sb.append(x).append('\n');
}
System.out.println(sb.toString());
}
void inputData() throws Exception {
InputStreamReader reader = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(reader);
N = Integer.parseInt(br.readLine());
map = new int[N][N];
for (int i=0; i<N; i++) {
String str = br.readLine();
for (int j=0; j<N; j++) {
map[i][j] = str.charAt(j)-'0';
}
}
}
public static void main(String[] args) throws Exception {
int ans = -2;
Main m = new Main();
m.inputData(); // 입력 받는 부분
m.Solve();// 여기서부터 작성
}
}
댓글