0

Here is my code, it is running on my computer:

const int m = 5;
const int n = 5;

// check whether the agent has visited the cells
int visited[m][n];


const int dx[] = {0, 1, -1, 0};
const int dy[] = {1, 0,  0, -1};

bool is_visited(int x, int y, int mat[m][n]){
    if (0 <= x and y >= 0 and x < m and y < n){
        if (visited[x][y] == false and mat[x][y] == 1) return true;
        else return false;
    }
    else return false;

    

}
int COUNT = 0;
unordered_map <int, int> match_query;
void FindConnectedComponents(int mat[m][n]){
    
    // check every unvisited cell in the mat
    for (int i = 0; i < m; ++i){
        for (int j = 0; j < n; ++j){

            if (visited[i][j] == false and mat[i][j] == 1){
                queue<pair<int, int> >q;
                //store coord of 1 
                q.push({i,j});  // now q is not empty
                // update visited
                visited[i][j] = true;

                COUNT = 0; 
                while (!q.empty()){
                    pair<int, int> temp_q = q.front();  
                    q.pop();  

                    int x = temp_q.first; int y = temp_q.second;
                    COUNT+=1;
                    
                    
                    for (int i = 0; i < 4; ++i){  
                        
                        int newX = x + dx[i];
                        int newY = y + dy[i];
                        if (is_visited(newX, newY, mat)){

                            visited[newX][newY] = true;
                            // continue store newX and newY on q to keep checking
                            q.push({newX, newY});

                        }

                    }

                }
            cout << COUNT <<" ";
            match_query[COUNT]++; 
            }

        }

    }
   
}

The problem is now I want to use a class and the value of m and n is not fixed anymore now m and should be int m = grid.size(); and int n = grid[0].size();. However, we can easily see that: the compiler immediately throws the error "grid is undeclared ".


class Solution {
    
    /* even if I try: 
  int m,n;
  int visited[m][n]; // ERROR: invalid use of non-static data member 'm'
                         

*/
   
public:
    
   static constexpr int dx[] = {0,1,-1,0};
   static constexpr int dy[] = {1,0,0,-1};
   
   int m = grid.size(); // ERROR 
   int n = grid[0].size(); // ERROR

   
   int visited[m][n];

   
   bool is_visited(int x, int y, vector<vector<char> >&grid){
    
        if (x >= 0 and x < grid.size() and y >= 0 and y < grid[0].size()){
            if (visited[x][y] == false and grid[x][y] =='1' ){
            
            return true;
            }
            else return false;
        }
        else return false;
        
        
        
    }
     
        
    int count = 0;
    int numIslands(vector<vector<char> >& grid) {
         
        for (int i= 0; i< grid.size(); ++i){
            for (int j = 0; i< grid[i].size(); ++j){
                if (grid[i][j] == '1' and visited[i][j] == false){
                    
                    visited[i][j] = true;
                    queue<pair<int, int> > q;
                    q.push({i,j});
                    
                    while (!q.empty()){
                        
                        pair<int, int> temp_q = q.front();
                        q.pop(); // delete q after storing it
                        int x = temp_q.first; int y = temp_q.second;
                        
                        for  (int i = 0; i< 4; ++i){
                            
                            int newX = x + dx[i];
                            int newY = y + dy[i];
                            if  (is_visited(newX, newY, grid) ){
                                
                                visited[newX][newY] = true;
                                q.push({newX, newY});
                                
                                
                                
                                
                            }
                            
                            
                        }
                        
                         
                    }
                    
                    
                    
                }
                count += 1;
              
            }
             
        }
        
        
        
        
       return count; 
        
        
    }
    
     
};

Can you guys please tell me how can I get the grid size for the m an n variables in order to pass visited that still is global?

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
Alexander
  • 3
  • 1
  • The compiler needs to know `n` and `m` at compile time, which requires knowing `grid` at compile time. It cannot work. Instead, use dynamic allocation to create a `grid` of the appropriate size in your `numIslands` method. – Botje Nov 18 '21 at 12:23
  • Does this answer your question? [How do you dynamically allocate a matrix?](https://stackoverflow.com/questions/1403150/how-do-you-dynamically-allocate-a-matrix) – Botje Nov 18 '21 at 12:24
  • 4
    Use standard C++ containers instead of fumbling your way through your own C-like constructions – Jabberwocky Nov 18 '21 at 12:33

0 Answers0