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
- boole_d
- APPENDING
- transporting
- Union-Find
- SAP GUI
- changing value
- DP - 유한배낭
- batch job
- SM36
- 필드카탈로그
- ALV Output Setting
- BOJ_Gold
- using value
- 날짜 계산 함수
- qfieldname
- CTS #CTS 이관 #SAP #ABAP
- DP - 무한배낭
- READ TABLE
- DP - 무한배낭(순서)
- cfieldname
- Dictionary Search Help
- ABAP
- ZPL
- MONAT_F4
- APPENDING CORRESPONDING
- Data Browser
- java
- FOR ALL ENTRIES IN
- NEW-PAGE PRINT ON
- SAP
Archives
- Today
- Total
Jin's Library
[Gold Ⅴ] 7576 - 토마토 본문
package BOJ.Gold;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.LinkedList;
import java.util.Queue;
// 골드 5 토마토
// BFS
public class BOJ_7576 {
static int M,N;
static int[][] box;
static int[] dx = {0, -1, 0, 1};
static int[] dy = {-1, 0, 1, 0};
static Queue<int[]> Q = new LinkedList<int[]>();
static void BFS(){
while(!Q.isEmpty()){
int[] tmt = Q.poll();
int x = tmt[0];
int y = tmt[1];
for(int i=0;i<4;i++){
int cx = x + dx[i];
int cy = y + dy[i];
if(cx>=0 && cy>=0 && cx<N && cy<M){
if(box[cx][cy]==0){
box[cx][cy] = box[x][y]+1;
Q.offer(new int[] {cx, cy});
}
}
}
}
}
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
M = Integer.parseInt(st.nextToken());
N = Integer.parseInt(st.nextToken());
box = new int[N][M];
for(int i=0;i<N;i++){
st = new StringTokenizer(br.readLine());
for(int j=0;j<M;j++){
int p = Integer.parseInt(st.nextToken());
if(p == 1) Q.offer(new int[] {i,j});
box[i][j] = p;
}
}
BFS();
boolean flag = true;
int MAX = 1;
for(int i =0; i<N;i++){
for(int j =0;j<M;j++){
if(box[i][j] == 0){
flag = false;
}
MAX = Math.max(MAX, box[i][j]);
}
}
int answer = 0 ;
if(flag) answer = MAX-1;
else answer = -1;
// System.out.println(Arrays.deepToString(box));
System.out.println(answer);
}
}'Algorithm - Java > BOJ - Gold' 카테고리의 다른 글
| [Gold Ⅳ] 16120 - PPAP (0) | 2025.09.29 |
|---|---|
| [Gold Ⅳ] 9935 - 문자열 폭발 (0) | 2025.09.29 |
| [Gold Ⅴ] 7569 - 토마토 (0) | 2025.09.29 |
| [Gold Ⅴ] 5430 - AC (0) | 2025.09.29 |
| [Gold Ⅳ] 5427 - 불 (0) | 2025.09.29 |