ALGORITHM/SWEXPERT|SOFTEER
[Softeer] 장애물 인식 프로그램 (lv.2)
0298
2021. 11. 2. 22:43
https://softeer.ai/practice/info.do?eventIdx=1&psProblemId=409
Softeer
제한시간 : C/C++(1초), Java/Python/JS(2초) | 메모리 제한 : 128MB 입력형식 입력 값의 첫 번째 줄에는 지도의 크기 N(정사각형임으로 가로와 세로의 크기는 같으며 5≤N≤25)이 입력되고, 그 다음 N줄에는
softeer.ai
2021
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
69
70
71
|
import java.util.*;
import java.io.*;
public class Main
{
public static int N;
public static int[][] arr;
public static boolean[][] vtd;
public static ArrayList<Integer> list;
public static int[] dx = {-1, 0, 1, 0};
public static int[] dy = {0, -1, 0, 1};
public static void solve(int pp, int qq) {
Queue<int[]> q = new LinkedList<>();
vtd[pp][qq] = true;
q.add(new int[]{pp, qq});
int size = 1;
while(!q.isEmpty()) {
int[] tmp = q.poll();
int x = tmp[0];
int y = tmp[1];
for(int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if(nx < 0 || ny < 0 || nx >= N || ny >= N || arr[nx][ny] == 0 || vtd[nx][ny]) continue;
else {
vtd[nx][ny] = true;
q.add(new int[]{nx, ny});
size++;
}
}
}
list.add(size);
}
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(bf.readLine());
N = Integer.parseInt(st.nextToken());
arr = new int[N][N];
vtd = new boolean[N][N];
list = new ArrayList<>();
for(int i = 0; i < N; i++) {
st = new StringTokenizer(bf.readLine());
String str = st.nextToken();
for(int j = 0; j < N; j++) {
arr[i][j] = Character.getNumericValue(str.charAt(j));
}
}
int count = 0;
for(int i = 0; i < N; i++) {
for(int j = 0; j < N; j++){
if(!vtd[i][j] && arr[i][j] == 1) {
count++;
solve(i, j);
}
}
}
System.out.println(count);
Collections.sort(list);
StringBuilder sb = new StringBuilder();
for (Integer integer : list) sb.append(integer).append("\n");
System.out.println(sb.toString());
}
}
|
cs |