Problem Description
KiKi has a matrix and wants to determine the matrix after performing k row or column swap operations. Please write a program to solve this.
Input Description:
- The first line contains two integers
nandm(separated by a space), representing a matrix withnrows andmcolumns (1 ≤ n ≤ 10, 1 ≤ m ≤ 10). - From line 2 to line
n+1, each line inputsmintegers (ranging from -2³¹ to 2³¹-1) separated by spaces. A total ofn*mnumbers are input, representing the elements of the initial matrix. - Next, input a integer
k(1 ≤ k ≤ 5), indicating the number of operations. Then, forklines, each line contains a charactertand two numbersaandb(separated by spaces).trepresents the operation:'r'for row swap,'c'for column swap.aandbare the rows/columns to swap (1 ≤ a ≤ b ≤ n for rows, 1 ≤ a ≤ b ≤ m for columns). - Hint: Ignore operations if
tis not'r'or'c'.
Output Description:
Output the matrix after swaps, with n rows and m columns. Each number is followed by a space.
Examples:
Example 1:
Input:
2 2
1 2
3 4
1
r 1 2
Output:
3 4
1 2
Example 2:
Input:
2 2
1 3
6 8
2
c 1 2
t 1 2
Output:
3 1
8 6
Explanatino:
When t is not 'r' or 'c', the operation is ignored. Thus, only the 1st and 2nd columns of the original matrix are swapped.
Thought Process
The key challenge is performing row and column swaps. For a row swap (when t = 'r'), we fix the rows and swap the elements of each column between the two rows. For a column swap (when t = 'c'), we fix the columns and swap the elements of each row between the two columns.
Row Swap (t = 'r'):
Loop through each column, and swap the elements at (a-1, col) and (b-1, col).
Column Swap (t = 'c'):
Loop through each row, and swap the elements at (row, a-1) and (row, b-1).
Code (Rewritten in C)
#include <stdio.h>
int main() {
int rowCount, colCount;
scanf("%d %d", &rowCount, &colCount); // Read matrix dimensions
int matrix[10][10]; // Max size is 10x10 as per problem constraints
for (int i = 0; i < rowCount; ++i) {
for (int j = 0; j < colCount; ++j) {
scanf("%d", &matrix[i][j]); // Read matrix elements
}
}
int numOps;
scanf("%d", &numOps); // Read number of operations
for (int op = 0; op < numOps; ++op) {
char opType;
int a, b;
scanf(" %c %d %d", &opType, &a, &b); // Read operation type and indices (skip whitespace)
if (opType == 'r') {
// Swap rows (a-1) and (b-1)
for (int col = 0; col < colCount; ++col) {
int temp = matrix[a-1][col];
matrix[a-1][col] = matrix[b-1][col];
matrix[b-1][col] = temp;
}
} else if (opType == 'c') {
// Swap columns (a-1) and (b-1)
for (int row = 0; row < rowCount; ++row) {
int temp = matrix[row][a-1];
matrix[row][a-1] = matrix[row][b-1];
matrix[row][b-1] = temp;
}
}
// Other operation types are ignored
}
// Print the resulting matrix
for (int i = 0; i < rowCount; ++i) {
for (int j = 0; j < colCount; ++j) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
return 0;
}
Summary
- When swapping rows or columns, convert the 1-based input indices (
a,b) to 0-based indices by subtracting 1 (e.g., rowabecomesa-1). - If you’re unsure about the logic, first solve the problem manually (e.g., swap rows/columns on paper) and then translate that reasoning into code.