螺旋插入矩阵中的元素 [英] Inserting elements in a matrix spirally

查看:98
本文介绍了螺旋插入矩阵中的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个数字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]]

Given a number x, insert elements 1 to x^2 in a matrix spirally. e.g. For x = 3, matrix looks like [[1,2,3],[8,9,4],[7,6,5]]. For this I've written following snippet. However, I'm getting o/p as [[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;

    }

推荐答案

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

This solution walks from the top left to the top right, the top right to the bottom right, the bottom right to the bottom left and the bottom left up to the top left. It is a tricky problem, hopefully my comments below assist in explaining.

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

Below is a codepen link to see it added to a table. 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]);
    }
}

这篇关于螺旋插入矩阵中的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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