Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
Tags
- oracle
- 알고리즘
- 문제풀이
- greedy
- 문제해결
- BOJ
- 자바
- SQL
- BFS
- 코딩테스트
- COS PRO
- Java
- binary search
- 프로그래머스
- 건강
- 코테
- 백준
- MySQL
- SWEA
- 운동기록
- db
- 투포인터
- 러닝일지
- dfs
- DP
- 문자열
- priorityqueue
- math
- 이분탐색
- 시뮬레이션
Archives
- Today
- Total
슈콩
[BOJ] 백준 2667 단지번호붙이기 본문
[문제]
https://www.acmicpc.net/problem/2667
[소스 코드]
import java.io.*;
import java.util.*;
public class Main {
static int n;
static int[][] map;
static boolean[][] visit;
static int[] dr = {-1,1,0,0};
static int[] dc = {0,0,-1,1};
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
n = Integer.parseInt(br.readLine());
map = new int[n][n];
for(int i=0;i<n;i++) {
String s = br.readLine();
for(int j=0;j<n;j++) {
map[i][j] = s.charAt(j) - '0';
}
}
visit = new boolean[n][n];
PriorityQueue<Integer> pq = new PriorityQueue<>();
for(int i=0;i<n;i++) {
for(int j=0;j<n;j++) {
if(!visit[i][j] && map[i][j]==1) {
pq.offer(bfs(i,j,1));
}
}
}
System.out.println(pq.size());
while(!pq.isEmpty()) {
System.out.println(pq.poll());
}
}
static int bfs(int i,int j,int idx) {
Queue<int[]> q = new LinkedList<>();
visit[i][j] = true;
q.offer(new int[] {i,j});
int cnt = 1;
while(!q.isEmpty()) {
int[] curr = q.poll();
for(int d=0;d<4;d++) {
int nr = curr[0] + dr[d];
int nc = curr[1] + dc[d];
if(nr<0 || nr>=n || nc<0 || nc>=n || visit[nr][nc]) continue;
if(map[nr][nc]==1) {
visit[nr][nc] = true;
q.offer(new int[] {nr,nc});
cnt++;
}
}
}
return cnt;
}
}'Algorithms > Baekjoon' 카테고리의 다른 글
| [BOJ] 백준 7569 토마토 (0) | 2025.09.29 |
|---|---|
| [BOJ] 백준 2644 촌수계산 (0) | 2025.09.29 |
| [BOJ] 백준 2606 바이러스 (0) | 2025.09.29 |
| [BOJ] 백준 2178 미로 탐색 (0) | 2025.09.29 |
| [BOJ] 백준 1260 DFS와 BFS (0) | 2025.09.29 |