I have a matrix of numbers and want to find the distance of each item from its farthest non-zero neighbour (in four directions). I came up with this idea
#include <stdlib.h>
#include <stdio.h>
int min(int a, int b, int c, int d)
{
    int e = a < b ? a : b;
    int f = c < d ? c : d;
    int r = e < f ? e : f;
    return r;
}
int main()
{
    int width = 50;
    int height = 50;
    int points[width][height];
    int distances[width][height][5]; // 0 left 1 right 2 bottom 3 top 4 min
    // adding some random values, zero and non-zero
    for (int y = 0; y < height; y++)
    {
        for (int x = 0; x < width; x++)
        {
            points[x][y] = rand() % 100;
        }
    }
    // scanning in four direction to check if the previous neighbour exists
    for (int y = 0; y < height; y++)
    {
        for (int x = 0; x < width; x++)
        {
            if (points[x][y] > 0)
            {
                distances[x][y][0] = distances[x - 1][y][0] > 0 ? distances[x - 1][y][0] + 1 : 1;
            }
        }
        for (int x = width - 1; x >= 0; x--)
        {
            if (points[x][y] > 0)
            {
                distances[x][y][1] = distances[x + 1][y][1] > 0 ? distances[x + 1][y][1] + 1 : 1;
            }
        }
    }
    for (int x = 0; x < width; x++)
    {
        for (int y = 0; y < height; y++)
        {
            if (points[x][y] > 0)
            {
                distances[x][y][2] = distances[x][y - 1][2] > 0 ? distances[x][y - 1][2] + 1 : 1;
            }
        }
        for (int y = height - 1; y >= 0; y--)
        {
            if (points[x][y] > 0)
            {
                distances[x][y][3] = distances[x][y + 1][3] > 0 ? distances[x][y + 1][3] + 1 : 1;
            }
        }
    }
    // finding the minimum of four distances
    for (int y = 0; y < height; y++)
    {
        for (int x = 0; x < width; x++)
        {
            if (points[x][y] > 0)
            {
                distances[x][y][4] = min(distances[x][y][0], distances[x][y][1], distances[x][y][2], distances[x][y][3]);
                printf("%d %d %d %d %d %d %d \n", x, y, distances[x][y][0], distances[x][y][1], distances[x][y][2], distances[x][y][3], distances[x][y][4]);
            }
        }
    }
    return 0;
}
but it doesn't work as expected. Most likely, I have made a stupid mistake and have a blind eye for that to see.
 
     
    