Skip to content

Instantly share code, notes, and snippets.

@anuar2k
Created May 4, 2020 14:14
Show Gist options
  • Save anuar2k/d237710912e56c9d26c587acd35f4f35 to your computer and use it in GitHub Desktop.
Save anuar2k/d237710912e56c9d26c587acd35f4f35 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
int main() {
int n;
scanf("%d", &n);
int **T = malloc(n * sizeof(*T));
int **W = malloc(n * sizeof(*W));
int *memBlockT = malloc(n * n * sizeof(*memBlockT));
int *memBlockW = malloc(n * n * sizeof(*memBlockW));
for (int i = 0; i < n; i++) {
T[i] = memBlockT + i * n;
W[i] = memBlockW + i * n;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &T[i][j]);
}
}
int dX[] = {-1, -1, -1, 0, 1, 1, 1, 0};
int dY[] = {-1, 0, 1, 1, 1, 0, -1, -1};
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
int result = 0;
for (int d = 0; d < 8; d++) {
if (i + dX[d] >= 0 && i + dX[d] < n && j + dY[d] >= 0 && j + dY[d] < n) {
result += T[i + dX[d]][j + dY[d]];
}
}
W[i][j] = result;
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
printf("%d ", W[i][j]);
}
printf("\n");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment