1196 – Corner I

1. Problem Understanding Phase (Corner Matrix Problem)
We need to print a special N×N matrix characterized by:
-
The diagonal from the top left to the bottom right corner and the area above it, where each element’s value equals its column index
-
The area below the diagonal, where each element’s value equals its row index
-
Implementing the matrix printing using recursion
2. Core Idea of the Recursive Algorithm
The program adopts the row-column recursion method:
-
Recursively process in row-major order
-
Recursively process each column within a row
-
The termination condition for recursion is when the row index exceeds N
3. Detailed Execution Process (Example with N=3)
Output Matrix:
1 1 1
1 2 2
1 2 3
Reference Code
#include <bits/stdc++.h> // Include all standard library headers
using namespace std; // Use standard namespace
/** * Recursively print the corner matrix * @param row Current row index being processed * @param col Current column index being processed * @param N Size of the matrix */
void printMatrix(int row, int col, int N) { // Termination condition: row index exceeds matrix size if (row > N) return;
// When column index exceeds matrix size, move to the next line and process the next row if (col > N) { cout << endl; // Output newline printMatrix(row + 1, 1, N); // Recursively process the next row return; }
// Output the current element: if column index ≤ row index, output column index, otherwise output row index cout << setw(3) << (col <= row ? col : row);
// Recursively process the next column in the current row printMatrix(row, col + 1, N);}
int main() { int N; // Size of the matrix cin >> N; // Read input
// Start recursive printing from row 1, column 1 printMatrix(1, 1, N);
return 0; // End of program}
1. Program Function Description
This program prints a special N×N matrix:
-
Above and including the diagonal: element value = column index
-
Below the diagonal: element value = row index
-
Using recursion to achieve row and column traversal
-
Each number occupies a width of 3 characters
2. Core Algorithm Analysis
Recursive Control Flow:
-
Row Recursion:
-
When all N columns are processed, move to the next line
-
Recursively call to process the next row
Column Recursion:
-
Recursively process each column from left to right within a row
-
Decide whether to output the row index or column index based on position
Termination Condition for Recursion:
-
Terminate when row index row > N
3. Variable Role Description
-
<span>row</span>: Current row index being processed (starting from 1) -
<span>col</span>: Current column index being processed (starting from 1) -
<span>N</span>: Dimension of the matrix -
<span>setw(3)</span>: Sets output width to 3 characters
4. Execution Process Example (N=3)
-
printMatrix(1,1,3)
-
Output 1
-
printMatrix(1,3,3)
-
Output 1
-
printMatrix(1,4,3) → New line
-
Output 1
-
printMatrix(1,2,3)
printMatrix(2,1,3)
-
Output 2
-
printMatrix(2,3,3)
-
Output 2
-
printMatrix(2,4,3) → New line
-
Output 1
-
printMatrix(2,2,3)
printMatrix(3,1,3)
-
Output 2
-
printMatrix(3,3,3)
-
Output 3
-
printMatrix(3,4,3) → New line
-
Output 1
-
printMatrix(3,2,3)
printMatrix(4,1,3) → Termination
5. Recursive Call Visualization
printMatrix(1,1,3)
│
├── Output 1
├── printMatrix(1,2,3)
│ ├── Output 1
│ ├── printMatrix(1,3,3)
│ │ ├── Output 1
│ │ └── printMatrix(1,4,3) → New line
│ │ └── printMatrix(2,1,3)
│ ...
└── ...
6. Important Notes
-
Order of Recursion:
-
Process in row-major order
-
Process each row from left to right
Output Format:
-
setw(3) ensures alignment
-
Use conditional operators to select output values
Boundary Conditions:
-
Row and column indices start from 1
-
The termination condition for recursion is row > N
7. Reference Test Cases
|
Input N |
Output Matrix |
|---|---|
|
1 |
1 |
|
2 |
1 11 2 |
|
3 |
1 1 11 2 21 2 3 |
|
4 |
1 1 1 11 2 2 21 2 3 31 2 3 4 |
This explanation is entirely based on the original code, providing a detailed analysis of the execution process and visualizing the call relationships to help beginners deeply understand the principles of recursively printing matrices.