【问题标题】:How can I iterate over a quadruple linked 2-dimensional grid of data as if it were a 2-dimensional array?如何像二维数组一样迭代四重链接的二维数据网格?
【发布时间】:2012-04-10 22:51:29
【问题描述】:

如何像二维数组一样遍历四重链接的二维数据网格?

我的网格结构是:

typedef bool tile;

struct BLOCK;
typedef struct BLOCK block;

struct BLOCK {
 const block * to_the_left;
 const block * above;
 const block * to_the_right;
 const block * below;

 tile data;
};

typedef struct {
 const block * start;
} map;

我需要能够像二维数组一样遍历这个网格,这样我才能在屏幕上以起始块为中心显示地图的图块。

附言我最希望看到 C 中的解决方案(这是我为这个项目编写的代码)、C++、Haskell 或 Java 代码,因为这些都是我熟悉的语言,但任何语言都可以。我只需要算法。

P.S.S.S.为清楚起见,通过像二维数组一样迭代,我的意思是我需要获取 x 和 y 位置的索引作为变量。例如,我需要调用 mvaddch(y,x,'#')。

【问题讨论】:

    标签: data-structures grid recursive-datastructures


    【解决方案1】:

    这将按照与二维数组相同的顺序进行迭代

        BLOCK * process = upper_left_block;
        BLOCK * leftmost = process;
        while(true)
        {
            //<do stuff here>
            process = process->to_the_right;
            if(process == null)
            {
                process = leftmost->below;
                leftmost = process;
            }
            if(process == null)
                break;
        }
    

    【讨论】:

    • 在上面添加了我需要的解释,我还需要 x 和 y 变量。
    • 我正在调试它,它几乎可以工作,但在到达底部时不会停止。我想我将能够修复它。编辑:NVM。这是我生成网格时的错误。
    • 啊哈!我发现了错误。在设置process = leftmost->below 之后,应包含leftmost = process 的小行。不过我不知道这方面的礼仪。如果我应该等你换行然后我会接受帖子,如果我应该编辑帖子并将其放入编辑队列中,或者如果做其他事情。
    猜你喜欢
    • 1970-01-01
    • 2013-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-27
    • 1970-01-01
    • 2011-12-28
    相关资源
    最近更新 更多