1424:
Qz’s Maximum All One Square
1424:
Qz’s Maximum All One Square
Submit: 145 Solved: 43
[ Submit][ Status][ Web Board]
Description
YH gave Qz an odd matrix consists of one or zero. He asked Qz to find a square that has the largest area. The problem is not so easy, that means the square you find must not contain any zero. Your task is finding the square and you only need to output the area of the matrix. If you help Qz, he will thanks you a lot and treat you a meal. Please call him after the contest if you solve this problem. His number is XXXXXXXXXXXX. Hehe~
Input
For each case, Qz first give you 2 number n and m (1 <= n <= 1000, 1 <= m <= 1000),the matrix is in size of n columns and m rows.
Then Qz will give you the matrix. To avoid cost his money, Qz decided to make this problem a little difficult... Do not worry, he just gave you multiple cases.
Output
For each test case, output the desired answer.
Sample Input
2 2
1 1
1 1
4 4
1 1 0 0
1 1 1 1
1 1 1 0
1 1 0 1
Sample Output
4
4
解决方案:dp题,公式为dp[i][j]=min(dp[i][j-1],dp[i-1][j],dp[i-1][j-1])+1;
code:#include<iostream> #include<cstdio> using namespace std; int dp[1003][1003]; int Mat[1003][1003]; int main() { int n,m; while(~scanf("%d%d",&n,&m)){ int temp=n;n=m;m=temp; for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ scanf("%d",&Mat[i][j]); } } int ans=0; for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ if(Mat[i][j]){ dp[i][j]=min(dp[i-1][j-1],min(dp[i-1][j],dp[i][j-1]))+1; if(dp[i][j]>ans) ans=dp[i][j]; } else dp[i][j]=0; } } printf("%d\n",ans*ans); } return 0; }
1424: