zoukankan      html  css  js  c++  java
  • (Java实现) 洛谷 P1387 最大正方形

    题目描述
    在一个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;
    
    	}
    
    }
    
    
    
    
  • 相关阅读:
    day40 JavaScript初识
    day39 CSS层叠样式表-01
    day38 HTML基础
    day35 数据操作补充和单表操作
    day33 数据库初识
    day27 线程同步
    day25 多进程
    day24 内置方法,异常机制
    ROS 进阶学习笔记(12)
    ROS进阶学习笔记(11)- Turtlebot Navigation and SLAM
  • 原文地址:https://www.cnblogs.com/a1439775520/p/12948886.html
Copyright © 2011-2022 走看看