-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdfshackerrank.cpp
More file actions
53 lines (50 loc) · 1.01 KB
/
dfshackerrank.cpp
File metadata and controls
53 lines (50 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <bits/stdc++.h>
using namespace std;
int row,col,**matrix;
int countChells(int i,int j)
{
if(i<0||j<0||i>=row||j>=col||matrix[i][j]==0)
return 0;
int count=matrix[i][j]--;
count+=countChells(i,j-1);
count+=countChells(i-1,j);
count+=countChells(i,j+1);
count+=countChells(i-1,j);
count+=countChells(i+1,j);
count+=countChells(i,j+1);
count+=countChells(i+1,j+1);
count+=countChells(i-1,j-1);
return count;
}
int maxGrid()
{
int max=-1,q;
cout<<"in max grid\n";
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
if(matrix[i][j]==1)
{
cout<<"countShells\n";
q=countChells(i,j);
}
if(q>max)
max=q;
}
}
return max;
}
int main()
{
cin>>row>>col;
matrix = new int*[row];
for(int i=0;i<row;i++)
matrix[i]=new int[col];
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
cin>>matrix[i][j];
}
cout<<maxGrid();
}