Aizu - DPL_3_A Largest Square 【基础DP 自己还是太菜】
Given a matrix (H × W) which contains only 1 and 0, find the area of the largest square matrix which only contains 0s.
Input
H W c
1,1
c
1,2
... c1,W c
2,1
c
2,2
... c2,W : cH,1 cH,2 ... cH,W
In the first line, two integers H and W separated by a space character are given. In the following H lines, ci,j, elements of the H × W matrix, are given.
Output
Print the area (the number of 0s) of the largest square.
Constraints
- 1 ≤ H, W ≤ 1,400
Sample Input
4 5 0 0 1 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 1 0
Sample Output
4
1表示有污渍
0表示干净
问最大的干净的正方形边长
f[i][j] = min(f[i-1][j],min(f[i][j-1],f[i-1][j-1]))+1;
完全没有考虑到状态之间还有这种关系,自己的思考能力才是最应该提升的。
题解
1 #include<iostream> 2 using namespace std; 3 #include<cstring> 4 #include<cstdio> 5 int f[1500][1500]; 6 int a[1500][1500]; 7 int main(){ 8 int n,m; 9 scanf("%d%d",&n,&m); 10 int maxn = 0; 11 for(int i=1;i<=n;i++){ 12 for(int j=1;j<=m;j++){ 13 scanf("%d",&a[i][j]); 14 if(a[i][j]==1) 15 f[i][j]=0; 16 else 17 f[i][j] = min(f[i-1][j],min(f[i][j-1],f[i-1][j-1]))+1; 18 maxn = max(f[i][j],maxn); 19 } 20 } 21 printf("%d\n",maxn*maxn); 22 return 0; 23 }