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
- baekjoon 16953
- baekjoon
- dfs
- spring boot
- coding test
- java.lang.nosuchmethoderror
- CodingTest
- bytebuddyinterceptor
- baekjoon 3187
- Error
- 안전영역 java
- springboot
- baekjoon1012
- html error
- baekjoon 4963
- baekjoon 2468
- baekjoon 2667
- 백준 현수막
- 백준 섬의갯수
- BFS
- attvalue
- exprectedbelow
- 백준 2667 java
- websecurityconfiguration
- baekjoon 14714
- beakjoon
- beakjoon 1697
- beakjoon11725
- 단지 번호붙이기
- backjoon 16173
Archives
- Today
- Total
NOT FOUND
[코테] 백준 14716 현수막: DFS Java 본문
https://www.acmicpc.net/problem/14716
이제 DFS는 ez...
내일은 골드 문제 풀어야징
전체 코드
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class baekjoon_14716 {
public static int N, M;
public static int[][] arr;
public static boolean[][] visited;
public static int[] dx = {0, 1, 0, -1, 1, -1, -1, 1};
public static int[] dy = {1, 0, -1, 0, 1, -1, 1, -1};
public static int count = 0;
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());
arr = new int[N][M];
visited = new boolean[N][M];
for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 0; j < M; j++) {
arr[i][j] = Integer.parseInt(st.nextToken());
}
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if(!visited[i][j] && arr[i][j] == 1) {
dfs(i, j);
count++;
}
}
}
System.out.println(count);
}
public static void dfs(int i, int j) {
visited[i][j] = true;
for (int k = 0; k < 8; k++) {
int nx = i + dx[k];
int ny = j + dy[k];
if(nx >= 0 && ny >= 0 && nx < N && ny < M) {
if(!visited[nx][ny] && arr[nx][ny] == 1){
dfs(nx, ny);
}
}
}
}
}
'coding test' 카테고리의 다른 글
DP 기초: 피보나치 수 2748, 1로 만들기 1463 Java (1) | 2025.06.12 |
---|---|
코테 스터디 마무리 및 후원 (0) | 2025.06.10 |
[코테] 백준 16173 점프왕 쩰리: DFS Java (2) | 2025.05.28 |
[코테] 백준 13565 침투: DFS Java (0) | 2025.05.28 |
[코테] 백준 2468 안전 영역: DFS Java (1) | 2025.05.14 |