A more interesting question today is that the 25 letters of a to Y are output in the following form
A |
B |
C |
D |
E |
P |
Q |
R |
S |
F |
O |
X |
Y |
T |
G |
N |
W |
V |
U |
H |
M |
L |
K |
J |
I |
The problem is interesting, is to turn the alphabet into the table, to output such a format, in fact, you need to construct a table like the following
1 |
2 |
3 |
4 |
5 |
16 |
17 |
18 |
18 |
6 |
15 |
24 |
25 |
20 |
7 |
14 |
23 |
22 |
21st |
8 |
13 |
12 |
11 |
10 |
9 |
This is actually a problem with the spiral matrix, which is essentially the same as the following: Given a row number row and column number cols, output the corresponding spiral array, such as 3 rows and 5 columns
1 |
2 |
3 |
4 |
5 |
12 |
13 |
14 |
15 |
6 |
11 |
10 |
9 |
8 |
7 |
Another example, 5 rows, 3 columns.
1 |
2 |
3 |
12 |
13 |
4 |
11 |
14 |
5 |
10 |
15 |
6 |
9 |
8 |
7 |
There may be many ways to solve this problem, but my idea is to press the top, right, bottom, and left four directions to the inside circle, such as thinking has, followed by writing code, gossip less, on the code
/** * Generates a spiral array based on the number of rows and columns passed in * @author Chenqionghe * @param int $row number of rows * @param int $col columns * @return Array */function rotation Sort ($row =5, $col =5) {$k = 1; $result = Array (); $small = $col < $row? $col: $row; $count = Ceil ($small/2); for ($i =0; $i < $count; $i + +) {$maxRight = $col-$i;//right maximum coordinate $maxBottom = $row-1-$i;//maximum coordinates F below or ($j = $i; $j <= $maxRight; $j + +)//structure The top line has the smallest ordinate, the horizontal axis increment {$result [$i] [$j] = $k + +; } for ($j = $i; $j < $maxBottom; $j + +)//construction to the right of a line ordinate increment, horizontal axis max {$result [$j +1][$maxRight] = $k + +; } for ($j = $maxRight-1; $j >= $i; $j-)//structure The bottom line has the largest ordinate, the horizontal axis decrements {if ($result [$maxBottom] [$j] ) break; $result [$maxBottom] [$j] = $k + +; } for ($j = $maxBottom-1; $j > $i; $j-)//structure to the left of a line, the vertical axis is decremented, the horizontal bar minimum {if ($result [$j] [$i]) break; $result [$j] [$i] = $k + +; }} return $result;}
This function is written by the great poet Chenqionghe, call simultaneous number two parameter row number and column number, can return the spiral array, the good man achieves the bottom bar, and then defines a way to print the spiral array in the form output, the method is as follows
/** * Output array in table format * @param $result spiral Array * @param $row number of rows * @param $col number of columns */function printArray ($result, $row, $col) { E Cho ' <table border=1 style= "width:500px;" > '; for ($i =0; $i < $row; $i + +) { echo ' <tr> '; for ($j =0; $j < $col; $j + +) { echo ' <td style= "padding:50px;" > '. $result [$i] [$j]. ' </td> '; } Echo ' <tr> '; } Echo ' </table> ';}
Then, let's call the above method to generate a 5*5 spiral array
$row = 5; $col = 5; $arr = Rotationsort ($row, $col);p Rintarray ($arr, $row, $col);
Execution results as follows I am a good boy, can only use perfect to describe. Now, we go back to that output 25 letter a-y problem, to solve this problem is more simple, that is, 0-to 24 is the key, a-y as the value, define the array on the line, as follows:
$arr = Array (' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G ', ' H ', ' I ', ' J ', ' K ', ' L ', ' N ', ' M ', ' O ', ' P ', ' Q ', ' R ', ' S ', ' T ', ' U ', ' V ', ' W ', ' X ', ' Y ', ' Z ',);
The output of the time a little change can be output a-y spiral array, the complete code is as follows
<?php/** * Generates a spiral array based on the number of rows and columns passed in * @author Chenqionghe * @param int $row number of rows * @param int $col columns * @return Array */function Rotationsort ($row =5, $col =5) {$k = 1; $result = Array (); $small = $col < $row? $col: $row; $count = Ceil ($small/2); for ($i =0; $i < $count; $i + +) {$maxRight = $col-$i;//right maximum coordinate $maxBottom = $row-1-$i;//maximum coordinates F below or ($j = $i; $j <= $maxRight; $j + +)//structure The top line has the smallest ordinate, the horizontal axis increment {$result [$i] [$j] = $k + +; } for ($j = $i; $j < $maxBottom; $j + +)//construction to the right of a line ordinate increment, horizontal axis max {$result [$j +1][$maxRight] = $k + +; } for ($j = $maxRight-1; $j >= $i; $j-)//structure The bottom line has the largest ordinate, the horizontal axis decrements {if ($result [$maxBottom] [$j] ) break; $result [$maxBottom] [$j] = $k + +; } for ($j = $maxBottom-1; $j > $i; $j-)//structure to the left of a line, the vertical axis is decremented, the horizontal bar minimum {if ($result [$j] [$i]) break; $result [$j] [$i] = $k + +; }} return $resUlt;} /** * Output array in table format * @param $rotationArr what to Output * @param $result spiral Array * @param $row number of rows * @param $col number of columns */function Printar Ray ($ROTATIONARR, $result, $row, $col) {echo ' <table border=1 style= "width:500px;" > '; for ($i =0; $i < $row; $i + +) {echo ' <tr> '; for ($j =0; $j < $col; $j + +) {//echo ' <td style= "padding:50px;" > '. $result [$i] [$j]. ' </td> '; Echo ' <td style= ' padding:50px; > '. $ROTATIONARR [$result [$i] [$j]-1]. ' </td> '; } echo ' <tr> '; } echo ' </table> ';} $arr = Array (' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G ', ' H ', ' I ', ' J ', ' K ', ' L ', ' N ', ' M ', ' O ', ' P ', ' Q ', ' R ', ' S ', ' T ', ' U ', ' V ', ' W ', ' X ', ' Y ', ' Z ',); $row = 5; $col = 5; $rotationArr = Rotationsort ($row, $col);p Rintarray ($arr, $ROTATIONARR, $row, $col);
The final output is as follows: is not particularly perfect, especially want to invite me to dinner!
PHP Implementation Spiral matrix (spiral Array)