We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Connected Cells in a Grid
Connected Cells in a Grid
Sort by
recency
|
270 Discussions
|
Please Login in order to post a comment
My Swift solution:
JavaScript solution inspired by other folks
Pretty standard DFS approach w/ O(1) memory trick (ignoring stack space). Recursively, the number of connected tiles is the sum of all of number of connected tiles in the immediately connected regions. A double for loop handles all of the neighbours, and we can set the matrix value to 0 when we've visited a cell, to ensure we don't visit it again.
c++ using bfs (breath first search) approach
define pii pair
define mk make_pair
int bfs(int r, int c, vector> &mat, vector> &vis, int n, int m) { int surf = 0; queue q; q.push(mk(r,c)); // cout << "starting " << r << " "<
}
int connectedCell(vector> matrix) { int n = matrix.size(), m = matrix[0].size(); vector> vis(n, vector(m,false));
}
C++ solution using backtracking(similar to rat in a maze)