ALGORITHM/SWEXPERT|SOFTEER
[Softeer] 동계 테스트 시점 예측 (lv.3)
0298
2021. 11. 2. 22:44
https://softeer.ai/practice/info.do?eventIdx=1&psProblemId=411&sw_prbl_sbms_sn=27754
Softeer
제한시간 : C/C++(1초), Java/Python/JS(2초) | 메모리 제한 : 256MB 입력형식 첫째 줄에는 격자 화면의 크기를 나타내는 두 개의 정수 N, M (5 ≤ N, M ≤ 100)이 주어진다. 그 다음 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
import java.util.*;
import java.io.*;
public class Main {
public static int N, M, answer;
public static int[][] arr;
public static int[] dx = {-1, 0, 1, 0};
public static int[] dy = {0, -1, 0, 1};
public static void init() {
Queue<int[]> q = new LinkedList<>();
q.add(new int[]{0, 0});
boolean[][] vtd = new boolean[N][M];
vtd[0][0] = true;
arr[0][0] = -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 >= M || vtd[nx][ny] || arr[nx][ny] == 1) continue;
else {
arr[nx][ny] = -1;
vtd[nx][ny] = true;
q.add(new int[]{nx, ny});
}
}
}
}
public static boolean check() {
for(int i = 0; i < N; i++) {
for(int j = 0; j < M; j++) {
if(arr[i][j] == 1) return false;
}
}
return true;
}
public static void solve(int x, int y) {
int count = 0;
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 >= M || arr[nx][ny] != -1) continue;
else if(arr[nx][ny] == -1) {
count++;
}
}
if(count >= 2) arr[x][y] = 2;
}
public static void melting() {
for(int i = 0; i < N; i++)
for(int j = 0; j < M; j++)
if(arr[i][j] == 2) arr[i][j] = -1;
}
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());
M = Integer.parseInt(st.nextToken());
arr = new int[N][M];
answer = 0;
for(int i = 0; i < N; i++) {
st = new StringTokenizer(bf.readLine());
for(int j = 0; j < M; j++) {
arr[i][j] = Integer.parseInt(st.nextToken());
}
}
while(true) {
init();
if(check()) break;
for(int i = 0; i < N; i++) {
for(int j = 0; j < M; j++) {
if(arr[i][j] == 1) {
solve(i, j);
}
}
}
melting();
answer++;
}
System.out.println(answer);
}
}
|
cs |