java - 如何在矩阵中生成同心层

标签 java loops matrix

我需要生成一个包含这方面的矩阵:

enter image description here

但是随着层数的增加,我找不到办法做到这一点。 理解一下,每种颜色都有n层(本例中,n=2),可以有m种颜色(本例中, m=3)。内部矩阵,绿色的,应该遵循与其他矩阵相同的螺旋模式,在图像中是错误的。下一个颜色,在本例中为黄色,需要从左上角开始“包围”前一个矩阵,填充一层并继续左上角的下一个“层”,依此类推。 颜色本身并不重要,重要的是每个单元格中的数字。

有什么想法吗?

PS:忘记绿色的 10 和 34,那些只是修改。

PS2:这个例子是手工填充的,对于这种大小的矩阵我可以做到,但是对于 256x256 是不可能的。

最佳答案

一种策略是从最内层开始,然后向外填充。这样,核心循环就变得特别简单,因为您只需遍历矩阵的相关部分,然后只填充尚未填充的字段。

矩阵的“相关”部分可以在层的颜色循环中轻松计算:对于每一层,一层覆盖的矩形的总大小(宽度和高度)增加 2。当填充了所需的层数后,用于填充矩阵的“计数器”将重置为零,以指示新颜色开始。

一个例子:

public class LayeredMatrix
{
    public static void main(String[] args)
    {
        test(1,1);
        test(2,2);
        test(3,3);
        test(2,3);
    }

    private static void test(int layers, int colors)
    {
        System.out.println(layers+" layers, "+colors+" colors");
        print(generate(layers, colors));
    }

    private static int[][] generate(int layers, int colors)
    {
        int size = layers * colors * 2;
        int matrix[][] = new int[size][size];
        int layerSize = 2;
        for (int color=0; color<colors; color++)
        {
            int colorOffset = (colors - color - 1) * layers;
            int counter = 1;
            for (int layer = 0; layer < layers; layer++)
            {
                int layerOffset = layers - layer - 1;
                int r0 = colorOffset + layerOffset;
                int c0 = colorOffset + layerOffset;
                int r1 = r0 + layerSize;
                int c1 = c0 + layerSize;
                for (int r=r0; r<r1; r++)
                {
                    for (int c=c0; c<c1; c++)
                    {
                        if (matrix[r][c] == 0)
                        {
                            matrix[r][c] = counter;
                            counter++;
                        }
                    }
                }
                layerSize += 2;
            }
        }
        return matrix;
    }

    private static void print(int matrix[][])
    {
        for (int r=0; r<matrix.length; r++)
        {
            for (int c=0; c<matrix[r].length; c++)
            {
                System.out.printf("%4d", matrix[r][c]);
            }
            System.out.println();
        }
        System.out.println();
    }

}

对于问题中描述的情况,它打印

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

关于java - 如何在矩阵中生成同心层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30258273/

相关文章:

xcode - 遍历对象/变量 Swift

algorithm - 用于空模型的交换算法的 Scala 版本

带有自定义 HTML 标签 View 的 Java HTML 渲染器

java - Ant 路径样式模式

excel - VBA Excel 2007 : Need to loop copy and loop count number except zero every row above

loops - 做一个while循环,在Dart/Flutter中循环1秒

java - 使用自定义 valueDeserializer 时,如何在 .properties/.yaml 中设置 ack-mode?

java - Android studio, File.properties 重新打开应用后删除

减少矩阵比较的计算量

java - 寻找矩阵上最短路径的问题