AcWing 1612. 最大正方形Java代码
原题链接
中等
作者:
JavaBear
,
2020-09-16 20:34:25
,
所有人可见
,
阅读 467
Java版本
import java.util.*;
import java.io.*;
public class Main{
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] strs1 = br.readLine().split(" ");
int rows = Integer.parseInt(strs1[0]);
int cols = Integer.parseInt(strs1[1]);
int[][] dp = new int[rows][cols];
int[][] matrix = new int[rows][cols];
for(int i = 0; i < rows; i++){
String[] strs = br.readLine().split(" ");
for(int j = 0; j < cols; j++){
matrix[i][j] = Integer.parseInt(strs[j]);
}
}
int maxSquareLength = 0;
for(int i = 0; i < rows; i++){
if(matrix[i][0] == 1) dp[i][0] = 1;
}
for(int i = 0; i < cols; i++){
if(matrix[0][i] == 1) dp[0][i] = 1;
}
for(int i =1; i < rows; i++){
for(int j = 1; j < cols; j++){
if(matrix[i][j] == 1){
dp[i][j] = Math.min(Math.min(dp[i-1][j-1],dp[i-1][j]),dp[i][j-1])+1;
if(dp[i][j] > maxSquareLength) maxSquareLength = dp[i][j];
}
}
}
System.out.println( maxSquareLength*maxSquareLength);
}
}