Qz’s Maximum All One Square+csuoj+dp

来源:互联网 发布:阿里云备案订单在哪里 编辑:程序博客网 时间:2024/05/22 04:49

1424: 

Qz’s Maximum All One Square

Time Limit: 5 Sec  Memory Limit: 128 MB
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 21 11 14 41 1 0 01 1 1 11 1 1 01 1 0 1

Sample Output

44
解决方案: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: 
0 0
原创粉丝点击