본문 바로가기

Algorithm/Code Fights (Code Signal)

Aracade Intro #23 boxBlur. Algorithm, 알고리즘, Codefights, CodeSignal, regx, 정규표현식, 코드파이트, 코드시그널, c++ java c# scalar

Aracade Intro #23 boxBlur. Algorithm, 알고리즘, Codefights, CodeSignal, 코드파이트, 코드시그널, c++ java c# scalar



Q.


Last night you partied a little too hard. Now there's a black and white photo of you that's about to go viral! You can't let this ruin your reputation, so you want to apply the box blur algorithm to the photo to hide its content.


The pixels in the input image are represented as integers. The algorithm distorts the input image in the following way: Every pixel x in the output image has a value equal to the average value of the pixel values from the 3 × 3 square that has its center at x, including x itself. All the pixels on the border of x are then removed.


Return the blurred image as an integer, with the fractions rounded down.



 어젯밤에 넌 너무 빡쎄게 놀았다. 지금 이런저런 너의 사진들이 인터넷에 퍼지려고 한다. 네 명성에 흠이 가는걸 내비둘수는 없고, 넌 블러처리 알고리듬을 통해 사진을 숨기려한다.


 이미지의 픽셀들은 정수로 되어있다. 알고리즘은 뒤의 방법으로 화면을 뒤틀 것이다. 모든 출력 이미지의 픽셀 x 는 3x3 의 평균 값을 가지고 있다. 3x3 사각형의 센터는 픽셀 x가 되는 것이고 평균 값에는 x 자신의 값도 포함된다. 모든 모서리부분의 픽셀 x 는 없어진다 (사이즈가 줄어드는 것)


 정수로써의 블러처리된 이미지를 반환해라, (반환시에는 소수점 반올림)


e.g.

Input -> image = [[1, 1, 1], 

                         [1, 7, 1], 

                         [1, 1, 1]]


Output -> boxBlur(image) = [[1]].


To get the value of the middle pixel in the input 3 × 3 square: (1 + 1 + 1 + 1 + 7 + 1 + 1 + 1 + 1) = 15 / 9 = 1.66666 = 1. The border pixels are cropped from the final result.



Input -> image =   [[7, 4, 0, 1], 

         [5, 6, 2, 2], 

         [6, 10, 7, 8], 

         [1, 4, 2, 0]]


Output -> boxBlur(image) = [[5, 4], 

                                      [4, 4]]


There are four 3 × 3 squares in the input image, so there should be four integers in the blurred output. To get the first value: (7 + 4 + 0 + 5 + 6 + 2 + 6 + 10 + 7) = 47 / 9 = 5.2222 = 5. The other three integers are obtained the same way, then the surrounding integers are cropped from the final result.




//Process

//1. Input image (2d vector)

//2. Iterate rows ( 1 ~ size - 1 )

// 2.1. Iterate columns ( 1 ~ size - 1)

//  2.1.1. Get sum and average of 3x3 around that index value

//  2.1.2. Put average to answer vector

//3. Return answerVector



//처리과정

//1. 2d 배열 이미지를 입력받는다.

//2. 행을 반복한다. (모서리 제외)

// 2.1. 열을 반복한다. (양끝 모서리 제외)

//  2.1.1. 해당 순서 인덱스의 3x3 값의 합과 평균을 구한다.

//  2.1.2. 평균을 리턴이미지에 넣는다.

//3. 리턴이미지를 반환한다.



코드.. 코드를 보자!!!!!!


Code.. lemme see code!!!!!!!!!




std::vector<std::vector<int>> boxBlur(std::vector<std::vector<int>> image) { 

    

    int sum;

    int length1 = image.size() - 1;

    int length2 = image[0].size() - 1;

    

    std::vector<std::vector<int>> answer(length1 - 1, std::vector<int>(length2 - 1));

    for (int i = 1; i < length1; ++i) 

    {

        for (int j = 1; j < length2; ++j) 

        {

            //int k = i-1;

            sum = image[i-1][j-1] + image[i-1][j] + image[i-1][j+1]

                 + image[i][j-1] + image[i][j] + image[i][j+1]

                 + image[i+1][j-1] + image[i+1][j] + image[i+1][j+1];

            

            answer[i-1][j-1] = sum/9;

        }

    }

    return answer;

}




Something else....




2018/10/29 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #22 avoidObstacles. Algorithm, 알고리즘, Codefights, CodeSignal, regx, 정규표현식, 코드파이트, 코드시그널, c++ java c# scalar

2018/10/28 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #21 isIPv4Address. Algorithm, 알고리즘, Codefights, CodeSignal, regx, 정규표현식, 코드파이트, 코드시그널, c++ java c# scalar

2018/10/28 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #20 arrayMaximalAdjacentDifference. Algorithm, Codefights, CodeSignal, 코드파이트, 코드시그널, c++ java c# scalar

2018/10/27 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #19 AreEquallyStrong. Algorithm, Codefights, CodeSignal, 코드파이트, 코드시그널, c++ java c# scalar

2018/10/24 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #18 PalindromeRearranging, Codefights, CodeSignal, 코드파이트, 코드시그널, c++ java c# scalar

2018/10/24 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #17 ArrayChange, Codefights, CodeSignal, 코드파이트, 코드시그널, c++ java c# scalar

2018/10/21 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #16 AreSimilar?, Codefights, CodeSignal, 코드파이트, 코드시그널, c++ java c# scalar

2018/10/21 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #15 AddBorder, Codefights, CodeSignal, 코드파이트, 코드시그널, c++ java c# scalar

2018/10/20 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #14 AlternatingSums, Codefights, CodeSignal

2018/10/19 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #13 ReverseParentheses, Codefights, CodeSignal

2018/10/08 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #12 SortByHeight

2018/10/06 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #11 IsLucky

2018/10/03 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #10 CommonCharacterCount

2018/09/26 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #9 AllLongestStrings

2018/09/24 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #8 MatrixElementsSum

2018/09/23 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #7 AlmostIncreasingSequence

2018/09/22 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #6 MakeArrayConsecutive2

2018/09/21 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #5 ShapeArea

2018/09/19 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #4 AdjacentElementsProduct

2018/09/16 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #3 CheckPalindrome

2018/09/16 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #2 CenturyFromYear

2018/09/16 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #1 Add