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
|
259 Discussions
|
Please Login in order to post a comment
C++ (more at https://github.com/IhorVodko/Hackerrank_solutions , feel free to give a star :) ) . No recursion, no container needed for alredy seen cells.
include
using namespace std; int solve(int i,int j,int n,int m,vector>&a) { if(i<0 || j<0 || i>=n || j>=m || a[i][j]==0) { return 0; } a[i][j]=0; int r,t,y,f,h,c,v,b; r=solve(i-1,j-1,n,m,a); t=solve(i-1,j,n,m,a); y=solve(i-1,j+1,n,m,a); f=solve(i,j-1,n,m,a); h=solve(i,j+1,n,m,a); c=solve(i+1,j-1,n,m,a); v=solve(i+1,j,n,m,a); b=solve(i+1,j+1,n,m,a); return 1+r+t+y+f+h+c+v+b; } int main() { int n,m; cin>>n>>m; vector>a; for(int i=0;itemp; for(int j=0;j>op; temp.push_back(op); } a.push_back(temp); } int ans=0; for(int i=0;i