Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
Tags
- 투포인터
- 문제해결
- BOJ
- math
- binary search
- DP
- MySQL
- 러닝일지
- 백준
- 문제풀이
- 알고리즘
- 코딩테스트
- dfs
- COS PRO
- 시뮬레이션
- BFS
- 이분탐색
- db
- 프로그래머스
- 건강
- 운동기록
- greedy
- SWEA
- 문자열
- oracle
- SQL
- 자바
- Java
- 코테
- priorityqueue
Archives
- Today
- Total
슈콩
[BOJ] 백준 14502 연구소 본문
[문제]
https://www.acmicpc.net/problem/14502
[소스 코드]
import java.io.*;
import java.util.*;
public class Main {
static int n,m;
static int[][] map;
static int result = 0;
static int[] dr = {-1,1,0,0};
static int[] dc = {0,0,-1,1};
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer 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());
}
}
select(0);
System.out.println(result);
}
private static void select(int cnt) {
if(cnt==3) {
int[][] copy = new int[n][m];
for(int i=0;i<n;i++) {
copy[i] = map[i].clone();
}
bfs(copy);
return;
}
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
if(map[i][j]==0) {
map[i][j] = 1;
select(cnt+1);
map[i][j] = 0;
}
}
}
}
private static void bfs(int[][] copy) {
Queue<int[]> q = new LinkedList<>();
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
if(copy[i][j]==2) {
q.offer(new int[] {i,j});
}
}
}
while(!q.isEmpty()) {
int[] curr = q.poll();
for(int d=0;d<4;d++) {
int nr = curr[0] + dr[d];
int nc = curr[1] + dc[d];
if(nr>=0 && nr<n && nc>=0 && nc<m && copy[nr][nc]==0) {
copy[nr][nc] = 2;
q.offer(new int[] {nr,nc});
}
}
}
int cnt = 0;
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
if(copy[i][j]==0) {
cnt++;
}
}
}
result = Math.max(result, cnt);
}
}'Algorithms > Baekjoon' 카테고리의 다른 글
| [BOJ] 백준 18869 멀티버스 Ⅱ (0) | 2025.09.20 |
|---|---|
| [BOJ] 백주 1822 차집합 (0) | 2025.09.19 |
| [BOJ] 백준 14499 주사위 굴리기 (0) | 2025.09.19 |
| [BOJ] 백준 16401 과자 나눠주기 (0) | 2025.09.18 |
| [BOJ] 백준 14921 용액 합성하기 (0) | 2025.09.18 |