BUPT-homework/semester1/pset11/2-3-MatrixAdd.c

36 lines
738 B
C
Raw Permalink Normal View History

2021-12-02 21:04:43 +08:00
#include <stdio.h>
2021-12-05 09:40:57 +08:00
#pragma GCC optimize("O2")
2021-12-02 21:04:43 +08:00
#define MAX 10
int matrixAdd(int matrix[][MAX], int m, int n, int x, int y);
int main(void) {
int m, n;
scanf("%d%d", &m, &n);
int matrix[m][MAX];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &matrix[i][j]);
}
}
int x, y;
scanf("%d%d", &x, &y);
printf("%d\n", matrixAdd(matrix, m, n, x - 1, y - 1));
return 0;
}
int matrixAdd(int matrix[][MAX], int m, int n, int x, int y) {
int sum = 0;
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
if (i + x >= 0 && i + x < m && j + y >= 0 && j + y < n &&
!(i == j && i == 0)) {
sum += matrix[i + x][j + y];
}
}
}
return sum;
}