关于检查二维数组[C++]中的字段
About checking fields around in 2 dimensional array [C++]
我知道这听起来可能很愚蠢,但我得问问。
这是一件简单的事情。
你有一个二维数组,里面有元素或者是空的,然后你得到一些位置(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
是否为空。
我不使用height和width的维度数组,而是使用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);
}
我知道,你不会得到所有,但你应该知道我做了什么。
相关文章:
- 将结构字段的类型展开为可变模板参数
- 将位字段导出到数组
- 为了方便起见,我应该避免公开私有字段变量吗
- 当字段可以为null时,如何使用C++接口在Avro中写入数据
- 在java中读取c++字节的位字段
- 链接器找不到在虚拟类 c++ 中访问的静态字段的符号
- 私有字段对象与私有继承?
- 声明没有默认构造函数的字段
- C++内存模型和位字段的最大序列
- 声明为无效的变量或字段'...' Ardunio 编译器上的错误
- 如何在QByteArray中放置和检索位字段而不会感到痛苦?
- C++ win32 如何使密码字段可选并启用复制和粘贴?
- 如何通过UDP接收QByteArray并将其解析为位字段结构?
- 仅匹配集合中的某些字段
- 结构字段名称与 GDB 中的 STL 数组冲突
- 如何使用位字段将数据从二进制文件复制到结构中?
- 结构体和类的不同大小(),彼此具有相同的字段类型
- 如何避免在数据结构中包含存储为字段的类?
- 聚合初始化和删除的复制构造函数,也称为不可复制的 obejcts 作为字段
- C++:用户定义的类,以成员字段作为地址