我想以螺旋顺序打印数组。适用于尺寸为3x3,4x4等的阵列。我的代码工作正常,但对于3x5,4x6或5x8大小,输出错误,只返回第一次迭代。
这是我的简单代码:
private function _spiral($rows, $cols, array $array) {
$offset = 0;
while($offset < ($rows - 1)){
for($col = $offset; $col <= $cols - 1; $col++){
print($array[$offset][$col] . ' ');
}
$offset++;
$cols--;
for($row = $offset; $row < $rows; $row++){
print($array[$row][$cols] . ' ');
}
$rows--;
for($col = $cols - 1; $col >= $offset; $col--){
print($array[$rows][$col] . ' ');
}
for($row = $rows; $row >= $offset; $row--){
print($array[$row][$offset - 1] . ' ');
}
}
}
包含3行和4列的示例:
$array = array(
array(00,01,02,03),
array(10,11,12,13),
array(20,21,22,23)
)
此数组的预期结果为0 1 2 3 13 23 22 21 20 10 11 12
,但我的函数输出在10之后停止。
4行4列:
$array = array(
array(00,01,02,03),
array(10,11,12,13),
array(20,21,22,23),
array(30,31,32,33)
)
...它应该返回0 1 2 3 13 23 33 32 31 30 20 10 11 12 22 21
,这就是我的代码返回的内容。
但我希望这两种情况都适用于我的代码。如何更正代码以便为第一个和其他情况生成正确的输出?
答案 0 :(得分:2)
您的代码存在一些问题:
它不会以同样的方式处理四个遍历方向。这四个方向有四个循环,但在某些方面你有<=
作为其他<
的循环结束条件,在某些情况下,条件是减去1,而在其他情况下不是。
它没有规定何时所有元素都由第一个或第二个内环打印,因此剩余的循环在某些情况下会打印已打印的元素。
外部循环条件不检查是否仍有需要遍历的列。仅仅测试这样的行是不够的。
虽然您可以尝试修复代码,但我认为最好从头开始,考虑到解决方案应该对所有四个方向都是对称的。这是一个重要的直观反应:点对称性。这将导致更少的代码和更少的错误。
您希望遍历数组中的维(行或列),直到到达数组的边框或已打印的元素。然后你想向右转90°并反复重复完全相同的逻辑。因此,如果您的代码对于这些不同的方向看起来不同,那么事情就不对了。
我将分享两个实现。两者都将使用“当前”单元格的概念,让它以螺旋运动的方式移动。
第一个解决方案使用相同的代码处理沿着一行返回或前进,类似地,它有一段用于向前或向后遍历列的代码。所以这个解决方案有两个内部循环,一个用于沿着一行遍历,另一个用于沿着列遍历。遍历行或列的方向保存在$direction
变量中,每次执行外循环时,该变量在1和-1之间翻转:
function _spiral(array $array) {
// No need to have the number of rows and columns passed as arguments:
// We can get that information from the array:
$rows = count($array);
$cols = count($array[0]);
// Set "current" cell to be outside array: it moves into it in first inner loop
$row = 0;
$col = -1;
$direction = 1; // Can be 1 for forward and -1 for backward
while ($rows > 0 and $cols > 0) {
// Print cells along one row
for ($step = 0; $step < $cols; $step++) {
$col += $direction;
print $array[$row][$col] . ' ';
}
// As we have printed a row, we have fewer rows left to print from:
$rows--;
// Print cells along one column
for ($step = 0; $step < $rows; $step++) {
$row += $direction;
print $array[$row][$col] . ' ';
}
// As we have printed a column, we have fewer columns left to print from:
$cols--;
// Now flip the direction between forward and backward
$direction = -$direction;
}
}
注意第一个内环和第二个内环之间的完美对称。
在第二种解决方案中,这种对称性的使用更进一步,以便仅用一个替换两个内环。为此,我们必须放弃对行和列使用单独的变量,并使用与维度相关的大小的概念:
function _spiral(array $array) {
// This version of the function aims to treat rows and columns in the same way,
// They are just another dimension, but all the logic is exactly the same:
// $size[] has the number of rows in $size[0] and number of columns in $size[1]
$size = Array(count($array), count($array[0]));
// $current[] has the current row in $current[0] and current column in $current[1]
$current = Array(0, -1);
// $direction[] has the current row-traversal direction in $direction[0]
// and column-traveral direction in $direction[1]
$direction = Array(1, 1);
$dimension = 0; // Which dimension to traverse along, can be 0 for row, 1 for column
while ($size[$dimension] > 0) {
// Switch dimension (row to column, column to row), to traverse along
$dimension = 1 - $dimension;
// Print one line along that dimension, in its current direction
for ($step = 0; $step < $size[$dimension]; $step++) {
$current[$dimension] += $direction[$dimension];
print $array[$current[0]][$current[1]] . ' ';
}
// As we have printed a line, we have fewer left to print from:
$size[1 - $dimension]--;
// Now flip the direction between forward and backward for this dimension:
$direction[$dimension] = -$direction[$dimension];
}
}
超过一年后的请求:这是一个版本,允许人们选择从角落开始,以及是逆时针而不是顺时针。此函数不会打印结果,而是返回带有螺旋序列的1D数组。这样你就可以自己决定如何处理结果:打印它,或者......无论如何。
function spiral(array $array, $startRight = false, $startBottom = false,
$counterClockWise = false) {
// This version allows to select which corner to start from, and in which direction.
// $size[] has the number of rows in $size[0] and number of columns in $size[1]
$size = [count($array), count($array[0])];
// $direction[] has the current row-traversal direction in $direction[0]
// and column-traversal direction in $direction[1]
$direction = [$startBottom ? -1 : 1, $startRight ? -1 : 1];
// Which dimension to traverse along: false means row, true means column.
// Every one of the optional arguments will flip the first dimension to use:
$dimension = ($startBottom xor $startRight xor $counterClockWise);
// $current[] has the current row in $current[0] and current column in $current[1]
$current = [$startBottom * (count($array)-1), $startRight * (count($array[0])-1)];
// Go back one step, outside of the grid
$current[!$dimension] -= $direction[!$dimension];
while ($size[$dimension] > 0) {
// Switch dimension (row to column, column to row), to traverse along
$dimension = !$dimension;
// Print one line along that dimension, in its current direction
for ($step = 0; $step < $size[$dimension]; $step++) {
$current[$dimension] += $direction[$dimension];
$result[] = $array[$current[0]][$current[1]]; // store in new array
}
// As we have printed a line, we have fewer left to print from:
$size[!$dimension]--;
// Now flip the direction between forward and backward for this dimension:
$direction[$dimension] = -$direction[$dimension];
}
return $result; // Return the resulting spiral as a 1D array
}
上查看它