Untitled

mail@pastecode.io avatar
unknown
c_cpp
14 days ago
2.2 kB
4
Indexable
Never
#include <stdio.h>

int main() {
    // Read in the arguments
    int L, D;
    scanf("%d %d", &L, &D);

    // Loop through all the i,j pair of the output, print the grid one by one
    for (int i = 0; i < 2*L-1; i++) {
        for (int j = 0; j < 2*L-1; j++) {

            // Caculate the absolute value in distance of i
            int difference_in_i = i - (L-1);
            if (difference_in_i < 0)
                difference_in_i *= -1;

            // Caculate the absoulte value in distance of j
            int difference_in_j = j - (L-1);
            if (difference_in_j < 0)
                difference_in_j *= -1;

            // Calculate manhhaten distance
            int distance2middle = difference_in_i + difference_in_j;

            // We start from observing the middle point, we want to know what is the mapping 
            // between Depth to the alphabet.
            // This is a example:
            // L = 6                V (L-1)
            // Depth    : 0 1 2 3 4 5 6 7 8 9 10
            // alpha    : A B C D E F E D C B A

            // We discovered that we can use the distance to the middle point,
            // to calculate what alphabet we should print in the middle.

            // alpha    : A B C D E F E D C B A
            // distance : 5 4 3 2 1 0 1 2 3 4 5

            // So the distance should be calcuated like this:
            int distance = D - (L-1);
            if (distance < 0)
                distance *= -1; // Taking absolute value

            // If the value is out of the range (Out of the crystal, the alphabet is not between A to Z),
            // then we should print a space.
            
            // A brief explaination of what we've caculated is as below:
            // (L-1-distance): The shift of the middle alphabet
            // distance2middle: The shift of the alphabet of current grid
            if ((L-1-distance) - distance2middle >= 0)
                printf("%c", 'A' + (L-1-distance) - distance2middle);
            else
                printf(" ");
        }
        // A new line
        printf("\n");
    }

    return 0;
}
Leave a Comment