팡세영
Log sey
팡세영
전체 방문자
오늘
어제
  • 분류 전체보기 (73)
    • PS (45)
      • programmers (13)
      • 백준 (29)
    • Android (16)
    • Daily (0)
    • Kotlin (6)
    • Design Pattern (1)
    • Java (1)
    • Flutter (1)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • java
  • 프로그래머스
  • LEVEL2
  • 완전탐색
  • 문자열
  • 의존성 주입
  • 하단네비게이션바
  • Kotlin
  • ArcitecturePattern
  • compose
  • 이분탐색
  • 안드로이드
  • 정렬
  • mvvm
  • programmers
  • binding
  • 백준
  • 해쉬맵
  • TestCode
  • 골드
  • programmers #프로그래머스
  • Android
  • DFS
  • 구현
  • CustomView
  • BFS
  • 자바
  • 코틀린
  • 실버
  • flutter

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
팡세영

Log sey

[백준] 영상처리 21938
PS/백준

[백준] 영상처리 21938

2022. 11. 11. 17:44

1 0 1
0 1 0
1 0 1


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

 

21938번: 영상처리

화면의 세로 $N$, 가로 $M$ 값이 공백으로 구분되어 주어진다. 두 번째 줄부터 $N + 1$줄까지 $i$번째 가로를 구성하고 있는 픽셀의 $R_{i,j}$, $G_{i,j}$, $B_{i,j}$의 값이 공백으로 구분되어 총 $M$개 주어진

www.acmicpc.net


[예제입력 1]

 

예제 입력 1을 3x3으로 변경하면 아래와 같이 됩니다.

1 0 1
0 1 0
1 0 1

  • R, G, B 의 평균 값을 구해서 경계 값이 넘으면 255로 넘지 않으면 0으로 배열을 초기화 시켜준 후
  • bfs로 상하좌우 탐색을 해서 인접한 구역의 수를 구하면 된다.

[Java]

import java.io.*;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;

public class Main
{
    public static int dx[] = {0, 0, -1, 1};
    public static int dy[] = {-1, 1, 0, 0};
    public static int n, m;
    public static int arr[][];
    public static boolean visited[][];

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        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++){
                int r = Integer.parseInt(st.nextToken());
                int g = Integer.parseInt(st.nextToken());
                int b = Integer.parseInt(st.nextToken());
                arr[i][j] = (r+g+b) / 3;
            }
        }

        int t = Integer.parseInt(br.readLine());

        for(int i=0; i<n; i++){
            for(int j=0; j<m; j++){
                if(t <= arr[i][j]) arr[i][j] = 255;
                else arr[i][j] = 0;
            }
        }

        int ans = 0;
        for(int i=0; i<n; i++){
            for(int j=0; j<m; j++){
                if(arr[i][j] == 255 && !visited[i][j]){
                    ans++;
                    bfs(new Pos(j, i));
                }
            }
        }

        bw.write(ans + "");
        bw.flush();
    }

    public static void bfs(Pos start){
        Queue<Pos> q = new LinkedList<>();
        q.add(start);
        visited[start.y][start.x] = true;

        while(!q.isEmpty()){
            Pos cur = q.poll();

            for(int i=0; i<4; i++){
                int nx = cur.x + dx[i];
                int ny = cur.y + dy[i];

                if(check(nx, ny)) continue;

                visited[ny][nx] = true;
                q.add(new Pos(nx, ny));

            }
        }
    }

    public static boolean check(int nx, int ny){
        return (nx >= m || nx < 0 || ny >= n || ny < 0 ||
                visited[ny][nx] || arr[ny][nx] == 0) ? true : false;
    }
}

class Pos{
    int x, y;

    public Pos(int x, int y) {
        this.x = x;
        this.y = y;
    }
}

'PS > 백준' 카테고리의 다른 글

[백준] - Australian Voting (호주식 투표법) 4419  (0) 2023.02.13
[백준] - 피보나치함수 (1003)  (0) 2022.11.24
[백준] 문자열 잘라내기 2866  (2) 2022.11.10
[백준] - 암기왕 2776  (0) 2022.11.09
[백준] - 키 큰 사람 11292  (0) 2022.11.07
    'PS/백준' 카테고리의 다른 글
    • [백준] - Australian Voting (호주식 투표법) 4419
    • [백준] - 피보나치함수 (1003)
    • [백준] 문자열 잘라내기 2866
    • [백준] - 암기왕 2776
    팡세영
    팡세영
    Android, CS, PS

    티스토리툴바