-
[백준] 14716 현수막ALGORITHM/BOJ 2021. 11. 15. 22:07
https://www.acmicpc.net/problem/14716
2021-11-13
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.LinkedList;import java.util.Queue;import java.util.StringTokenizer;public class Main14716 {public static int N, M, answer;public static int[][] arr;public static int[] dx = {-1, 0, 1, 0, -1, -1, 1, 1};public static int[] dy = {0, -1, 0, 1, -1, 1, -1, 1};public static boolean[][] vtd;public static void solve(int valx, int valy) {Queue<int[]> q = new LinkedList<>();q.add(new int[]{valx, valy});vtd[valx][valy] = true;while(!q.isEmpty()) {int size = q.size();while(size > 0) {int[] tmp = q.poll();int x = tmp[0];int y = tmp[1];for(int i = 0; i < 8; i++) {int nx = x + dx[i];int ny = y + dy[i];if(nx < 0 || ny < 0 || nx >= N || ny >= M || arr[nx][ny] == 0 || vtd[nx][ny]) continue;else {vtd[nx][ny] = true;q.add(new int[]{nx, ny});}}size--;}}}public static void main(String[] args) throws IOException {BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));StringTokenizer st = new StringTokenizer(bf.readLine());N = Integer.parseInt(st.nextToken());M = Integer.parseInt(st.nextToken());answer = 0;arr = new int[N][M];vtd = new boolean[N][M];for(int i = 0; i < N; i++) {st = new StringTokenizer(bf.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(arr[i][j] == 1 && !vtd[i][j]) {solve(i, j);answer++;}}}System.out.println(answer);}}cs #문제풀이
BFS 문제인데 대각선 방향까지 체크하면 된다.
'ALGORITHM > BOJ' 카테고리의 다른 글
[백준] 7568 덩치 (0) 2022.02.02 [백준] 4991 로봇 청소기 (0) 2021.12.11 [백준]1316 그룹 단어 체커 (0) 2021.11.08 [백준] 2941 크로아티아 알파벳 (0) 2021.11.08 [백준] 2467 용액 (0) 2021.11.02