题目描述
在一个n*m的只包含0和1的矩阵里找出一个不包含0的最大正方形,输出边长。
输入输出格式
输入格式:
输入文件第一行为两个整数n,m(1<=n,m<=100),接下来n行,每行m个数字,用空格隔开,0或1.
输出格式:
一个整数,最大正方形的边长
输入输出样例
输入样例#1:
4 4
0 1 1 1
1 1 1 0
0 1 1 0
1 1 0 1
输出样例#1:
2
import java.util.Scanner;
public class zuidazhengfangxing {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int m = sc.nextInt();
int n = sc.nextInt();
int[][] arr = new int[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
arr[i][j] = sc.nextInt();
}
}
int borderSideLength = 1;
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
if (arr[i][j] == 1) {
//其他三个正方形的点
int temp = min(arr[i][j - 1], arr[i - 1][j],arr[i - 1][j - 1]);
if (temp != 0) {//!0就可以赋值
arr[i][j] = temp + 1;
if (arr[i][j] > borderSideLength)
borderSideLength = arr[i][j];
}
}
}
}
System.out.println(borderSideLength);
}
static int min(int a, int b, int c) {
a = a < b ? a : b;
a = a < c ? a : c;
return a;
}
}