ry's birthday today and he has ordered his favourite square cake consisting of '0's and '1's . But Gary wants the biggest piece of '1's and no '0's . A piece of cake is defined as a part which consist of only '1's, and all '1's share an edge with each other on the cake. Given the size of cake N and the cake, can you find the count of '1's in the biggest piece of '1's for Gary ? Input Format : The first line of input contains an integer, that denotes the value of N. Each of the following N lines contain N space separated integers. Output Format : Print the count of '1's in the biggest piece of '1's, according to the description in the task. Constraints : 1 <= N <= 1000 Time Limit: 1 sec Sample Input 1: 2 1 1 0 1
It's Gary's birthday today and he has ordered his favourite square cake consisting of '0's and '1's . But Gary wants the biggest piece of '1's and no '0's . A piece of cake is defined as a part which consist of only '1's, and all '1's share an edge with each other on the cake. Given the size of cake N and the cake, can you find the count of '1's in the biggest piece of '1's for Gary ?
Input Format :
The first line of input contains an integer, that denotes the value of N.
Each of the following N lines contain N space separated integers.
Output Format :
Print the count of '1's in the biggest piece of '1's, according to the description in the task.
Constraints :
1 <= N <= 1000
Time Limit: 1 sec
Sample Input 1:
2
1 1
0 1
Sample Output 1:
3
Solution: /////////////////
public class Solution {
static int[][] dir = { { 1, 0 }, { -1, 0 }, { 0, 1 }, { 0, -1 } };
public static int dfs(String[] edge, int n) {
boolean[][] visited = new boolean[n][n];
int max = 0;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(!visited[i][j] && edge[i].charAt(j)=='1' ){
int ans = max1s( edge,visited,i,j,n);
if(max <ans){
max = ans;
}
}
}
}
return max;
}
public static int max1s(String[] edge,boolean[][] visited,int x,int y,int n){.
Please complete the code.
Step by step
Solved in 3 steps with 1 images