关于检查二维数组[C++]中的字段

About checking fields around in 2 dimensional array [C++]

本文关键字:C++ 字段 二维数组 于检查 检查      更新时间:2023-10-16

我知道这听起来可能很愚蠢,但我得问问。

这是一件简单的事情。

你有一个二维数组,里面有元素或者是空的,然后你得到一些位置(x和y),我必须从周围的自由字段中画一个字段。

我知道怎么做,只是看起来。。。优雅或漂亮。我做这件事的方式是检查我是否在最大左边、最大右边、顶部、底部等。然后如果周围的字段中有什么,然后rand()。

它太长了,看起来很不愉快。我不知道有没有更短的路?谢谢

对不起我的英语,尽我最大的努力。

可能存在一些性能问题。最挑剔的情况是,当你在角落里有点[x, y]时,rand()可能会多次选择无效元素,所以你必须rand()并再次检查。

我的方法是检查可用的邻居,并将每个有效的邻居推送到std::vector中。在那之后,只有一个随机数生成,它在这个向量中选择一个元素:

std::vector<Coordinate> validNeighbours;
// Coordinate is a struct with x and y integers, you can use std::pair<int, int> or the very pointers to the elements
if(/* has neighbour to the left*/)
    validNeighbours.push_back(Coordinate(x - 1, y));
// check in other directions
Coordinate c = validNeighbours[std::rand() % validNeighbours.size()];

如果您只有1x1数组(validNeighbours.size()为0),您可能还想在执行取模之前检查validNeighbours是否为空。

我不使用heightwidth的维度数组,而是使用height+2和width+2的维度数组。然后,可以使用任何有意义的伪值填充边界单元格。

这是一个学校项目,所以我不能使用STL。---给其中一个anwser。

但我有自己的解决方案:)我做了我的结构向量(但它像位置向量,而不是数组向量),我得到了这个代码,无论如何,我认为这是最短的方法,感谢所有的帮助:

Vector Organism::FindFreeSpace()
{
int height=home->GetHeight();
int width=home->GetWidth();
Vector* choices;
Vector shift;

if(location.x==0 || location.x==width-1)
{
    if(location.y==0 || location.y==height-1)
    {
        choices= new Vector[2];
        choices[0]=( location.x==0 ? Vector(1,0) : Vector(-1,0));
        choices[1]=( location.y==0 ? Vector(0,1) : Vector(0,-1));
        ShuffleChoices(choices, 2);
        for(int h=0;h<2;h++)
        {
            if(home->IsFree(choices[h]+location)==true)
            {
                location+=choices[h];
                shift=choices[h];
                delete[] choices;
                return shift;
            }
        }
    }
    else
    {
        choices=new Vector[3];
        choices[0]=( location.x==0 ? Vector(1,0) : Vector(-1,0));
        choices[1]=Vector(0,1);
        choices[2]=Vector(0,-1);
        ShuffleChoices(choices, 3);
        for(int h=0;h<3;h++)
        {
            if(home->IsFree(choices[h]+location)==true)
            {
                location+=choices[h];
                shift=choices[h];
                delete[] choices;
                return shift;
            }
        }
    }
}
else if(location.y==0 || location.y==height-1)
{
    choices=new Vector[3];
    choices[0]=( location.y==0 ? Vector(0,1) : Vector(0,-1));
    choices[1]=Vector(1,0);
    choices[2]=Vector(-1,0);
    ShuffleChoices(choices, 3);
    for(int h=0;h<3;h++)
    {
        if(home->IsFree(choices[h]+location)==true)
        {
            location+=choices[h];
            shift=choices[h];
            delete[] choices;
            return shift;
        }
    }

}
else
{
    choices=new Vector[4];
    choices[0]=Vector(0,1);
    choices[1]=Vector(1,0);
    choices[2]=Vector(-1,0);
    choices[3]=Vector(0,-1);
    ShuffleChoices(choices, 4);
    for(int h=0;h<4;h++)
    {
        if(home->IsFree(choices[h]+location)==true)
        {
            location+=choices[h];
            shift=choices[h];
            delete[] choices;
            return shift;
        }
    }
}

return Vector(0,0);
}

我知道,你不会得到所有,但你应该知道我做了什么。