Finding all adjacent elements in a 2D array

后端 未结 5 1490
半阙折子戏
半阙折子戏 2021-02-15 18:07

I am working on a project where at one point I am stuck.

My question is for example I have the following 2D array containing 3 different integers.

2 2 2          


        
5条回答
  •  耶瑟儿~
    2021-02-15 19:00

    I love this kind of problems :-) so here it is my answer. As said by Frerich Raabe, this can be solved with a floodFill function. For example, opencv library would provide such a function off the shelf.

    Please forgive me if in the following code you'll find traces of C++, in case they should be simple to be replaced.

    typedef struct Point {
       int x;
       int y;
    } Point;
    
    int areaOfBiggestContiguousRegion(int* mat,int nRows, int nCols) {
      int maxArea = 0;
      int currValue, queueSize, queueIndex;  
      int* aux;
      Point queue[1000]; //Stores the points I need to label
      Point newPoint, currentPoint;
      int x,y,x2,y2;
      //Code: allocate support array aux of same size of mat
      //Code: fill aux of zeros
    
      for (y = 0; y < nRows; y++)
        for (x = 0; x < nCols; x++)
          if (aux[y * nCols + x] == 0) {//I find a pixel not yet labeled, my seed for the next flood fill
            queueIndex = 0; //Contains the index to the next element in the queue
            queueSize = 0;
    
            currValue = mat[y * nCols + x]; //The "color" of the current spot
            aux[y * nCols + x] = 1;
            newPoint.x = x;
            newPoint.y = y;
            queue[queueSize] = newPoint;
            queueSize++; 
    
            while(queueIndex != queueSize) {
              currPoint = queue[queueIndex];
              queueIndex++;
    
              //Look left, right, up, down
    
              x2 = currPoint.x - 1;
              y2 = currPoint.y;
              //Some copy & paste, sorry I have been too long on C++ to remember correctly about C functions
              if (x2 >= 0 && aux[y2 * nCols + x2] == 0 && mat[y2 * nCols + x2] == currValue) {
                aux[y2 * nCols + x2] = 1;
                newPoint.x = x2;
                newPoint.y = y2;
                queue[queueSize] = newPoint;
                queueSize++; 
              }
    
              x2 = currPoint.x + 1;
              y2 = currPoint.y;
              //Some copy & paste, sorry I have been too long on C++ to remember correctly about C functions
              if (x2 < nCols && aux[y2 * nCols + x2] == 0 && mat[y2 * nCols + x2] == currValue) {
                aux[y2 * nCols + x2] = 1;
                newPoint.x = x2;
                newPoint.y = y2;
                queue[queueSize] = newPoint;
                queueSize++; 
              }
    
              x2 = currPoint.x;
              y2 = currPoint.y - 1;
              //Some copy & paste, sorry I have been too long on C++ to remember correctly about C functions
              if (y2 >= 0 && aux[y2 * nCols + x2] == 0 && mat[y2 * nCols + x2] == currValue) {
                aux[y2 * nCols + x2] = 1;
                newPoint.x = x2;
                newPoint.y = y2;
                queue[queueSize] = newPoint;
                queueSize++; 
              }
    
              x2 = currPoint.x;
              y2 = currPoint.y + 1;
              //Some copy & paste, sorry I have been too long on C++ to remember correctly about C functions
              if (y2 < nRows && aux[y2 * nCols + x2] == 0 && mat[y2 * nCols + x2] == currValue) {
                aux[y2 * nCols + x2] = 1;
                newPoint.x = x2;
                newPoint.y = y2;
                queue[queueSize] = newPoint;
                queueSize++; 
              }
            } //while
    
          if (queueSize > maxArea)
            maxArea = queueSize; //If necessary we could store other details like currentValue
          }//if (aux...
    
    return maxArea;
    }
    

    Note: In C++ using std containers and a constructor for Point it becomes much more compact

提交回复
热议问题