0

给定一个数字 x,在矩阵中螺旋插入元素 1 到 x^2。例如对于 x = 3,矩阵看起来像 [[1,2,3],[8,9,4],[7,6,5]]。为此,我编写了以下代码段。但是,我得到 o/p 为 [[7,9,5],[7,9,5],[7,9,5]]

while(t<=b && l<=r){
               System.out.print(t+" "+b+" "+l+" "+r+"\n");
        if(dir==0){
            for(int i = l;i<=r;i++){
                arr.get(t).set(i,x);
                x++;
            }

            t++;
        }else if(dir==1){
            for(int i = t;i<=b;i++){
                arr.get(i).set(r,x);
                x++;
            }
            r--;
        }else if(dir==2){
            for(int i = r;i>=l;i--){
                arr.get(b).set(i,x);
                x++;
            }
            b--;
        }else if(dir==3){
            for(int i = b;i>=t;i--){
                arr.get(l).set(i,x);
                x++;
            }
            l++;
        }
        dir = (dir+1)%4;

    }
4

2 回答 2

0

您可以使用下一个代码(我为一些处理巨大矩阵大小的实现而开发的代码)。它将使用任何矩阵大小的宽度(列)和高度(行)并产生您需要的输出

    List<rec> BuildSpiralIndexList(long w, long h)
    {
        List<rec> result = new List<rec>();
        long count = 0,dir = 1,phase = 0,pos = 0;
        long length = 0,totallength = 0;
        bool isVertical = false;

        if ((w * h)<1) return null;
        do
        {
            isVertical = (count % 2) != 0;
            length = (isVertical ? h : w) - count / 2 - count % 2;
            phase = (count / 4);
            pos = (count % 4);
            dir = pos > 1 ? -1 : 1;
            for (int t = 0; t < length; t++)
                // you can replace the next code with printing or any other action you need
                result.Add(new rec()
                {
                    X = ((pos == 2 || pos == 1) ? (w - 1 - phase - (pos == 2 ? 1 : 0)) : phase) + dir * (isVertical ? 0 : t),
                    Y = ((pos <= 1 ? phase + pos : (h - 1) - phase - pos / 3)) + dir * (isVertical ? t : 0),
                    Index = totallength + t
                });
            totallength += length;
            count++;
        } while (totallength < (w*h));
        return result;
    }
于 2016-08-17T13:54:32.883 回答
0

此解决方案从左上到右上,从右上到右下,从右下到左下,从左下到左上。这是一个棘手的问题,希望我下面的评论有助于解释。

下面是一个 codepen 链接,可以看到它添加到表格中。 https://codepen.io/mitchell-boland/pen/rqdWPO

const n = 3; // Set this to a number

matrixSpiral(n);

function matrixSpiral(number){

    // Will populate the outer array with n-times inner arrays
    var outerArray = [];

    for(var i = 0; i < number; i++){
      outerArray.push([]);
    }


    var leftColumn = 0;
    var rightColumn = number - 1;
    var topRow = 0;
    var bottomRow = number-1;
    var counter = 1; // Used to track the number we are up to.

    while(leftColumn <= rightColumn && topRow  <=bottomRow){

        // populate the top row
        for(var i = leftColumn; i <= rightColumn; i++){
          outerArray[leftColumn][i] = counter;
          counter++;
        }
        // Top row is now populated
        topRow ++;

        // Populate the right column
        for(var i = topRow ; i <= bottomRow; i++){
          outerArray[i][rightColumn] = counter;
          counter++;
        }
        // Right column now populated.
        rightColumn--;

        // Populate the bottom row
        // We are going from the bottom right, to the bottom left
        for(var i = rightColumn; i >= leftColumn; i--){
          outerArray[bottomRow][i] = counter;
          counter++;
        }
        // Bottom Row now populated
        bottomRow--;

        // Populate the left column
        // We are going from bottom left, to top left
        for(var i = bottomRow; i >= topRow ; i--){
          outerArray[i][leftColumn] = counter;
          counter++;
        }
        // Left column now populated.
        leftColumn++;

        // While loop will now repeat the above process, but a step in.
    }

    // Console log the results.
    for(var i = 0; i < number; i++){
        console.log(outerArray[i]);
    }
}
于 2018-11-27T03:36:15.330 回答