【问题标题】:How can I fill a 2D array spirally in C? Example/problem如何在 C 中螺旋填充二维数组?示例/问题
【发布时间】:2020-12-13 09:01:16
【问题描述】:

所以我已经为这个例子苦苦挣扎了一个小时,我什至无法开始处理我应该如何做这个。

编写一个程序,对于给定的 n 和 m,形成所描述的矩阵。 矩阵应该是 m x m,并用它“螺旋”填充 从左上角开始。矩阵中的第一个值是 数 n。它一直重复到矩阵的“边缘”,在该“边缘”处 点数递增。在数字 9 变为 0 之后。0 ≤ n ≤ 9, 0 ≤ 米≤9

【问题讨论】:

  • 你能先在纸上弄清楚怎么做吗?
  • 天真的方法:我将从用于导航的 x=0、y=0、dx=1 和 dy=0 值开始。继续填充n,直到达到y 的限制,然后递增并“旋转”您的dx/dy 对。首先用-1 填充东西会有所帮助,这样您就可以检测以前填充的单元格和边缘。
  • 我尝试通过在 CodeBlocks 中评论来解决这个问题,并试图找到行/列数字如何更改值的模式
  • 更聪明的方法:线性计算,然后在显示时将其“打包”成螺旋形。要显示的位置有一个模式。在线性序列中,您会看到:n-1 x (n)、m-1 x (n+1) % 10 等。
  • 这是有助于首先在纸上寻找模式的方法之一。

标签: arrays c multidimensional-array


【解决方案1】:

前段时间我做了一个函数来在一个奇数大小的网格上显示数字 1 到 n。 原则是从中心开始,然后移动;

  • x = 1
  • 右边的x框
  • 底部有x框
  • x++
  • 左侧的x框
  • 顶部的 x 框
  • x++

通过这个简单的算法,你可以很容易地想象,也许从问题的中心开始并递减你的值,从中心开始似乎更容易。


这是说明上述解决方案的代码,当然可以针对您的问题进行调整,这只是一个线索。

#define WE 5

void    clock(int grid[WE][WE])
{
    int count;
    int i;
    int reach;
    int flag;
    int tab[2] = {WE / 2, WE / 2}; //x , y

    count = 0;
    flag = 0;
    i = 0;
    reach = 1;
    grid[tab[1]][tab[0]] = count;
    for (int j = 0; j < WE - 1 && grid[0][WE - 1] != pow(WE, 2) - 1; j++)
        for (i = 0; i < reach && grid[0][WE - 1] != pow(WE, 2) - 1; i++, reach++)
        {
            if(flag % 2 == 0)
            {
                for(int right = 0 ; right < reach ; right++, tab[0]++, count++, flag = 1)
                    grid[tab[1]][tab[0]] = count;
                if(reach < WE - 1)
                    for(int bottom = 0; bottom < reach; bottom++, count++, tab[1]++)
                        grid[tab[1]][tab[0]] = count;
            }
            else
            {
                for(int left = 0; left < reach; left++, count++, tab[0]--, flag = 0)
                    grid[tab[1]][tab[0]] = count;
                for(int top = 0; top < reach; top++, tab[1]--, count++)
                    grid[tab[1]][tab[0]] = count;
            }
        }
}

【讨论】:

  • 这是个好主意,我会确保尝试一下,因为我确实有另一个类似的问题。这个我已经解决了。谢谢
【解决方案2】:

我终于解决了。如果有人感兴趣,我是这样做的:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

//Fills the row number "row" with the number n
int fillRow(int m, int n, int arr[m][m], int row)
{
    int j;

    for(j=0;j<m;j++)
    {
        if(arr[row][j] == -1 || arr[row][j] == n-1) arr[row][j] = n;
    }

}

//Fills the column number "col" with the number n
int fillCol(int m, int n, int arr[m][m], int col)
{
    int i;

    for(i=0;i<m;i++)
    {
        if(arr[i][col] == -1 || arr[i][col] == n-1) arr[i][col] = n;
    }

}

int main()
{

    int n, m, i, j, r=1, c=1, row=-1, col=-1;

    scanf("%d %d",&n, &m);

    int arr[m][m];
    
    
    //Fill array with -1 everywhere
    for(i=0;i<m;i++)
    {
        for(j=0;j<m;j++)
        {
            arr[i][j] = -1;
        }

    }
    
    //Calculate which row/column to fill (variables row/col)
    //Fill row then column then row than column...
    for(i=0;i<2*m;i++)
    {
        if(i%2==0)
        {
            row = (r%2==0) ? m-r/2 : r/2;
            fillRow(m, n, arr, row);
            n++;
            r++;
        }
        else if(i%2==1)
        {
            col = (c%2==0) ? c/2-1 : m-c/2-1;
            fillCol(m, n, arr, col);
            n++;
            c++;
        }
    }
    
    //If an element is larger than 9, decrease it by 10
    //Prints the elements
    for(i=0;i<m;i++)
    {
        for(j=0;j<m;j++)
        {
            if(arr[i][j]>9) arr[i][j] -=10;
            printf("%d ",arr[i][j]);
        }
        printf("\n");
    }

    return 0;
}

【讨论】:

    猜你喜欢
    • 2021-11-06
    • 1970-01-01
    • 1970-01-01
    • 2021-12-26
    • 2014-07-10
    • 2021-03-03
    • 1970-01-01
    • 2018-01-27
    • 2014-12-23
    相关资源
    最近更新 更多