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
- greedy
- 이분탐색
- 투포인터
- 문제풀이
- 건강
- 코딩테스트
- DP
- COS PRO
- 코테
- 운동기록
- math
- SQL
- SWEA
- db
- 알고리즘
- binary search
- Java
- 문자열
- BFS
- 프로그래머스
- MySQL
- dfs
- 자바
- 시뮬레이션
- priorityqueue
- 러닝일지
- oracle
- 백준
Archives
- Today
- Total
슈콩
COS PRO 1급 JAVA 꽃피는 봄이 언제 오나요 본문




[소스 코드]
import java.util.*;
class Main {
public int[] dr = {-1,1,0,0};
public int[] dc = {0,0,-1,1};
public int solution(int n, int[][] garden) {
int answer = 0;
Queue<int[]> q = new LinkedList<>();
boolean[][] visit = new boolean[n][n];
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(garden[i][j]==1){
q.offer(new int[]{i,j});
visit[i][j] = true;
}
}
}
while(!q.isEmpty()){
boolean check = false;
int size = q.size();
for(int i=0;i<size;i++){
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>=n || visit[nr][nc]) continue;
visit[nr][nc] = true;
q.offer(new int[]{nr,nc});
check = true;
}
}
if(check) answer++;
}
return answer;
}
}