Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[황정훈] 치즈 #188

Merged
merged 1 commit into from
Nov 19, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions solution/BOJ_2636_치즈/황정훈.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
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 R, C, T = 0, pred = 0, remain = 0;
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;
}
}

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

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

do {
remain = melt();
pred = predict();
T++;
}while(remain > pred);

bw.write(T+"\n"+remain+"\n");
bw.flush();
bw.close();
br.close();
}
static int predict(){
boolean[][] visit = new boolean[R][C];
Queue<Point> Q = new ArrayDeque<>();
Q.add(new Point(0, 0));
visit[0][0] = true;
int tmp = 0;

while(!Q.isEmpty()){
Point cur = Q.poll();
for (int i = 0; i < 4; i++) {
int nr = cur.r + dr[i];
int nc = cur.c + dc[i];
if(0<=nr && nr<R && 0<=nc && nc<C && !visit[nr][nc]){
visit[nr][nc] = true;
if(map[nr][nc]==0) Q.add(new Point(nr, nc));
else if(map[nr][nc]==1){
map[nr][nc] = 2;
tmp++;
}
}
}
}
return tmp;
}
static int melt(){
int tmp = 0;
for (int i = 0; i < R; i++) {
for (int j = 0; j < C; j++) {
if (map[i][j]==2) map[i][j] = 0;
else if (map[i][j]==1) tmp++;
}
}
return tmp;
}
}