-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprint-matrix-in-spiral-order.php
93 lines (88 loc) · 2.91 KB
/
print-matrix-in-spiral-order.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
$matrix = [[0, 1, 2, 3], [1, 2, 3, 4], [0, 5, 4, 5], [9, 8, 7, 6]];
function printMatrix($matrix) {
$matrixSize = count($matrix);
for ($row = 0; $row < $matrixSize; $row++) {
for ($column = 0; $column < $matrixSize; $column++) {
printValue($matrix, $row, $column);
}
echo "<br>";
}
}
function printMatrixBySpiralClockwise($matrix) {
$matrixSize = count($matrix);
$top = $left = 0;
$bottom = $right = $matrixSize - 1;
$direction = 0;
while ($top <= $bottom && $left <= $right) {
if ($direction === 0) {
// From left to right
for ($column = $left; $column <= $right; $column++) {
printValue($matrix, $top, $column);
}
$top++;
} else if ($direction == 1) {
// From top to bottom
for ($row = $top; $row <= $bottom; $row++) {
printValue($matrix, $row, $right);
}
$right--;
} else if ($direction == 2) {
// From right to left
for ($column = $right; $column >= $left; $column--) {
printValue($matrix, $bottom, $column);
}
$bottom--;
} else if ($direction == 3) {
// From bottom top top
for ($row = $bottom; $row >= $top; $row--) {
printValue($matrix, $row, $left);
}
$left++;
}
$direction = (++$direction % 4); // 0,1,2,3,0,...
}
}
function printMatrixBySpiralCounterClockwise($matrix) {
$matrixSize = count($matrix);
$top = $left = 0;
$bottom = $right = $matrixSize - 1;
$direction = 0;
while ($top <= $bottom && $left <= $right) {
if ($direction === 0) {
// From right to left
for ($column = $right; $column >= $left; $column--) {
printValue($matrix, $top, $column);
}
$top++;
} else if ($direction == 1) {
// From top to bottom
for ($row = $top; $row <= $bottom; $row++) {
printValue($matrix, $row, $left);
}
$left++;
} else if ($direction === 2) {
// From left to right
for ($column = $left; $column <= $right; $column++) {
printValue($matrix, $bottom, $column);
}
$bottom--;
} else if ($direction == 3) {
// From bottom to top
for ($row = $bottom; $row >= $top; $row--) {
printValue($matrix, $row, $right);
}
$right--;
}
$direction = (++$direction % 4); // 0,1,2,3,0,...
}
}
function printValue($matrix, $row, $column) {
echo $matrix[$row][$column] . "\t";
}
echo 'Matrix:<br>';
printMatrix($matrix);
echo "<br> Matrix clockwise:<br>";
printMatrixBySpiralClockwise($matrix);
echo "<br><br> Matrix counterclockwise:<br>";
printMatrixBySpiralCounterClockwise($matrix);