Medium

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.

Example

For

image = [[1, 1, 1],

                   [1, 7, 1],

                   [1, 1, 1]]

the output should be 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.

For

image = [[7, 4, 0, 1],

                   [5, 6, 2, 2],

                 [6, 10, 7, 8],

                   [1, 4, 2, 0]]

the output should be

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.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] array.array.integer image

    An image, stored as a rectangular matrix of non-negative integers.

    Guaranteed constraints:
    3 ≤ image.length ≤ 100,
    3 ≤ image[0].length ≤ 100,
    0 ≤ image[i][j] ≤ 255.

  • [output] array.array.integer

    • A blurred image represented as integers, obtained through the process in the description.

[C] Syntax Tips

// Prints help message to the console
// Returns a string
char * helloWorld(char * name) {
    char * answer = malloc(strlen(name) + 8);
    printf("This prints to the console when you Run Tests");
    strcpy(answer, "Hello, ");
    strcat(answer, name);
    return answer;
}

더보기

Solution

// Arrays are already defined with this interface:
// typedef struct arr_##name {
//   int size;
//   type *arr;
// } arr_##name;
//
// arr_##name alloc_arr_##name(int len) {
//   arr_##name a = {len, len > 0 ? malloc(sizeof(type) * len) : NULL};
//   return a;
// }
//
//
arr_arr_integer boxBlur(arr_arr_integer image)
{
	arr_arr_integer returnBox=alloc_arr_arr_integer(image.size-2);
	for(int i=0;i<returnBox.size;i++)
		returnBox.arr[i]=alloc_arr_integer(image.arr[0].size-2);

	for(int i=0;i<returnBox.size;i++)
		for(int j=0;j<returnBox.arr[0].size;j++)
			returnBox.arr[i].arr[j]=0;

	for(int i=1;i<image.size-1;i++)
		for(int j=1;j<image.arr[0].size-1;j++)
		{
			for(int k=-1;k<=1;k++)
				for(int l=-1;l<=1;l++)
					returnBox.arr[i-1].arr[j-1]+=image.arr[i+k].arr[j+l];
			returnBox.arr[i-1].arr[j-1]/=9;
		}

	return returnBox;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Array Replace  (0) 2020.04.05
<Codesignal> Minesweeper  (0) 2020.04.05
<Codesignal> avoidObstacles  (0) 2020.04.05
<Codesignal> isIPv4Address  (0) 2020.04.05
<Codesignal> arrayMaximalAdjacentDifference  (0) 2020.04.05

+ Recent posts