Notice
Recent Posts
Recent Comments
Link
«   2024/10   »
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
Archives
Today
Total
관리 메뉴

코딩블로그

백준 14716 - 현수막 JAVA 실버 1 본문

카테고리 없음

백준 14716 - 현수막 JAVA 실버 1

_hanbxx_ 2024. 3. 24. 16:18
728x90

https://www.acmicpc.net/problem/14716

 

14716번: 현수막

혁진이의 생각대로 프로그램을 구현했을 때, 현수막에서 글자의 개수가 몇 개인지 출력하여라.

www.acmicpc.net

 

전형적인 DFS 문제

import java.util.*;
import java.lang.*;
import java.io.*;

// The main method must be in a class named "Main".
class Main {
    public static int[][] arr;
    public static boolean[][] visited;
    public static int[] dy = {-1,1,0,0,-1,1,-1,1};
    public static int[] dx = {0,0,-1,1,-1,1,1,-1};
    public static int M,N;
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());

        M = Integer.parseInt(st.nextToken());
       N = Integer.parseInt(st.nextToken());

        arr = new int[M][N];
        visited = new boolean[M][N];
        //초기화
        for (int i = 0; i < M; i++) {
            st = new StringTokenizer(br.readLine());
            for (int j = 0; j < N; j++) {
                arr[i][j] = Integer.parseInt(st.nextToken());
            }
        }

        int depth = 0;
        for (int i = 0; i < M; i++) {
            for (int j = 0; j < N; j++) {
                 if (!visited[i][j] && arr[i][j] == 1) {
                     dfs(i,j);
                     depth++;
                 }
               
            }
        }

        System.out.println(depth);
    }
    public static void dfs (int x, int y) {
        visited[x][y] = true;

        for (int i = 0 ; i <8; i++) {
            int tempX = x + dx[i];
            int tempY = y + dy[i];
            if (tempX >= 0 && tempY >=0 && tempX < M &&tempY < N ) {
                if (!visited[tempX][tempY] && arr[tempX][tempY] == 1) {
                    dfs(tempX,tempY);
                }
            }
        }
    }
}

 

+ BFS 풀이

import java.util.*;
import java.lang.*;
import java.io.*;

// The main method must be in a class named "Main".
class Main {
    public static int[][] arr;
    public static boolean[][] visited;
    public static int[] dy = {-1,1,0,0,-1,1,-1,1};
    public static int[] dx = {0,0,-1,1,-1,1,1,-1};
    public static int M,N;
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());

        M = Integer.parseInt(st.nextToken());
        N = Integer.parseInt(st.nextToken());

        arr = new int[M][N];
        visited = new boolean[M][N];
        //초기화
        for (int i = 0; i < M; i++) {
            st = new StringTokenizer(br.readLine());
            for (int j = 0; j < N; j++) {
                arr[i][j] = Integer.parseInt(st.nextToken());
            }
        }

        int depth = 0;
        for (int i = 0; i < M; i++) {
            for (int j = 0; j < N; j++) {
                 if (!visited[i][j] && arr[i][j] == 1) {
                     bfs(i,j);
                     depth++;
                 }
               
            }
        }

        System.out.println(depth);
    }
    public static void bfs (int x, int y) {
        Queue<int[]> q = new LinkedList<>();
        visited[x][y] = true;
        q.offer(new int[]{x,y});

        while(!q.isEmpty()) {
            int[] temp = q.poll();
            int temX = temp[0];
            int temY = temp[1];
            for (int i = 0 ; i < 8; i++) {
                int tempX = temX + dx[i];
                int tempY = temY + dy[i];
                if (tempX >= 0 && tempY >=0 && tempX < M &&tempY < N ) {
                    if (!visited[tempX][tempY] && arr[tempX][tempY] == 1) {
                        visited[tempX][tempY] = true;
                        q.offer(new int[]{tempX,tempY});
                    }
                }
            }
            
        }
    }
}

처음에 계속 시간초과 나다가 이유를 못찾다가 

visited[tempX]tempY] = true를 안해줘서 시간초과가 났다

 

728x90