728x90
21938번: 영상처리
화면의 세로 $N$, 가로 $M$ 값이 공백으로 구분되어 주어진다. 두 번째 줄부터 $N + 1$줄까지 $i$번째 가로를 구성하고 있는 픽셀의 $R_{i,j}$, $G_{i,j}$, $B_{i,j}$의 값이 공백으로 구분되어 총 $M$개 주어진
www.acmicpc.net
주의할 점
255 255 255 || 1 1 1 || 184 298 387
주어진 배열의 크기는 R G B 3색의 픽셀의 값이다.
그러므로 (R+G+B)/3 >= 경계값 일 경우에만 R G B의 픽셀값이 255가 된다.
tmp배열에 받아와서 Change()로 계산해준 뒤, map배열에 NxM 만큼을 할당해줬다.
아마 굳이 map 배열을 새로 생성할 필요는 없을 것 같고, 처음 받아올 때 계산하면 더 효율적일 것 같다.
package 백준renew;
import java.io.*;
import java.util.*;
public class 실버2_21938_영상처리 {
static int N, M;
static int line;
static int tmp[][];
static int map[][];
static boolean visited[][];
static int goX[] = {-1, 1, 0, 0};
static int goY[] = {0, 0, -1, 1};
static class AVI{
int x;
int y;
AVI(int x, int y){
this.x = x;
this.y = y;
}
}
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
tmp = new int[N][M*3];
map = new int[N][M];
visited = new boolean[N][M*3];
for(int i=0; i<N; i++) {
st = new StringTokenizer(br.readLine());
for(int j=0; j<M*3; j++) {
tmp[i][j] = Integer.parseInt(st.nextToken());
}
}
line = Integer.parseInt(br.readLine());
Change();
int cnt = 0;
for(int i=0; i<N; i++) {
for(int j=0; j<M; j++) {
if(map[i][j] == 255 && !visited[i][j]) {
BFS(i, j);
cnt++;
}
}
}
System.out.println(cnt);
}
static void BFS(int x, int y) {
Queue<AVI> queue = new LinkedList<>();
queue.offer(new AVI(x, y));
while(!queue.isEmpty()) {
AVI a = queue.poll();
visited[a.x][a.y] = true;
for(int i=0; i<4; i++) {
int newX = a.x + goX[i];
int newY = a.y + goY[i];
if(newX < 0 || newX >= N || newY <0 || newY >=M || map[newX][newY] == 0 || visited[newX][newY]) {
continue;
}
queue.offer(new AVI(newX, newY));
visited[newX][newY] = true;
}
}
}
static void Change() {
for(int i=0; i<N; i++) {
for(int j=0; j<M*3; j+=3) {
int sum = 0;
for(int k=0; k<3; k++) {
sum += tmp[i][j+k];
}
if(sum/3>=line) {
map[i][j/3] = 255;
}else {
map[i][j/3] = 0;
}
}
}
}
}
728x90
'코테 > Algorithm' 카테고리의 다른 글
[BOJ] 13549: 숨바꼭질3 (JAVA) (0) | 2024.02.23 |
---|---|
[BOJ] 14940: 쉬운 최단거리 (JAVA) (0) | 2024.02.22 |
[BOJ] 14248: 점프점프 (JAVA) (0) | 2024.02.20 |
[BOJ] 17086: 아기상어2 (JAVA) (0) | 2024.02.19 |
[BOJ] 1326: 폴짝폴짝 (JAVA) (0) | 2024.02.19 |