#54134: EM LAST


st2037454@gmail.com (章雋輝)


#include <stdio.h>

#define MAX_N 100

// Helper to copy matrix B into matrix A
void copyMatrix(int n, int dest[MAX_N][MAX_N], int src[MAX_N][MAX_N]) {
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            dest[i][j] = src[i][j];
        }
    }
}

// A: Rotate Counter-Clockwise
void rotateCCW(int n, int mat[MAX_N][MAX_N]) {
    int temp[MAX_N][MAX_N];
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            temp[i][j] = mat[j][n - 1 - i];
        }
    }
    copyMatrix(n, mat, temp);
}

// B: Rotate Clockwise
void rotateCW(int n, int mat[MAX_N][MAX_N]) {
    int temp[MAX_N][MAX_N];
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            temp[i][j] = mat[n - 1 - j][i];
        }
    }
    copyMatrix(n, mat, temp);
}

// C: Flip Horizontally
void flipHorizontal(int n, int mat[MAX_N][MAX_N]) {
    int temp[MAX_N][MAX_N];
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            temp[i][j] = mat[i][n - 1 - j];
        }
    }
    copyMatrix(n, mat, temp);
}

// D: Flip Vertically
void flipVertical(int n, int mat[MAX_N][MAX_N]) {
    int temp[MAX_N][MAX_N];
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            temp[i][j] = mat[n - 1 - i][j];
        }
    }
    copyMatrix(n, mat, temp);
}

// Struct to store operations
typedef struct {
    char type;
    int count;
} Operation;

int main() {
    int num_ops_groups;
    
    // 1. Parse number of operation groups
    if (scanf("%d", &num_ops_groups) != 1) return 0;

    Operation ops[100]; // Assuming reasonable max number of operations
    
    // 2. Parse the operations (e.g., A 2, C 1)
    for (int i = 0; i < num_ops_groups; i++) {
        // Space before %c tells scanf to skip whitespace/newlines
        scanf(" %c%d", &ops[i].type, &ops[i].count);
    }

    // 3. Parse Matrix size and data
    int N;
    scanf("%d", &N);
    
    int mat[MAX_N][MAX_N];
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            scanf("%d", &mat[i][j]);
        }
    }

    // 4. Process Operations
    for (int i = 0; i < num_ops_groups; i++) {
        char type = ops[i].type;
        int count = ops[i].count;

        // Optimization: 
        // Rotations repeat every 4 (e.g., 5 rotations = 1 rotation)
        // Flips repeat every 2
        if (type == 'A' || type == 'B') count %= 4;
        else count %= 2;

        for (int k = 0; k < count; k++) {
            switch (type) {
                case 'A': rotateCCW(N, mat); break;
                case 'B': rotateCW(N, mat); break;
                case 'C': flipHorizontal(N, mat); break;
                case 'D': flipVertical(N, mat); break;
            }
        }
    }

    // 5. Output Result
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            printf("%d", mat[i][j]);
            if (j < N - 1) printf(" ");
        }
        printf("\n");
    }

    return 0;
}