Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- How to save space and time with a mirrored NxN array
- [ 0 3 9 2 ]
- [ 3 0 5 6 ]
- [ 9 5 0 1 ]
- [ 2 6 1 0 ]
- #include <QVector>
- #include <QDebug>
- #include <QString>
- class MirroredArray
- {
- public:
- MirroredArray(int sideLength)
- {
- values.fill(0, sideLength*(sideLength+1)/2);
- this->s = sideLength;
- }
- int get(int r, int c)
- {
- if(c > r)
- {
- return values.at(s*r-(r-1)*r/2 + c-r);
- }
- else
- {
- return values.at(s*c-(c-1)*c/2 + r-c);
- }
- }
- void set(int r, int c, int value)
- {
- if(c > r)
- {
- values[s*r-(r-1)*r/2 + c-r] = value;
- }
- else
- {
- values[s*c-(c-1)*c/2 + r-c] = value;
- }
- }
- int getSide()
- {
- return s;
- }
- QString contentsToString()
- {
- QString temp = "(" + QString::number(values.size()) + ") - ";
- for(int i = 0; i<values.size(); i++)
- temp += QString::number(i) + ", ";
- return temp;
- }
- private:
- QVector <int> values;
- int s;
- };
- 01234 (n=5)
- 5678
- 901
- 23
- 4
Add Comment
Please, Sign In to add comment