Geeks's Garden | Practice | GeeksforGeeks

Geek has a rosarium and it has N*M slots, some of them are empty and some of them have roses. Given N*M character array consists of 0's and 1's. 0 represents an empty spot and 1 represents a rose. The task is to find the maximum number of adjacent roses in geek's rosarium. For a spot (i, j) adjacent spots are (i+1, j), (i-1, j), (i, j-1), (i, j+1), (i-1, j-1), (i-1, j+1), (i+1, j-1),  and (i+1, j+1).
Input: 
1. The first line of the input contains a single integer  T denoting the number of test cases. The description of T test cases follows.
2. The first line of each test case contains two space-separated integers N and M.
3. Next N lines consist of M characters.

Output: For each test case, print the maximum number of adjacent roses.

Constraints:
1. 1 <= T <= 100
2. 1 <= N, M <= 100
3. '0' <= arr[i][j] <= '1'

Example:
Input:
2
2 2
10
01
3 4
1001
0011
0101
Output:
2
5
0
manushresthofficial24669
This comment was deleted.
+1
drxmagg5 months ago
#include <iostream>
#include <vector>

using namespace std;

const char VISITED = '2';

int dfs(vector<string> & v, int r, int c) {
    if (r < 0 || r >= v.size() || c < 0 
       || c >= v[0].size() || v[r][c] == VISITED) {
        return 0;
    }
    
    if (v[r][c] == '0') {
        v[r][c] = VISITED;
        return 0;
    } 
    // we got unvisited one

    v[r][c] = VISITED;

    return 1 + dfs(v, r-1, c) 
           + dfs(v, r-1, c+1) 
           + dfs(v, r, c+1) 
           + dfs(v, r+1, c+1) 
           + dfs(v, r+1, c)
           + dfs(v, r+1, c-1)
           + dfs(v, r, c-1)
           + dfs(v, r-1, c-1);
}


int solve(vector<string> & v) {
    int res = 0;
    
    for(int r = 0; r < v.size(); ++r) {
        for(int c = 0; c < v[0].size(); ++c) {
            res = max(res, dfs(v, r, c));
        }
    }
    return res;
}

int main() {
	int t;
	cin >> t;
	
	for(int i = 0; i < t; ++i) {
	    int n, m;
	    cin >> n >> m;
	    
	    vector<string> v(n);
	    for(int nn = 0; nn < n; ++nn) {
	       cin >> v[nn];
	        
	    }
	    
	    cout << solve(v) << "\n";
	}
	
	return 0;
}
0
anutiger5 months ago
int helper(vector<vector<char>> &arr,vector<vector<char>>&dp,int r,int c){
    int dx[] = {1,-1,0,0,-1,-1,1,1};
    int dy[] = {0,0,-1,1,-1,1,-1,1};
    queue<pair<int,int>>q;
    q.push({r,c});
    dp[r][c] = 1;
    int res = 1;
    while(!q.empty()){
    auto it = q.front(); q.pop(); 
    int x1 = it.first;
    int y1 = it.second;
    for(int i = 0; i < 8 ; i ++){
            int x  = x1 + dx[i];
            int y = y1 + dy[i];
            if(x >= 0 and x < arr.size() and y >= 0 and y < arr[0].size() and dp[x][y] ==  0 and arr[x][y] == '1'){
                dp[x][y] = 1;
                q.push({x,y});
                res++;
            }
        }
    }
    return res;
}
int main()
 {
	//code
	int t ;
	cin >> t;
	while(t--){
	    int n,m;
	    cin >> n >> m;
	    
	    vector<vector<char>> arr(n,vector<char>(m,'0'));
	    vector<vector<char>> dp(n,vector<char>(m,0));
	    for(int i = 0; i < n ; i ++){
	        for(int j = 0 ; j < m ; j ++){
	            char c ;
	            cin >> c;arr[i][j] = c;
	        }
	    }
	    int mi = 0;
	    for(int i = 0; i < n ; i ++){
	        for(int j = 0 ; j < m ; j++){
	            if(arr[i][j] == '1' and dp[i][j] == 0){
	                int a = helper(arr,dp,i,j);
	                mi = max(mi,a);
	            }
	        }
	    }
	    cout<<mi<<endl;
	}
	return 0;
}
-1
imranwahid6 months ago
Easy C++ solution
https://ide.geeksforgeeks.org/k4Dv4TqdyC
0
salonitayal7 months ago
I cannot submit my solution. Also it is giving WA on this compiler whereas working fine on  gfg-ide and my local machine.
https://ide.geeksforgeeks.org/HqEw69Kt7Q
0
Rohit Singh7 months ago
Rohit Singh
#include <bits stdc++.h="">using namespace std;#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);char a[101][101];bool vis[101][101] = {false};int t, m, n, ans, ct;pair<int, int=""> nbr[] = {{1, 0}, { -1, 0}, {0, 1}, {0, -1}, { -1, 1}, { -1, -1}, {1, 1}, {1, -1}};bool check(int i, int j){    if ((i >= 0) && (i < n) && (j >= 0) && (j < m) && (!vis[i][j]))        return true;    return false;}void dfs(int i, int j){    vis[i][j] = true;    ct++;    for (int k = 0; k < 8; k++)    {        if (check(i + nbr[k].first, j + nbr[k].second))        {            dfs(i + nbr[k].first, j + nbr[k].second);        }    }}int main(){    fast    cin >> t;    while (t--)    {        ans  = 0;        cin >> n >> m;        for (int i = 0; i < n; i++)        {            for (int j = 0; j < m; j++)            {                cin >> a[i][j];                if (a[i][j] == '0')                    vis[i][j] = true;                else                    vis[i][j] = false;            }        }        for (int i = 0; i < n; i++)        {            for (int j = 0; j < m; j++)            {                if (!vis[i][j])                {                    ct = 0;                    dfs(i, j);                    ans = max(ans, ct);                }            }        }        cout << ans  << "\n";    }}
0
Vivek Kejriwal8 months ago
Vivek Kejriwal
def dfs(l,i,j,r,c,count):    if i<0 or i>=r or j<0 or j>= c or l[i][j]!=1:        return
    l[i][j] = 0    count[0] += 1
    dfs(l,i+1,j,r,c,count)    dfs(l,i-1,j,r,c,count)    dfs(l,i,j-1,r,c,count)    dfs(l,i,j+1,r,c,count)    dfs(l,i-1,j-1,r,c,count)    dfs(l,i-1,j+1,r,c,count)    dfs(l,i+1,j-1,r,c,count)    dfs(l,i+1,j+1,r,c,count)
def func(l,r,c):    ret = 0    for i in range(r):        for j in range(c):
            if l[i][j] == 1:                count = [0]                dfs(l,i,j,r,c,count)                ret = max(ret,count[0])    return ret
if __name__ == "__main__":    t = int(input())    for _ in range(t):        r,c = map(int,input().strip().split())        l = []        for i in range(r):            l.append(list(map(int,list(input().strip()))))
        print(func(l,r,c))
0
Faisal Khan8 months ago
Faisal Khan
can anyone plz explain the question
0
62_YASH GOSWAMI10 months ago
62_YASH GOSWAMI
Java Solution
class GFG {  static int[][] move={{1,0},{-1,0},{0,-1},{0,1},{-1,-1},{-1,1},{1,-1},{1,1}};     public static int find(int[][] arr,int[][] visited,int i,int j)    {        if(i<0 || j<0 || i>=arr.length || j>=arr[i].length || arr[i][j]==0 || visited[i][j]==1)            return 0;        visited[i][j]=1;        int count=0;        for(int[] x:move)        {            count+=find(arr,visited,i+x[0],j+x[1]);        }        return count+1;    }public static void main (String[] args) throws IOException {    BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));    int t=Integer.parseInt(bf.readLine());    while(t-->0){        String[] input=bf.readLine().trim().split(" ");        int n=Integer.parseInt(input[0]);        int m=Integer.parseInt(input[1]);               int[][] arr=new int[n][m];                for(int i=0;i<n;i++) {="" input="bf.readLine().trim().split("");" for(int="" j="0;j&lt;m;j++)" {="" arr[i][j]="Integer.parseInt(input[j]);" }="" }="" int="" max="0;" for(int="" i="0;i&lt;n;i++)" {="" for(int="" j="0;j&lt;m;j++)" {="" if(arr[i][j]="=1)" max="Math.max(max,find(arr,new" int[n][m],i,j));="" }="" }="" system.out.println(max);="" }="" }="" }<="" code="">
0
Ankit Podder10 months ago
Ankit Podder
Correct Answer.Correct AnswerExecution Time:0.01
#include <bits stdc++.h="">using namespace std;class Solution{int isOk(int x , int y , int n , int m){return (x>=0 && x<n &&="" y="">=0 && y<m); }="" int="" dfs(vector<vector<int="">> &grid ,vector<vector<int>> &visit , int i , int j,int n , int m){visit[i][j] = 1;if(grid[i][j] == 0) return 0;int count = 1;int X[] = {1 , -1 , 0 , 0 , -1 , -1 , 1 , 1};int Y[] = {0 , 0 , -1 , 1 , -1 , 1 , -1 , 1};for(int k = 0 ; k < 8 ; k++){int x = i + X[k];int y = j + Y[k];if(isOk(x , y , n , m) && visit[x][y]==0){count += dfs(grid , visit , x , y , n , m);}}return count;}public:int sol(vector<vector<int>> &grid , int n , int m){vector<vector<int>> visit(n , vector<int>(m , 0));int ans = 0;for(int i = 0 ; i < n ; i++){for(int j = 0 ; j < m ; j++){if(visit[i][j] == 0){int v = dfs(grid , visit , i , j , n , m);ans = max(ans , v);}}}return ans;}};int main() {int t;cin >> t;while(t--){int n , m;cin >> n >> m;vector<vector<int>> grid;for(int i = 0 ; i < n ; i++){vector<int> temp;string s;getline(cin >> ws , s);for(auto it : s){if(it=='1' || it=='0'){temp.push_back(it - '0');}}grid.push_back(temp);}int ans = 0;Solution s;ans = s.sol(grid , n , m);cout << ans << "\n";}return 0;}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.