gpt4 book ai didi

c++ - 优化数组元素的计算和设置

转载 作者:太空宇宙 更新时间:2023-11-04 11:56:22 28 4
gpt4 key购买 nike

有一个实例化对象的二维数组,11 X 11 字段。

一个或两个字段可以设置为主要字段,这意味着该字段符合 A 值。主场成为相对坐标系的中心,其中其附近的邻居(X 和 Y 上的 +/-1)符合 B 值,其他四个级别的邻居符合 C 值。其余字段(此 11 行/列示例中没有字段)符合 D - 默认值。 -1 的索引实际上是索引 11,因此索引 0 是索引 12。

如果数组中有两个主要字段,则设置更高等级的值。

问题:在 C++ 中为实例化对象的值计算和赋能 setter 的最佳方式是什么?

  • @rhalbersma 提供的第一个示例,更改为适合条件

PrimaryFieldsNet.h

#ifndef PRIMARYFIELDSNET_H
#define PRIMARYFIELDSNET_H

#define ABS(X) ((X) > 0 ? (X) : (-(X)))
#include <iostream>

using namespace std;

typedef pair<int, int> Point;

class PrimaryFieldsNet
{
private:
int m_rows;
int m_grid[11][11];
PrimaryFieldsNet() { }
int distance_x(Point const& a, Point const& b);
int distance_y(Point const& a, Point const& b);
int delta_x(Point const& a, Point const& b);
int delta_y(Point const& a, Point const& b);
int calculate(Point const& a, Point const& b);
void update_grid(Point const& pry_p1);
void update_grid(Point const& pry_p1, Point const& pry_p2);

public:
PrimaryFieldsNet(int rows);
void setup();
void setup(Point const& pry_p1);
void setup(Point const& pry_p1, Point const& pry_p2);
int* grid();
};

#endif

PrimaryFieldsNet.cpp

#include "PrimaryFieldsNet.h"

PrimaryFieldsNet::PrimaryFieldsNet(int rows)
{
m_rows = rows;
}

int* PrimaryFieldsNet::grid()
{
//cout << m_grid << endl;
return *m_grid;
}

int PrimaryFieldsNet::distance_x(Point const& a, Point const& b)
{
return a.first - b.first;
}

int PrimaryFieldsNet::distance_y(Point const& a, Point const& b)
{
return a.second - b.second;
}

int PrimaryFieldsNet::delta_x(Point const& a, Point const& b)
{
int d_x;

d_x = distance_x(a, b);

if (d_x < -(m_rows-1)/2)
d_x = a.first + m_rows - b.first;
else if (d_x > (m_rows-1)/2)
d_x = ABS(a.first - m_rows - b.first);
else
d_x = ABS(d_x);

return d_x;
}

int PrimaryFieldsNet::delta_y(Point const& a, Point const& b)
{
int d_y;

d_y = distance_y(a, b);

if (d_y < -(m_rows-1)/2)
d_y = a.second + m_rows - b.second;
else if (d_y > (m_rows-1)/2)
d_y = ABS(a.second - m_rows - b.second);
else
d_y = ABS(d_y);

return d_y;
}

int PrimaryFieldsNet::calculate(Point const& a, Point const& b)
{
int dmax, result;

dmax = max(delta_x(a, b), delta_y(a, b));

if (dmax < 2)
result = dmax;
else if (dmax < (m_rows+1)/2)
result = 2;
else
result = 3;

return result;
}

void PrimaryFieldsNet::update_grid(Point const& pry_p1)
{
for (int x = 0; x < m_rows; ++x)
for (int y = 0; y < m_rows; ++y)
m_grid[x][y] = calculate(Point(x,y), pry_p1);
}

void PrimaryFieldsNet::update_grid(Point const& pry_p1, Point const& pry_p2)
{
for (int x = 0; x < m_rows; ++x)
for (int y = 0; y < m_rows; ++y)
m_grid[x][y] = min(calculate(Point(x,y), pry_p1), calculate(Point(x,y), pry_p2));
}

void PrimaryFieldsNet::setup()
{
Point pry_p1((m_rows-1)/2,(m_rows-1)/2);
update_grid(pry_p1);
}

void PrimaryFieldsNet::setup(Point const& pry_p1)
{
update_grid(pry_p1);
}

void PrimaryFieldsNet::setup(Point const& pry_p1, Point const& pry_p2)
{
update_grid(pry_p1, pry_p2);
}

主要.cpp

#include <iostream>
#include "PrimaryFieldsNet.h"

using namespace std;

int main()
{
int* grid;

PrimaryFieldsNet pfnet(11);
//pfnet.setup(Point(5,2));
pfnet.setup(Point(10,6), Point(2,2));
grid = pfnet.grid();

for (int x = 0; x < 11; ++x) {
for (int y = 0; y < 11; ++y)
cout << *(grid + x*11 + y) << " ";
cout << "\n";
}

return 0;
}

示例输出为:

2 2 2 2 2 1 1 1 2 2 2 
2 1 1 1 2 2 2 2 2 2 2
2 1 0 1 2 2 2 2 2 2 2
2 1 1 1 2 2 2 2 2 2 2
2 2 2 2 2 2 2 2 2 2 2
2 2 2 2 2 2 2 2 2 2 2
2 2 2 2 2 2 2 2 2 2 2
2 2 2 2 2 2 2 2 2 2 2
2 2 2 2 2 2 2 2 2 2 2
2 2 2 2 2 1 1 1 2 2 2
2 2 2 2 2 1 0 1 2 2 2

最佳答案

您的问题本质上与 C++ 无关。看来您只需要遍历整个数组(使用双重嵌套循环,先行,然后列),并根据到 A 值字段的距离确定值。例如

SomeValueType values[4] = { A, B, C, D };

typedef std::pair<int, int> Point;
Point grid[12][12];
// allocate memory for grid

int distance(Point const& a, Point const& b)
{
int delta.x = a.first - b.first;
int delta.y = a.second - b.second;
return std::max(std::abs(delta.x), std::abs(delta.y));
}

Point center(0,0);

for (int x = 0; x < 12; ++x)
for (int y = 0; y < 12; ++y)
grid[x][y] = values[dist(Point(x,y), center)];

关于c++ - 优化数组元素的计算和设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15989749/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com