PHP - 创建一个螺旋 [英] PHP - create a spiral

查看:97
本文介绍了PHP - 创建一个螺旋的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有 $行= 4 $ COLS = 4; ,如何创建一个数组以螺旋为了16个元素,如:

  1,2,3,4,
12,13,14,5,
11,16,15,6,
10,9,8,7,


解决方案

我的解决方案是相当冗长,因为其目的是为你检查它,了解它是如何工作的。

 < PHP
/ **
 *用给定尺寸的二维数组,
 *其元素的数字递增的顺序
 *在螺旋的模式呈现。
 * /
功能createSpiral($ W,$ H){
   如果($ W< = 0 || $ H< = 0)返回FALSE;   $ AR =阵列();
   $使用=阵列();   //建立网格
   为($ J = 0; $ J< $ H; $ J ++){
      $ AR [$ J] =阵列();
      为($ I = 0; $ I< $ W; $ I ++)
          $ AR [$ J] [$ i] =' - ';
   }   //建立'使用'网格,在每个维度中的一个较大
   为($ J = -1; $ J< = $ H; $ J ++){
      $使用[$ J] =阵列();
      为($ I = -1; $ I< = $ W; $ I ++){
          如果($ I == -1 || $ I == $ W ||附加$ J == -1 ||附加$ J $ ==高)
             $使用[$ J] [$ i] = TRUE;
          其他
             $使用[$ J] [$ i] = FALSE;
      }
   }   //填充网格螺旋
   $ N = 0;
   $ I = 0;
   $ J = 0;
   $方向= 0; // 0 - 左,1 - 下降,2 - 右边,3 - 向上
   而(真){
      $ AR [$ J] [$ i] = $ n ++;
      $使用[$ J] [$ i] = TRUE;      //进展
      开关($方向){
         情况下0:
            $ I ++; // 向右走
            如果($使用[$ J] [$ i]){//得RHS
               $我 - ; $ J ++; //返回左,再向下
               $方向= 1;
            }
            打破;
        情况1:
           $ J ++; // 下去
           如果($使用[$ J] [$ i]){//得底
               $ j--; $我 - ; //返回了起来,然后离开
               $方向= 2;
            }
            打破;
         案例2:
            $我 - ; // 向左走
            如果($使用[$ J] [$ i]){//得到了LHS
               $ I ++; $ j--; //返回离开,再向上
               $方向= 3;
            }
            打破;
         案例3:
            $ j--; //上去
            如果($使用[$ J] [$ i]){//得到顶部
               $ J ++; $ I ++; //落回原位,然后用鼠标右键
               $方向= 0;
            }
            打破;
       }       //如果新的位置是在使用中,我们就大功告成了!
       如果($使用[$ J] [$ i])
           返回$ AR;
   }
}/ **
 *假定输入是一个二维阵列。
 * /
功能print2DGrid($数组){
   的foreach($数组作为$行){
       的foreach($行为$列){
          打印的sprintf(%2D,$ COL);
       }
       打印\\ n;
   }
}
$宽度= 12;
$高度= 8;print2DGrid(createSpiral($宽度,高度$));
?>

这是的行动,给下面的输出:

  0 1 2 3 4 5 6 7 8 9 10 11
35 36 37 38 39 40 41 42 43 44 45 12
34 63 64 65 66 67 68 69 70 71 46 13
33 62 83 84 85 86 87 88 89 72 47 14
32 61 82 95 94 93 92 91 90 73 48 15
31 60 81 80 79 78 77 76 75 74 49 16
30 59 58 57 56 55 54 53 52 51 50 17
29 28 27 26 25 24 23 22 21 20 19 18

希望这有助于。

assuming I have $rows = 4, and $cols = 4;, How do I create a array with 16 elements in a spiral order, like:

1, 2, 3, 4,
12,13,14,5,
11,16,15,6,
10,9, 8, 7,

解决方案

My solution is quite verbose, because the intent is for you to examine it and learn how it works.

<?php
/**
 * Creates a 2D array with the given dimensions,
 * whose elements are numbers in increasing order
 * rendered in a 'spiral' pattern.
 */
function createSpiral($w, $h) {
   if ($w <= 0 || $h <= 0) return FALSE;

   $ar   = Array();
   $used = Array();

   // Establish grid
   for ($j = 0; $j < $h; $j++) {
      $ar[$j] = Array();
      for ($i = 0; $i < $w; $i++)
          $ar[$j][$i]   = '-';
   }

   // Establish 'used' grid that's one bigger in each dimension
   for ($j = -1; $j <= $h; $j++) {
      $used[$j] = Array();
      for ($i = -1; $i <= $w; $i++) {
          if ($i == -1 || $i == $w || $j == -1 || $j == $h)
             $used[$j][$i] = true;
          else
             $used[$j][$i] = false;
      }
   }

   // Fill grid with spiral
   $n = 0;
   $i = 0;
   $j = 0;
   $direction = 0; // 0 - left, 1 - down, 2 - right, 3 - up
   while (true) {
      $ar[$j][$i]   = $n++;
      $used[$j][$i] = true;

      // Advance
      switch ($direction) {
         case 0:
            $i++; // go right
            if ($used[$j][$i]) { // got to RHS
               $i--; $j++; // go back left, then down
               $direction = 1;
            }
            break;
        case 1:
           $j++; // go down
           if ($used[$j][$i]) { // got to bottom
               $j--; $i--; // go back up, then left
               $direction = 2;
            }
            break;
         case 2:
            $i--; // go left
            if ($used[$j][$i]) { // got to LHS
               $i++; $j--; // go back left, then up
               $direction = 3;
            }
            break;
         case 3:
            $j--; // go up
            if ($used[$j][$i]) { // got to top
               $j++; $i++; // go back down, then right
               $direction = 0;
            }
            break;
       }

       // if the new position is in use, we're done!
       if ($used[$j][$i])
           return $ar;
   }
}

/**
 * Assumes the input is a 2D array.
 */
function print2DGrid($array) {
   foreach ($array as $row) {
       foreach ($row as $col) {
          print sprintf("% 2d ", $col);
       }
       print "\n";
   }
}


$width  = 12;
$height = 8;

print2DGrid(createSpiral($width, $height));
?>

Here it is in action, giving the following output:

 0  1  2  3  4  5  6  7  8  9 10 11 
35 36 37 38 39 40 41 42 43 44 45 12 
34 63 64 65 66 67 68 69 70 71 46 13 
33 62 83 84 85 86 87 88 89 72 47 14 
32 61 82 95 94 93 92 91 90 73 48 15 
31 60 81 80 79 78 77 76 75 74 49 16 
30 59 58 57 56 55 54 53 52 51 50 17 
29 28 27 26 25 24 23 22 21 20 19 18 

Hope this helps.

这篇关于PHP - 创建一个螺旋的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆