Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Procedural
- */
- $n = 3;
- $matrix = [
- [1, 2, 3],
- [4, 5, 6],
- [7, 8, 9],
- ];
- function rotate(&$matrix, $n)
- {
- $result = [];
- for ($col = 0; $col < $n; $col++) {
- $currRow = [];
- for ($row = $n - 1; $row >= 0; $row--) {
- $currRow[] = $matrix[$row][$col];
- }
- $result[] = $currRow;
- }
- $matrix = $result;
- }
- function renderMatrix($matrix, $n)
- {
- for ($row = 0; $row < $n; $row++) {
- for ($col = 0; $col < $n; $col++) {
- echo $matrix[$row][$col] . ' ';
- }
- echo '<br />';
- }
- }
- // Show initial matrix
- renderMatrix($matrix, $n);
- echo '<br />';
- // One rotation
- rotate($matrix, $n);
- renderMatrix($matrix, $n);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement