Qz’s Maximum All One Square+csuoj+dp

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 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: 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值