C Program Shortest Distance in Matrix

C Program Shortest Distance in Matrix


#include <stdio.h>

/* 求n的絕對值 */
int abs(int n) {
    return n > 0 ? n : -n;
}

int main(int argc, const char * argv[]) {
    int N, x, y;
    int row, column;

    printf("Dimension -> "); scanf("%d", &N);
    printf("Point -> "); scanf("%d %d", &x, &y);
    x--;
    y--;
    for (row = 0; row < N; row++) {
        for (column = 0; column < N; column++) {
            // Shortest distance from (row, column) to (x, y)
            printf("%3d ", abs(row - x) + abs(column - y));
        }
        printf("\n");
    }
    return 0;
}


Learn More :