Observing Objects

시간 제한2초메모리 제한1024 MB

요약
N×M 격자에서 8방향으로 연결된 같은 색 픽셀 덩어리의 개수를 센다.
난이도

쉬움10점 중 3점

유형
DFS, 그래프
정답자
아직 제출이 없습니다

문제

In image processing fundamentals, determining the region where an object is located is crucial.

You are given a screen of size N×MN\times M, consisting of NMNM pixels. Each pixel at position (i,j)(i,j) has a specific RGB value represented by three integers: R_ijR\_{ij}​ (Red), G_ijG\_{ij}​ (Green), and B_ijB\_{ij}​ (Blue). Each color component is an integer in the \[0,255]\[0,255] range.

The task is to identify the number of distinct objects on the screen. Two pixels are considered part of the same object if their RGB values are identical and connected in one of the 88 possible directions: up, down, left, right, and the 44 diagonals.

An object is defined as a group of connected pixels that meet the above criteria. Write a program to determine the number of distinct objects on the screen.

입력

The first line contains two space-separated integers, NN and MM, denoting the size of the image. (1≤N,M≤1,0001 \le N,M \le 1\\,000)

The following NN lines contain 3NM3NM integers, where each line has 3M3M space-separated integers, denoting the RGB value of each pixel. Here, the 3j3j-th, the (3j+1)(3j+1)-th, and the (3j+2)(3j+2)-th integer of the ii-th row denotes R_ijR\_{ij}​, G_ijG\_{ij}​, and B_ijB\_{ij}​, respectively. (0≤R_ij,G_ij,B_ij≤2550 \le R\_{ij},G\_{ij},B\_{ij} \le 255)

출력

Output the total number of distinct objects in the screen.

예제1

  1. 예제 1

    입력
    3 3
    255 0 0 255 0 0 255 0 0
    255 0 0 0 255 255 0 255 0
    255 0 0 255 0 0 255 0 0
    
    예상 출력
    3