二维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;
}

相关文章:

  • 2021-09-25
  • 2021-09-11
  • 2022-01-26
  • 2022-12-23
  • 2021-11-30
  • 2022-01-09
  • 2022-12-23
猜你喜欢
  • 2021-10-31
  • 2021-09-25
  • 2021-06-08
  • 2022-12-23
相关资源
相似解决方案