-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix_rotation_anti_clockwise.php
104 lines (100 loc) · 2.83 KB
/
matrix_rotation_anti_clockwise.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
94
95
96
97
98
99
100
101
102
103
104
<?php
$_fp = fopen("php://stdin", "r");
$array_params = split(" ", preg_replace( "/\r|\n/", "", fgets($_fp)));
$rows = $array_params[0];
$columns = $array_params[1];
$rotations = $array_params[2];
$matrix = array();
for ($i = 0; $i < $rows; $i++) {
$matrix[$i] = split(" ", preg_replace( "/\r|\n/", "", fgets($_fp)));
}
fclose($_fp);
$first_row = 0;
$first_column = 0;
$last_row = $rows -1;
$last_column = $columns -1;
do {
//pushing elements to row
$index_quadrant = $first_row;
// first row
$i = $first_row;
for($j = $first_column; $j < $last_column; $j++) {
$quadrant[$index_quadrant][] = $matrix[$i][$j];
}
// last column
$j = $last_column;
for($i = $first_row; $i < $last_row; $i++) {
$quadrant[$index_quadrant][] = $matrix[$i][$j];
}
// last row
$i = $last_row;
for($j = $last_column; $j > $first_column; $j--) {
$quadrant[$index_quadrant][] = $matrix[$i][$j];
}
// first column
$j = $first_column;
for($i = $last_row; $i > $first_row; $i--) {
$quadrant[$index_quadrant][] = $matrix[$i][$j];
}
$index_quadrant++;
$first_row++;
$first_column++;
$last_row--;
$last_column--;
} while ($first_row < $last_row && $first_column < $last_column);
//print_r($quadrant); exit;
foreach($quadrant as $index => $row) {
//size of array
$array_size = count($row);
$rotations_left = $rotations >= $array_size ? $rotations % $array_size : $rotations;
//rotate
for($iterations = 0; $iterations < $rotations_left; $iterations++) {
array_push($row, $row[0]);
array_shift($row);
}
$quadrant[$index] = $row;
}
//print_r($quadrant); exit;
$first_row = 0;
$first_column = 0;
$last_row = $rows -1;
$last_column = $columns -1;
$length_quadrant = count($quadrant);
do {
//pushing elements to row
$index_quadrant = $first_row;
$array_index = 0;
// first row
$i = $first_row;
for($j = $first_column; $j < $last_column; $j++) {
$matrix[$i][$j] = $quadrant[$index_quadrant][$array_index];
$array_index++;
}
// last column
$j = $last_column;
for($i = $first_row; $i < $last_row; $i++) {
$matrix[$i][$j] = $quadrant[$index_quadrant][$array_index];
$array_index++;
}
// last row
$i = $last_row;
for($j = $last_column; $j > $first_column; $j--) {
$matrix[$i][$j] = $quadrant[$index_quadrant][$array_index];
$array_index++;
}
// first column
$j = $first_column;
for($i = $last_row; $i > $first_row; $i--) {
$matrix[$i][$j] = $quadrant[$index_quadrant][$array_index];
$array_index++;
}
$index_quadrant++;
$first_row++;
$first_column++;
$last_row--;
$last_column--;
} while ($index_quadrant < $length_quadrant);
foreach($matrix as $row) {
print(implode(" ", $row)) . "\n";
}
?>