1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
#include <iostream>
/* В това решение използваме int за размери, вместо unsigned int, защото
* правим изваждания, които се обработват по-лесно като можеш да имаш отрицателни числа.
* Може да се преработи с unsigned int (и окуражавам да го направите), но кода става
* по-досаден и грозен.
*/
const int SIZE = 10;
int clamp(int value, int min, int max) {
if (value < min) return min;
if (value > max) return max;
return value;
}
void sumCells(const unsigned int mat[SIZE][SIZE],
int startRow, int endRow,
int startCol, int endCol,
unsigned int &sum, unsigned int &count)
{
startRow = clamp(startRow, 0, SIZE - 1);
endRow = clamp(endRow, 0, SIZE - 1);
startCol = clamp(startCol, 0, SIZE - 1);
endCol = clamp(endCol, 0, SIZE - 1);
for (int row = startRow; row <= endRow; row++) {
for (int col = startCol; col <= endCol; col++) {
sum += mat[row][col];
count++;
}
}
}
double averageCells(const unsigned int mat[SIZE][SIZE], int row, int col, unsigned int radius) {
unsigned int sum = 0, sumCount = 0;
sumCells(mat, row - radius, row + radius, col - radius, col + radius, sum, sumCount);
return (double)sum / sumCount;
}
int main() {
unsigned int mat[SIZE][SIZE];
for (int i = 0; i < SIZE; i++)
for (int j = 0; j < SIZE; j++)
std::cin >> mat[i][j];
unsigned int radius;
std::cin >> radius;
double blurred[SIZE][SIZE] = { 0 };
for (int i = 0; i < SIZE; i++)
for (int j = 0; j < SIZE; j++)
blurred[i][j] = averageCells(mat, i, j, radius);
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++)
std::cout << blurred[i][j] << ' ';
std::cout << std::endl;
}
return 0;
}
|