7

単位正方形をサブ長方形に分割する (X,Y) 座標のセットがあります。私の座標が -

         (    x1,    y1)    (    x2,    y2)      

         (0.0000,0.0000)    (0.3412,0.4175)   
         (0.7445,0.0000)    (1.0000,0.6553)   
         (0.7445,0.6553)    (1.0000,1.0000)   
         (0.0000,0.6553)    (0.7445,1.0000)   
         (0.3412,0.0000)    (0.7445,0.4175)   
         (0.3412,0.4175)    (0.7445,0.6553)   
         (0.0000,0.4175)    (0.3412,0.6553)....etc (total 10,000 coordinates)

例として、私は 16 セットのデータのみを取得し、これらの座標は私の正方形を次のように分割します-

ここに画像の説明を入力

類似ボックスの定義

隣接するボックスの数が類似しているボックスは、類似のボックスと見なされます。ボックス[8]、ボックス[13]などの上の画像には、4つの最近傍があります。したがって、それらは類似のボックスと見なされます。

以下の画像はこれを明確にする必要があります-

ここに画像の説明を入力

::私の問題::

私たちが見ることができる画像から-

box[8]の場合、最も近いボックスは次のとおりです。

box(1) (4 つの隣接がある)

box[4] (これにも 4 つの隣接があります)

box[14] (4 つの隣接がある)

box[16] (4 つの隣接がある)

したがって、この場合、最も近いボックスの隣人の合計 = 4+4+4+4 =16

再びbox[13]の場合、最も近いボックスは次のとおりです。

box[3] (6 個の近隣がある)

box[5] (これにも 4 つの隣接があります)

box[6] (隣人が3人いる)

box[12] (隣人が3人いる)

したがって、この場合、最も近いボックスの隣人の合計 = 6+4+3+3 =16

そして、ここで (同様の箱) box[8] と box[13] の隣人の合計 = 16+16 =32.

同様に、4 つの隣接ボックスを持つすべてのボックスをグループ化し、最も近いボックスの隣接ボックスの合計を見つけたいと考えています。同様のグループごとに続けます。

マイコード

これが私のコードです。

#include <iostream>
#include <cstdlib>
#include <vector>
#include <stdio.h>

using namespace std;

class Rect {
public:
double x1, x2, y1, y2; // coordinates

Rect(double X1, double Y1, double X2, double Y2) {
  if (X1 < X2) {
    x1 = X1; x2 = X2;
  } else {
    x2 = X1; x1 = X2;
  }
  if (Y1 < Y2) {
    y1 = Y1; y2 = Y2;
  } else {
    y2 = Y1; y1 = Y2;
  }


}

bool isAdjacent(Rect rect) {
    if (x1 == rect.x1 || x1 == rect.x2 ||
        x2 == rect.x1 || x2 == rect.x2) {
      // use only < when comparing y1 and rect.y2 avoids sharing only a corner
      if (y1 >= rect.y1 && y1 < rect.y2) {
        return true;
      }
      if (y2 > rect.y1 && y2 <= rect.y2) {
        return true;
      }
      if (rect.y1 >= y1 && rect.y1 < y2) {
        return true;
      }
      if (rect.y2 > y1 && rect.y2 <= y2) {
        return true;
      }
    }
    if (y1 == rect.y1 || y1 == rect.y2 ||
        y2 == rect.y1 || y2 == rect.y2) {
      if (x1 >= rect.x1 && x1 < rect.x2) {
        return true;
      }
      if (x2 > rect.x1 && x2 <= rect.x2) {
        return true;
      }
      if (rect.x1 >= x1 && rect.x1 < x2) {
        return true;
      }
      if (rect.x2 > x1 && rect.x2 <= x2) {
        return true;
      }
    }
    return false;
  }

};



void isNearest(int b){

vector<Rect> rects;     
                //Rect(  x1 ,  y1  ,   x2  ,  y2   ) 
  rects.push_back(Rect(0.0000,0.0000, 0.8147,0.1355));
  rects.push_back(Rect(0.8147,0.0000, 1.0000,0.1355));

  rects.push_back(Rect(0.8147,0.1355, 0.9058,0.8350));
  rects.push_back(Rect(0.0000,0.1355, 0.1270,0.9689));

  rects.push_back(Rect(0.9058,0.1355, 0.9134,0.2210));
  rects.push_back(Rect(0.9058,0.8350, 1.0000,1.0000));
  rects.push_back(Rect(0.8147,0.8350, 0.9058,1.0000));


  rects.push_back(Rect(0.1270,0.1355, 0.6324,0.3082));
  rects.push_back(Rect(0.1270,0.9689, 0.8147,1.0000));
  rects.push_back(Rect(0.0000,0.9689, 0.1270,1.0000));

  rects.push_back(Rect(0.9134,0.1355, 1.0000,0.2210));
  rects.push_back(Rect(0.9134,0.2210, 1.0000,0.8350));
  rects.push_back(Rect(0.9058,0.2210, 0.9134,0.8350));


  rects.push_back(Rect(0.6324,0.1355, 0.8147,0.3082));
  rects.push_back(Rect(0.6324,0.3082, 0.8147,0.9689));
  rects.push_back(Rect(0.1270,0.3082, 0.6324,0.9689));


  int nearBox_count = 0;

  double TotalArea=0;


  for (int x = 0; x < rects.size(); ++x) {

    if (rects[b].isAdjacent(rects[x])) {


      if (x==b) {
continue; //this is our box , so do not count it.
}


nearBox_count++;

printf("box[%d] is nearest to box[%d]  \n", (b+1), (x+1));

}
}

printf("Total number of nearest box for [%d] is %d  \n",(b+1),nearBox_count );
printf("\n");

}


int main() {

  for (int i = 0; i < 16; ++i)
  {
    isNearest(i);
  }

return 0;
}

このような正しい結果が得られます-

box[1] is nearest to box[2]  
box[1] is nearest to box[4]  
box[1] is nearest to box[8]  
box[1] is nearest to box[14]  
Total number of nearest box for [1] is 4  

box[2] is nearest to box[1]  
box[2] is nearest to box[3]  
box[2] is nearest to box[5]  
box[2] is nearest to box[11]  
Total number of nearest box for [2] is 4  

box[3] is nearest to box[2]  
box[3] is nearest to box[5]  
box[3] is nearest to box[7]  
box[3] is nearest to box[13]  
box[3] is nearest to box[14]  
box[3] is nearest to box[15]  
Total number of nearest box for [3] is 6  

box[4] is nearest to box[1]  
box[4] is nearest to box[8]  
box[4] is nearest to box[10]  
box[4] is nearest to box[16]  
Total number of nearest box for [4] is 4  

box[5] is nearest to box[2]  
box[5] is nearest to box[3]  
box[5] is nearest to box[11]  
box[5] is nearest to box[13]  
Total number of nearest box for [5] is 4  

box[6] is nearest to box[7]  
box[6] is nearest to box[12]  
box[6] is nearest to box[13]  
Total number of nearest box for [6] is 3  

box[7] is nearest to box[3]  
box[7] is nearest to box[6]  
box[7] is nearest to box[9]  
box[7] is nearest to box[15]  
Total number of nearest box for [7] is 4  

box[8] is nearest to box[1]  
box[8] is nearest to box[4]  
box[8] is nearest to box[14]  
box[8] is nearest to box[16]  
Total number of nearest box for [8] is 4  

box[9] is nearest to box[7]  
box[9] is nearest to box[10]  
box[9] is nearest to box[15]  
box[9] is nearest to box[16]  
Total number of nearest box for [9] is 4  

box[10] is nearest to box[4]  
box[10] is nearest to box[9]  
Total number of nearest box for [10] is 2  

box[11] is nearest to box[2]  
box[11] is nearest to box[5]  
box[11] is nearest to box[12]  
Total number of nearest box for [11] is 3  

box[12] is nearest to box[6]  
box[12] is nearest to box[11]  
box[12] is nearest to box[13]  
Total number of nearest box for [12] is 3  

box[13] is nearest to box[3]  
box[13] is nearest to box[5]  
box[13] is nearest to box[6]  
box[13] is nearest to box[12]  
Total number of nearest box for [13] is 4  

box[14] is nearest to box[1]  
box[14] is nearest to box[3]  
box[14] is nearest to box[8]  
box[14] is nearest to box[15]  
Total number of nearest box for [14] is 4  

box[15] is nearest to box[3]  
box[15] is nearest to box[7]  
box[15] is nearest to box[9]  
box[15] is nearest to box[14]  
box[15] is nearest to box[16]  
Total number of nearest box for [15] is 5  

box[16] is nearest to box[4]  
box[16] is nearest to box[8]  
box[16] is nearest to box[9]  
box[16] is nearest to box[15]  
Total number of nearest box for [16] is 4  

最も近いボックスを識別して隣接するボックスの数を数えることはできますが、同様のボックスをグループ化して (上記のように) 合計を見つける方法がわかりませんでした。

そして、私はここで立ち往生しています。誰でも私を助けることができますか?

更新されたコード スニペット

vector<CheckRect> rects;

unsigned isNearest(unsigned b, vector<unsigned>& neighbours) {

  unsigned nearBox_count = 0;

  for (unsigned x = 0; x < rects.size(); ++x) {
    if (rects[b].isAdjacent(rects[x])) {
      if (x==b) continue; //this is our box , so do not count it.
      nearBox_count++;
      printf("box[%d] is nearest to box[%d]  \n", (b+1), (x+1));
      neighbours.push_back(x);
    }
  }

  printf("Total number of nearest box for [%d] is %d  \n",
        (b+1), nearBox_count );
  printf("\n");

  return nearBox_count;
}

int main(){

cin>>N;

for(int b=0; b<N; b++){

  ifstream inputFile1("RectCoordinates.txt"); //input from the file previously generated
  int rect_number;
  double xa0,ya0,xa1,ya1;
  int neighbours;
  isNearest( b, &neighbours);// This is the line that causing my ERROR


  }
 vector<unsigned> nearBox_count(rects.size());
  vector< vector<unsigned> > neighbours(rects.size());
  for (unsigned i = 0; i < rects.size(); ++i) {
    nearBox_count[i] = isNearest(i, neighbours[i]);
  }

  // Calculate the sums of neighbouring boxes
  vector<unsigned> neighCount(rects.size(), 0);
  for (unsigned i = 0; i < rects.size(); i++) {
    for (unsigned j = 0; j < neighbours[i].size(); j++) {
      neighCount[i] += nearBox_count[neighbours[i][j]];
    }
  }

  // Calculate your result
  map<unsigned,unsigned> finalCount;
  for (unsigned i = 0; i < rects.size(); i++)
  {
    if (finalCount.count(nearBox_count[i]) == 0)
      finalCount[nearBox_count[i]] = neighCount[i];
    else
      finalCount[nearBox_count[i]] += neighCount[i];
  }

  // Print the result
  for (map<unsigned,unsigned>::iterator it = finalCount.begin();
        it != finalCount.end(); ++it) {
    printf("Sum neighbours for the neighbours of similar boxes with %d "
           "neighbours is %d\n", it->first, it->second);
  }

  return 0;
}

エラーが表示されます-

ss.cpp: In function ‘int main()’:
ss.cpp:102:29: error: invalid initialization of reference of type ‘std::vector<unsigned int>&’ from expression of type ‘unsigned int’
ss.cpp:22:10: error: in passing argument 2 of ‘unsigned int isNearest(unsigned int, std::vector<unsigned int>&)’

どうすれば修正できますか?

4

4 に答える 4

1

これらすべてを大幅に簡素化したい場合は、コベレフスキー氏の推奨事項を使用できると思います。

#include <iostream>
#include <cstdlib>
#include <vector>
#include <stdio.h>

using namespace std;

class Rect {
public:
double x1, x2, y1, y2; // coordinates

//methods
Rect(double X1, double Y1, double X2, double Y2) {
  if (X1 < X2) {
    x1 = X1; x2 = X2;
  } else {
    x2 = X1; x1 = X2;
  }
  if (Y1 < Y2) {
    y1 = Y1; y2 = Y2;
  } else {
    y2 = Y1; y1 = Y2;
  }
}

~Rect()
{
};

int numNeighbors() const { return neighbors.size();}
int sumOfNeighbors() const { int res(0); for(size_t i=0;i< neighbors.size();++i) res += neighbors[i]->numNeighbors(); return res;}
std::vector<Rect*> getNeighbors() {return neighbors;};

void addNeighbor(Rect* newNeighbor) {neighbors.push_back(newNeighbor);}

//data
std::vector<Rect*> neighbors;

bool isAdjacent(Rect* rect) {
    if (x1 == rect->x1 || x1 == rect->x2 ||
        x2 == rect->x1 || x2 == rect->x2) {
      // use only < when comparing y1 and rect->y2 avoids sharing only a corner
      if (y1 >= rect->y1 && y1 < rect->y2) {
        return true;
      }
      if (y2 > rect->y1 && y2 <= rect->y2) {
        return true;
      }
      if (rect->y1 >= y1 && rect->y1 < y2) {
        return true;
      }
      if (rect->y2 > y1 && rect->y2 <= y2) {
        return true;
      }
    }
    if (y1 == rect->y1 || y1 == rect->y2 ||
        y2 == rect->y1 || y2 == rect->y2) {
      if (x1 >= rect->x1 && x1 < rect->x2) {
        return true;
      }
      if (x2 > rect->x1 && x2 <= rect->x2) {
        return true;
      }
      if (rect->x1 >= x1 && rect->x1 < x2) {
        return true;
      }
      if (rect->x2 > x1 && rect->x2 <= x2) {
        return true;
      }
    }
    return false;
  }

};

vector<Rect*> rects;

void CalculateAdjacentsForRect(unsigned int rects_element){

    for (unsigned int x = 0; x < rects.size(); x++) {
        if (rects[rects_element]->isAdjacent(rects[x])) {
            if (x==rects_element) {
                continue; //this is our box , so do not count it.
            }
            rects[rects_element]->addNeighbor(rects[x]);
        }
    }
}

const int MAX_ADJACENT_RECTS = 10;

int main() {

                    //Rect(  x1 ,  y1  ,   x2  ,  y2   )
    rects.push_back(&Rect(0.0000,0.0000, 0.8147,0.1355));
    rects.push_back(&Rect(0.8147,0.0000, 1.0000,0.1355));

    rects.push_back(&Rect(0.8147,0.1355, 0.9058,0.8350));
    rects.push_back(&Rect(0.0000,0.1355, 0.1270,0.9689));

    rects.push_back(&Rect(0.9058,0.1355, 0.9134,0.2210));
    rects.push_back(&Rect(0.9058,0.8350, 1.0000,1.0000));
    rects.push_back(&Rect(0.8147,0.8350, 0.9058,1.0000));


    rects.push_back(&Rect(0.1270,0.1355, 0.6324,0.3082));
    rects.push_back(&Rect(0.1270,0.9689, 0.8147,1.0000));
    rects.push_back(&Rect(0.0000,0.9689, 0.1270,1.0000));

    rects.push_back(&Rect(0.9134,0.1355, 1.0000,0.2210));
    rects.push_back(&Rect(0.9134,0.2210, 1.0000,0.8350));
    rects.push_back(&Rect(0.9058,0.2210, 0.9134,0.8350));


    rects.push_back(&Rect(0.6324,0.1355, 0.8147,0.3082));
    rects.push_back(&Rect(0.6324,0.3082, 0.8147,0.9689));
    rects.push_back(&Rect(0.1270,0.3082, 0.6324,0.9689));

    for (unsigned int i = 0; i < rects.size(); i++)
    {
        CalculateAdjacentsForRect(i);
    }

    for (unsigned int i = 0; i < rects.size(); i++)
    {
        cout << "\nRect" << i << " has a neighbor sum of " << rects[i]->sumOfNeighbors();
    }

    cout << "\n";

    for (int ix = 0; ix < MAX_ADJACENT_RECTS; ix++)
    {
        int num_rects_with_this_num_of_adjacents = 0;
        int num_adjacents_total_for_similar_rects = 0;
        for (unsigned int i = 0; i < rects.size(); i++) {
            if ( rects[i]->numNeighbors() == ix ) {
               num_rects_with_this_num_of_adjacents++;
               num_adjacents_total_for_similar_rects += rects[i]->sumOfNeighbors();
            }
        }
        cout << "\nThere are " << num_rects_with_this_num_of_adjacents << " rects with " << ix << " adjacent rects. They have a cum neighbor sum of " << num_adjacents_total_for_similar_rects;
    }

    return 0;
}
于 2013-08-22T14:45:15.407 に答える
0

あなたの価値を計算しようとするだけでなく、私はあなたのコードにいくつかの小さな変更を加えました。

すべてのリスト インデックスが負になることはなく、将来的に非常に多くの長方形が作成される可能性が高いため、すべての を にすることをお勧めしintますunsigned。これには、以下のコードでの符号付き整数と符号なし整数の比較に関する特定のコンパイラ警告を抑制するという追加の利点があります。

提案する 2 番目の変更は、rectsを繰り返すたびに宣言するのではなく、1 回だけ宣言することですisNearest。以下のコードではrects、グローバル変数を作成し、それを初期化する別の関数を作成することでこれを実現しています。グローバル変数を作成することで、すべてのs を にrects置き換えることができるようになりました (完全なデータセットに追加するときに変更を忘れる可能性を減らします)。16rects.size()16

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <vector>
#include <map>
#include <stdio.h>

using namespace std;

class Rect {
public:
  double x1, x2, y1, y2; // coordinates

  Rect(double X1, double Y1, double X2, double Y2) {
    if (X1 < X2) {
      x1 = X1; x2 = X2;
    } else {
      x2 = X1; x1 = X2;
    }
    if (Y1 < Y2) {
      y1 = Y1; y2 = Y2;
    } else {
      y2 = Y1; y1 = Y2;
    }
}

bool isAdjacent(Rect rect) {
    if (x1 == rect.x1 || x1 == rect.x2 ||
        x2 == rect.x1 || x2 == rect.x2) {
      // use only < when comparing y1 and rect.y2 avoids sharing only a corner
      if (y1 >= rect.y1 && y1 < rect.y2) {
        return true;
      }
      if (y2 > rect.y1 && y2 <= rect.y2) {
        return true;
      }
      if (rect.y1 >= y1 && rect.y1 < y2) {
        return true;
      }
      if (rect.y2 > y1 && rect.y2 <= y2) {
        return true;
      }
    }
    if (y1 == rect.y1 || y1 == rect.y2 ||
        y2 == rect.y1 || y2 == rect.y2) {
      if (x1 >= rect.x1 && x1 < rect.x2) {
        return true;
      }
      if (x2 > rect.x1 && x2 <= rect.x2) {
        return true;
      }
      if (rect.x1 >= x1 && rect.x1 < x2) {
        return true;
      }
      if (rect.x2 > x1 && rect.x2 <= x2) {
        return true;
      }
    }
    return false;
  }
};

vector<Rect> rects;

unsigned isNearest(unsigned b, vector<unsigned>& neighbours) {

  unsigned nearBox_count = 0;

  for (unsigned x = 0; x < rects.size(); ++x) {
    if (rects[b].isAdjacent(rects[x])) {
      if (x==b) continue; //this is our box , so do not count it.
      nearBox_count++;
      printf("box[%d] is nearest to box[%d]  \n", (b+1), (x+1));
      neighbours.push_back(x);
    }
  }

  printf("Total number of nearest box for [%d] is %d  \n",
        (b+1), nearBox_count );
  printf("\n");

  return nearBox_count;
}

void initRects(void) {

                //Rect(  x1 ,  y1  ,   x2  ,  y2   ) 
  rects.push_back(Rect(0.0000,0.0000, 0.8147,0.1355));
  rects.push_back(Rect(0.8147,0.0000, 1.0000,0.1355));

  rects.push_back(Rect(0.8147,0.1355, 0.9058,0.8350));
  rects.push_back(Rect(0.0000,0.1355, 0.1270,0.9689));

  rects.push_back(Rect(0.9058,0.1355, 0.9134,0.2210));
  rects.push_back(Rect(0.9058,0.8350, 1.0000,1.0000));
  rects.push_back(Rect(0.8147,0.8350, 0.9058,1.0000));


  rects.push_back(Rect(0.1270,0.1355, 0.6324,0.3082));
  rects.push_back(Rect(0.1270,0.9689, 0.8147,1.0000));
  rects.push_back(Rect(0.0000,0.9689, 0.1270,1.0000));

  rects.push_back(Rect(0.9134,0.1355, 1.0000,0.2210));
  rects.push_back(Rect(0.9134,0.2210, 1.0000,0.8350));
  rects.push_back(Rect(0.9058,0.2210, 0.9134,0.8350));


  rects.push_back(Rect(0.6324,0.1355, 0.8147,0.3082));
  rects.push_back(Rect(0.6324,0.3082, 0.8147,0.9689));
  rects.push_back(Rect(0.1270,0.3082, 0.6324,0.9689));
}

void readRects(const string& filename) {

  ifstream fpInput(filename.c_str());
  double dTemp[4];

  while (true) {
    for (unsigned i = 0; i < 4; i++) fpInput >> dTemp[i];
    if (!fpInput.good()) break;
    rects.push_back(Rect(dTemp[0], dTemp[1], dTemp[2], dTemp[3]));
  }

  fpInput.close();
}

int main() {

  // Initialize the vector rects
  //initRects();
  readRects("RectCoordinates.txt");

  vector<unsigned> nearBox_count(rects.size());
  vector< vector<unsigned> > neighbours(rects.size());
  for (unsigned i = 0; i < rects.size(); ++i) {
    nearBox_count[i] = isNearest(i, neighbours[i]);
  }

  // Calculate the sums of neighbouring boxes
  vector<unsigned> neighCount(rects.size(), 0);
  for (unsigned i = 0; i < rects.size(); i++) {
    for (unsigned j = 0; j < neighbours[i].size(); j++) {
      neighCount[i] += nearBox_count[neighbours[i][j]];
    }
  }

  // Calculate your result
  map<unsigned,unsigned> finalCount;
  for (unsigned i = 0; i < rects.size(); i++)
  {
    if (finalCount.count(nearBox_count[i]) == 0) {
      finalCount[nearBox_count[i]] = neighCount[i];
    } else {
      finalCount[nearBox_count[i]] += neighCount[i];
    }
  }

  // Print the result
  for (map<unsigned,unsigned>::iterator it = finalCount.begin();
        it != finalCount.end(); ++it) {
    printf("Sum neighbours for the neighbours of similar boxes with %d "
           "neighbours is %d\n", it->first, it->second);
  }

  return 0;
}

更新:Rect上記のコードは、ソース ファイル内で s を指定するか、外部ファイルから読み込むことで使用できるようになりました。上記の変更例では、入力ファイルはRectCoordinates.txt次のとおりです。

0.0000  0.0000  0.8147  0.1355
0.8147  0.0000  1.0000  0.1355

0.8147  0.1355  0.9058  0.8350
0.0000  0.1355  0.1270  0.9689

0.9058  0.1355  0.9134  0.2210
0.9058  0.8350  1.0000  1.0000
0.8147  0.8350  0.9058  1.0000


0.1270  0.1355  0.6324  0.3082
0.1270  0.9689  0.8147  1.0000
0.0000  0.9689  0.1270  1.0000

0.9134  0.1355  1.0000  0.2210
0.9134  0.2210  1.0000  0.8350
0.9058  0.2210  0.9134  0.8350


0.6324  0.1355  0.8147  0.3082
0.6324  0.3082  0.8147  0.9689
0.1270  0.3082  0.6324  0.9689

上記は結果を出力します:

Sum neighbours for the neighbours of similar boxes with 2 neighbours is 8
Sum neighbours for the neighbours of similar boxes with 3 neighbours is 32
Sum neighbours for the neighbours of similar boxes with 4 neighbours is 165
Sum neighbours for the neighbours of similar boxes with 5 neighbours is 22
Sum neighbours for the neighbours of similar boxes with 6 neighbours is 25
于 2013-08-21T17:22:29.863 に答える
0

ベクトルを使用できると思いますが、この方法では大量のメモリ スペースが必要になる可能性があります。たとえば、vector 4neighbours を作成します。一見すると、各ボックスは少なくとも 4 つの隣接要素であると言えます (ただし、考えられる隣接要素の数ごとにこれを行う必要があります。計算できることを願っていますが、そうではありません)。超ビッグ)。次に、各四角形の近傍の数を確認するときに、対応するベクトルでプッシュバックを使用します。

printf("Total number of nearest box for [%d] is %d  \n",(b+1),nearBox_count );
if(nearBox_count == 4)
 4neighbours.pushback(rects[b]);
else if (nearBox_count == 5)
 5neighbours.pushback(rects[b]);
else if -etc-

次に、ベクトルを調べて、ベクトルの各メンバーについて隣人をチェックし、隣人ごとに隣人を計算して合計します (2 つのループをネストします - ループ内のループ)

例:コード化されていません

int TotalNeighbours = 0;
while(go through all the boxes in the vector)
{
    while(go through all the neighbours of each box)
    {
     TotalNeighbours++;
    }
}

上記の制限はありますが、このようなものが機能する可能性があると思います。ネイバーの最大数に応じて、次のようになります。

  • 大量のメモリ使用量;
  • 大量の記述コード (大量の if ステートメント);

編集: 1 つのボックスには隣人が 0 人いる可能性があり、残りは 4 つではなく、少なくとも 1 つの隣人を持つことができます。

于 2013-08-21T16:58:17.943 に答える