如何迭代四维链接的二维数据网格,就像它是一个二维数组? [英] How can I iterate over a quadruple linked 2-dimensional grid of data as if it were a 2-dimensional array?

查看:218
本文介绍了如何迭代四维链接的二维数据网格,就像它是一个二维数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网格结构是:

如何迭代四维链接的二维网格数据? >

  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;

瓦数据;
};

typedef struct {
const block * start;
}地图;

我需要能够遍历这个网格,就像它是一个二维数组,所以我可以以围绕开始块的屏幕显示地图的瓦片。



PSS我最喜欢在C中看到一个解决方案(这是我为这个项目编写的代码),C ++,Haskell或者Java代码,因为这些都是我所熟悉的语言,但任何语言都很好。我只需要算法。



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

解决方案

订单好像是二维数组

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


How can I iterate over a quadruple linked 2-dimensional grid of data as if it were a 2-dimensional array?

My grid structure is:

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;

I need to be able to iterate over this grid like it is a 2-dimensional array so I can display tiles of the map on the screen centering around the start block.

P.S.S. I would most preferable like to see a solution in C, (this is what I'm coding in for this project), C++, Haskell, or Java code as those are languages I know well, but any language is fine. I just need the algorithm.

P.S.S.S. For clarity, by iterate over like a 2-dimensional array I mean I need to get an index into the x and y position as variables. For example, I need to do call mvaddch(y,x,'#').

解决方案

this will iterate in the same order as if it were a 2 dimensional array

    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;
    }

这篇关于如何迭代四维链接的二维数据网格,就像它是一个二维数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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