Skip to content

Commit

Permalink
Merge pull request #193 from sds-2021-summer-algorithm/wjdgns7712-pat…
Browse files Browse the repository at this point in the history
…ch-6

[황정훈] 빙산
  • Loading branch information
wjdgns7712 authored Nov 19, 2021
2 parents 4ef7a7b + 1f22c51 commit df645e2
Showing 1 changed file with 99 additions and 0 deletions.
99 changes: 99 additions & 0 deletions solution/BOJ_2573_빙산/황정훈.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import java.io.*;
import java.util.*;

public class Main {

static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
static StringTokenizer st;

static int N, M, Y = 0, mass = 1;
static int[] dr = {-1, 0, 1, 0};
static int[] dc = {0, 1, 0, -1};
static int[][] map;
static class Point{
int r, c;
public Point(int r, int c){
this.r = r;
this.c = c;
}
}
static boolean[][] visit;
static Queue<Point> Q;

public static void main(String[] args) throws Exception {

st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
map = new int[N][M];
for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 0; j < M; j++) {
map[i][j] = Integer.parseInt(st.nextToken());
}
}

while(mass==1){
Y++;
melt();
split_check();
}

bw.write(mass>1 ? Y+"\n" : "0\n");
bw.flush();
bw.close();
br.close();
}
static void melt(){
int[][] tmp = new int[N][M];
for (int i = 1; i < N-1; i++) {
for (int j = 1; j < M-1; j++) {
if (map[i][j]<=0) continue;
int cnt = 0;
for (int w = 0; w < 4; w++) {
int nr = i + dr[w];
int nc = j + dc[w];
if(map[nr][nc]<=0) cnt++;
}
tmp[i][j] -= cnt;
}
}

for (int i = 1; i < N-1; i++) {
for (int j = 1; j < M-1; j++) {
map[i][j] += tmp[i][j];
}
}
}
static void split_check(){
mass = 0;
visit = new boolean[N][M];

for (int i = 1; i < N-1; i++) {
for (int j = 1; j < M-1; j++) {
if (map[i][j]>0 && !visit[i][j]){
mass++;
bfs(i, j);
}
}
}
}
static void bfs(int i, int j){
Q = new ArrayDeque<>();
Q.add(new Point(i, j));
visit[i][j] = true;

while(!Q.isEmpty()){
Point cur = Q.poll();
for (int w = 0; w < 4; w++) {
int nr = cur.r + dr[w];
int nc = cur.c + dc[w];
if (map[nr][nc]>0 && !visit[nr][nc]){
Q.add(new Point(nr, nc));
visit[nr][nc] = true;
}
}
}
}
}

0 comments on commit df645e2

Please sign in to comment.