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

[양은서] 치즈 #182

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
91 changes: 91 additions & 0 deletions solution/BOJ_2636_치즈/yess.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
```
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {
static int m ;
static int n ;
static int [][] map;
static boolean [][]check;
static int [] dx = {1,-1,0,0};
static int [] dy = {0,0,1,-1};
static int answer = 0 ;
static int cnt = 0 ;
static int left = 0 ;
public static void main(String [] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
m = Integer.parseInt(st.nextToken());
n = Integer.parseInt(st.nextToken());
map = new int [m][n];
for(int i = 0 ; i < m ; i ++) {
st = new StringTokenizer(br.readLine());
for(int j = 0 ; j < n ; j ++) {
map[i][j] = Integer.parseInt(st.nextToken());
if (map[i][j] == 1) cnt ++;
}
}
while (cnt > 0 ) {
left = cnt;

check = new boolean[m][n];
checkOutside(0,0);
check = new boolean [m][n];
for(int i = 1; i < m ; i ++) {
for(int j = 1; j < n ; j ++) {
if (map[i][j] == 1 && check [i][j] == false) {
checkOutsideCheese(i,j);
}
}
}
for(int i = 0 ; i < m ; i ++) {
for(int j = 0 ; j < n ; j ++) {
map[i][j] = map[i][j] == 3 ? 2 : map[i][j];
}
}
answer ++;
}
System.out.println(answer);
System.out.println(left);
}

static void checkOutsideCheese(int x, int y ) {
check[x][y] = true ;
if (isitRealOutside(x,y) == true )
{
cnt --;
map[x][y] = 3;
}
for(int i = 0 ; i < dx.length; i ++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (nx < 0 || ny < 0 || nx >= m || ny >= n) continue;
if (map[nx][ny] != 1 || check[nx][ny] == true) continue;
checkOutsideCheese(nx, ny);
}
}
static boolean isitRealOutside(int x, int y ) {
for(int i = 0 ; i < 4; i ++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (map[nx][ny] == 2) return true;
}
return false;
}

static void checkOutside(int x, int y ) {
check[x][y] = true ;
map[x][y] = 2;
for(int i = 0 ; i < dx.length; i ++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (nx < 0 || nx >= m || ny < 0 || ny >= n) continue;
if (map[nx][ny] == 1 || check[nx][ny] == true ) continue;
checkOutside(nx,ny);
}
}
}

```