c++ - 螺旋迭代

标签 c++ algorithm matrix iteration

我需要一个算法来从中心向外扫描像素。问题是长度和尺寸不同,有时无法到达位置(见下图蓝色部分)。

Image1

为了进一步说明问题,我将展示示例输出:

Image2

如果比较图片,您会发现它呈螺旋状,输出与常规 for 循环相匹配,显然问题在于它无法正确打印蓝色部分

代码如下:

#include<iostream>
#include<string>
#include<math.h>

int arr[] = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 };
int arrSize = sizeof(arr) / sizeof(arr[0]);
int width = 5;
int height = 3;

void normal2DArray() {
    int index = 0;
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            std::cout << std::to_string(x) << "," << std::to_string(y) << " = " << std::to_string(arr[index]) << "\n";
            index++;
        }
    }
}

int convertToInex(int x, int y) {
    int left = x * y; // elements to the left
    int right = (width - x) * y; // elements to the right
    return left + right + x;
}

void spiralArray() {
    // calculate middle point, which is also the start point
    int x = round((float)width / 2) - 1;
    int y = round((float)height / 2) - 1;

    int direction = 0; // 0=right, 1=up, 2=left, 3=down
    int turnCounter = 1;
    int numSteps = 1;
    int step = 1;
    int index;

    while (true) {

        index = convertToInex(x, y); // defines the index position in arr
        std::cout << std::to_string(x) << "," << std::to_string(y) << " = " << std::to_string(arr[index]) << "\n";

        switch (direction) {
        case 0: x++; break;
        case 1: y--; break;
        case 2: x--; break;
        case 3: y++; break;
        }
        index = convertToInex(x, y);

        if (step % numSteps == 0) {
            direction = (direction + 1) % 4;
            turnCounter++;
            if (turnCounter % 2 == 0) numSteps++;
        }
        step++;
        if (step > arrSize) break;
    }
}

void main() {
    std::cout << "Output of Normal 2D Array:\n";
    normal2DArray();

    std::cout << "\n"; // better spacing

    std::cout << "Output of Spiral Array:\n";
    spiralArray();
}

我试图让代码尽可能简单和小巧。它应该可以导入和使用。 是的,我已经在网上搜索了我的答案,但我没有在这里找到任何可以掩盖问题的东西,也没有像我这样的类似设置(1D arr 和组合的 2D 数组 WIDTH/HEIGHT),而且肯定不是在 c++ 中。

❗我还需要一个适用于所有宽度和高度以及排列尺寸的解决方案,也适用于任何一侧❗

我希望您能为我提供有用的答案,并感谢您提供又好又快的算法实现/优化

编辑: 感谢此线程中的回复。我决定采用 @ldog 中的解决方案现在,尽管我对此并不完全满意。

以下是编辑后的代码部分:

    int failcounter = 0;
    while (true) {
    index = convertToInex(x, y); // defines the index position in arr
    if (index < 0 || index > arrSize) failcounter++;
    else std::cout << std::to_string(x) << "," << std::to_string(y) << " = " << std::to_string(arr[index]) << "\n";

// unchanged code inbetween

if (step > arrSize + failcounter) break;

最佳答案

根据您的评论:

@Beta they don't need to connect. It just has to detect that it's outside the array size (in that case -1) and don't scan them and find the next continue point. So it would continue like this: 5, 1, 6, 11

您似乎并不关心螺旋线是否“越界”。在这种情况下,简单的答案是,将没有螺旋的形状嵌入一个总是保证有螺旋的形状中。

因此,如果您的输入矩形是 N x M,则将其嵌入到大小为 max(M,N) x max(M,N) 的矩形中,求解后者的问题,打印时忽略原始问题中不存在的数字。然后,您打印的序列将始终是唯一的,具体取决于嵌入的方式。最合理的嵌入会尝试将较小的矩形尽可能地居中在较大的矩形中,但这取决于您。

在这种情况下,您不需要算法,因为如果您愿意做簿记并找出所涉及的公式,您可以分析计算所有内容。

关于c++ - 螺旋迭代,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71918692/

相关文章:

r - 比较多行并在 R 或 Excel 中创建矩阵

c++ - 使用 CPR 从 C++ 调用 HTTP?

Java 时间复杂度 O(n^2/3)

algorithm - 如何优化Apriori算法?

python - 如何向量化双线性和二次形式的评估?

c++ - "basic"类的运算符重载

C++ boost : How To Run boost_check_library. py

c++ - 错误 : ‘infinity’ does not name a type

使用 getenv 调用 CreateProcessAsUser 的 C++ LPTSTR 问题

algorithm - 最大二分匹配(ford-fulkerson)