코드와이
[BAEKJOON] 14502. 연구소 본문
문제링크
14502번: 연구소
인체에 치명적인 바이러스를 연구하던 연구소에서 바이러스가 유출되었다. 다행히 바이러스는 아직 퍼지지 않았고, 바이러스의 확산을 막기 위해서 연구소에 벽을 세우려고 한다. 연구소는 크
www.acmicpc.net
package acmicpc.Gold5;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class 연구소 {
static int n, m, ans;
static int[][] map;
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
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());
}
}
ans = 0;
build(0, 0, 3);
System.out.println(ans);
}
static int[] dr = {-1,1,0,0};
static int[] dc = {0,0,-1,1};
private static void build(int r, int c, int cnt) {
if(cnt == 0) {
for(int i = 0 ; i < n ; i++) {
for(int j = 0 ; j < m ; j++) {
if(map[i][j] == 2) spread(i,j,3);
}
}
ans = Math.max(ans, check());
for(int i = 0 ; i < n ; i++) {
for(int j = 0 ; j < m ; j++) {
if(map[i][j] == 2) spread(i,j,0);
}
}
return;
}
if(r == n - 1 && c == m) return;
if(c == m) {
build(r+1, 0, cnt);
return;
}
if(map[r][c] > 0) {
build(r, c+1, cnt);
} else {
map[r][c] = 1;
build(r, c+1, cnt-1);
map[r][c] = 0;
build(r, c+1, cnt);
}
}
private static void spread(int r, int c, int x) {
int s = x == 3? 0 : 3;
for(int d = 0 ; d < 4 ; d++) {
int nr = r + dr[d];
int nc = c + dc[d];
if(nr < 0 || nc < 0 || nr >= n || nc >= m) continue;
if(map[nr][nc] == s) {
map[nr][nc] = x;
spread(nr,nc,x);
}
}
}
private static int check() {
int sum = 0;
for(int i = 0 ; i < n ; i++) {
for(int j = 0 ; j < m ; j++) {
if(map[i][j] == 0) sum++;
}
}
return sum;
}
}
'acmicpc' 카테고리의 다른 글
[BAEKJOON] 1932. 정수 삼각형 (0) | 2021.03.27 |
---|---|
[BAEKJOON] 17472. 다리 만들기2 (0) | 2021.03.26 |
[BAEKJOON] 2206. 벽 부수고 이동하기 (0) | 2021.03.25 |
[BAEKJOON] 11055. 가장 큰 증가 부분 수열 (0) | 2021.03.25 |
[SW Expert Academy] 3308. 최장 증가 부분 수열 (Hard) (0) | 2021.03.25 |