p1387 最大正方形

动态规划水题:

状态转移方程为f[i][j]=min(min(f[i-1][j],f[i][j-1]),f[i-1][j-1])),很显然,因为如果左边上边和左上都不为0,则上一个状态就可以转移过来。而f的意义是以i,j为右下顶点的能形成的正方形最大边长。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
#define N 6001
using namespace std;
typedef long long ll ;
int dp[N][N];
int ()
{
int m, n;
cin >> m >> n;
int ans = 0;
for (int i = 1; i <= m;i++)
{
for (int j = 1; j <= n;j++)
{
int a;
cin >> a;
if(!a)
continue;
dp[i][j]=min(min(dp[i-1][j],dp[i][j-1]),dp[i-1][j-1])+1;

ans = max(ans, dp[i][j]);
}
}
cout << ans << endl;
}