1

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

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

如果数组中有两个主要字段,则设置较高的排名值。

问题:在 C++ 中计算实例化对象的值并为其设置功率的最佳方法可能是什么?

  • @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);
}

主文件

#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
4

1 回答 1

1

您的问题本质上与 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)];
于 2013-04-13T16:42:04.457 回答