Algorithm
problem Link : https://onlinejudge.org/index.php?option=onlinejudge&Itemid=8&page=show_problem&problem=1015
The poor man went to the King and said, “Lord, I cannot maintain my family. Please give me some wealth so that I can survive with my wife and children.” The King replied, “I shall grant you a piece of land so that you can cultivate and grow food for your family. In the southern part of the Kingdom there is a rectangular forest. Trees have been planted there at regular intervals. Some of the trees have been cut for use. You are allowed to take any rectangular piece of land that does not contain any tree. You need not go to the forest to select the piece of land. I have a map containing 1’s at places where there is a tree and 0s at points where the tree has been cut.” Help the poor man to find out the largest piece of land. Area of the land is measured in units of number of trees that were there. Your program should take a matrix of 1’s and 0’s as input and output the area of the largest rectangular piece of land that contain no tree. Be careful about the efficiency of your program. Input The input file may contain multiple test cases. The first line of each test case contains two integers M and N (1 ≤ M, N ≤ 100) giving the number of rows and columns in the matrix that follows. Each of the next M lines contains N symbols (either ‘0’ or ‘1’). Two consecutive symbols in a line will be separated by a single space. The input terminates with two zeros for M and N. Output For each test case in the input print a line giving the area (in terms of the number of trees were there) of the largest rectangular piece of land containing no tree.
Sample Input 6 7 0 1 1 0 1 1 0 0 0 0 0 0 1 0 1 0 0 0 0 0 1 0 1 0 0 0 0 1 1 1 0 0 0 1 0 1 1 0 1 1 0 0 0 0
Sample Output 12
Code Examples
#1 Code Example with C Programming
Code -
C Programming
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n,m;
while(scanf("%d %d",&n,&m),n||m){
vector<<int>> dp(n+1,vector<int>(m+1));
for(int i=1;i < =n;i++)
for(int j=1;j < =m;j++){
scanf("%d",&dp[i][j]);
dp[i][j] += (dp[i-1][j]+dp[i][j-1]-dp[i-1][j-1]);
}
int best = 0;
for(int i=1;i < =n;i++) for(int j=1;j < =m;j++)
for(int k=i;k < =n;k++) for(int l=j;l < =m;l++){
int cur = dp[k][l] - dp[i-1][l] - dp[k][j-1] + dp[i-1][j-1];
if(cur == 0)
best = max(best,(k-i+1)*(l-j+1));
// we can do additional pruning if not 0
}
printf("%d\n",best);
}
}
Copy The Code &
Try With Live Editor
Input
0 1 1 0 1 1 0
0 0 0 0 0 1 0
1 0 0 0 0 0 1
0 1 0 0 0 0 1
1 1 0 0 0 1 0
1 1 0 1 1 0 0
0 0
Output
Demonstration
UVA Online Judge solution -10074 - Take the Land - UVA Online Judge solution in C,C++,Java