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
- Data Browser
- java
- NEW-PAGE PRINT ON
- ABAP
- 날짜 계산 함수
- ZPL
- changing value
- 필드카탈로그
- boole_d
- BOJ_Gold
- using value
- READ TABLE
- DP - 유한배낭
- CTS #CTS 이관 #SAP #ABAP
- SAP GUI
- SAP
- Dictionary Search Help
- Union-Find
- ALV Output Setting
- batch job
- qfieldname
- transporting
- cfieldname
- MONAT_F4
- DP - 무한배낭(순서)
- SM36
- FOR ALL ENTRIES IN
- APPENDING
- APPENDING CORRESPONDING
- DP - 무한배낭
Archives
- Today
- Total
Jin's Library
[Silver Ⅱ] 1012 - 유기농 배추 본문
package BOJ.Silver;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
// 실버 2 유기농 배추
// BFS
public class BOJ_1012 {
static int N,M,W,cnt;
static int[][] farm;
static boolean[][] visit;
static int[] dx = {0, -1, 0, 1};
static int[] dy = {1, 0, -1, 0};
static void BFS(int x, int y){
Queue<int[]> Q = new LinkedList<int[]>();
Q.offer(new int[] {x,y});
while(!Q.isEmpty()){
x = Q.peek()[0];
y = Q.peek()[1];
visit[x][y] = true;
Q.poll();
for(int i=0; i<4; i++){
int cx = x + dx[i];
int cy = y + dy[i];
if(cx >= 0 && cy >= 0 && cx < M && cy < N){
if(!visit[cx][cy] && farm[cx][cy] == 1){
Q.offer(new int[] {cx,cy});
visit[cx][cy] = true;
}
}
}
}
}
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine());
StringTokenizer st;
while(T-->0){
st = new StringTokenizer(br.readLine());
M = Integer.parseInt(st.nextToken());
N = Integer.parseInt(st.nextToken());
W = Integer.parseInt(st.nextToken());
farm = new int[M][N];
visit = new boolean[M][N];
cnt = 0;
for(int i=0;i<W;i++){
st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
farm[a][b] = 1;
}
for(int i=0;i<M;i++){
for(int j=0;j<N;j++){
if(farm[i][j] == 1 && !visit[i][j]){
BFS(i, j);
cnt++;
}
}
}
System.out.println(cnt);
}
}
}'Algorithm - Java > BOJ - Silver' 카테고리의 다른 글
| [Silver Ⅳ] 18258 - 큐 2 (0) | 2025.09.30 |
|---|---|
| [Silver Ⅴ] 1316 - 그룹 단어 체커 (0) | 2025.09.30 |
| [Silver Ⅳ] 1302 - 베스트셀러 (0) | 2025.09.30 |
| [Silver I] 1283 - 단축키 지정 (0) | 2025.09.30 |
| [Silver Ⅲ] 7795 - 먹을 것인가 먹힐 것인가 (0) | 2025.09.29 |