Connected Cell in a Grid

Hackerrank


Description:

Consider a matrix with rows and columns, where each cell contains either a or a and any cell containing a is called a filled cell. Two cells are said to be connected if they are adjacent to each other horizontally, vertically, or diagonally; in other words, cell is connected to cells , , , , , , , and , provided that the location exists in the matrix for that .

1 1 1
1 x 1
1 1 1

周围全是connected。

If one or more filled cells are also connected, they form a region. Note that each cell in a region is connected to at least one other cell in the region but is not necessarily directly connected to all the other cells in the region.

Task Given an matrix, find and print the number of cells in the largest region in the matrix. Note that there may be more than one region in the matrix.

Example:

Sample Input

4
4
1 1 0 0
0 1 1 0
0 0 1 0
1 0 0 0
Sample Output

5

Explanation

The diagram below depicts two regions of the matrix; for each region, the component cells forming the region are marked with an X:

X X 0 0     1 1 0 0
0 X X 0     0 1 1 0
0 0 X 0     0 0 1 0
1 0 0 0     X 0 0 0

Idea:

DFS, 用visited 2d vector来记录是否visited。每次recurse一下count加1.

Code:

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <unordered_map>

using namespace std;


int DFS(vector< vector<int> >& grid, int i, int j, vector< vector<bool> >& DP);

int get_biggest_region(vector< vector<int> > grid) {
    if(grid.empty()) return 0;

    int m=grid.size();
    int n=grid[0].size();

    vector<vector<bool>> DP(m, vector<bool>(n, false));

    int max_size=0;
    for(int i=0; i<m; ++i){
        for(int j=0; j<n; ++j){
            if(!DP[i][j] && grid[i][j]==1){
                max_size = max(max_size, DFS(grid, i, j, DP));
            }
        }
    }

    return max_size;
}

int DFS(vector< vector<int> >& grid, int i, int j, vector< vector<bool> >& DP){
    DP[i][j]=true;
    int m=grid.size();
    int n=grid[0].size();

    vector<pair<int, int>> move={{-1,0}, {-1,1}, {0,1}, {1,1}, {1,0}, {1,-1}, {0,-1}, {-1,-1}};

    int count=0;

    for(auto e: move){
        int k=i+e.first;
        int l=j+e.second;

        if(k>=0 && k<m && l>=0 && l<n && !DP[k][l] && grid[k][l]==1){
            count += DFS(grid, k, l, DP);
        }
    }
    return count+1;
}


int main(){
    int n;
    cin >> n;
    int m;
    cin >> m;
    vector< vector<int> > grid(n,vector<int>(m));
    for(int grid_i = 0;grid_i < n;grid_i++){
       for(int grid_j = 0;grid_j < m;grid_j++){
          cin >> grid[grid_i][grid_j];
       }
    }
    cout << get_biggest_region(grid) << endl;
    return 0;
}

results matching ""

    No results matching ""