洛谷P1387 最大正方形

解法

二维dp还是要找一种递推的关系

这里用正方形的右下角来一起递推,也就是说如果当前值是1的话,那么就去寻找左边上面和左上的最小dp值+1,最后输出最大的dp值即可

代码

#include <bits/stdc++.h>
using namespace std;
int num[666][666],dp[666][666];
int main()
{
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n,m,maxn=-1;;
  cin>>n>>m;
  for(int i=1;i<=n;i++)
  for(int j=1;j<=m;j++)
  cin>>num[i][j];
  for(int i=1;i<=n;i++)
  for(int j=1;j<=m;j++)
  if(num[i][j]==1)
  {
    dp[i][j]=min(min(dp[i-1][j],dp[i][j-1]),dp[i-1][j-1])+1;
    maxn=max(maxn,dp[i][j]);
  }
  cout<<maxn;
}
posted @ 2019-01-20 13:36  baccano!  阅读(143)  评论(0编辑  收藏  举报