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;
    
    	}
    
    }
    
    
    
    
  • 相关阅读:
    学习方法与经验总结
    工具综合症?资料收集狂?
    SpringMVC 过滤器Filter使用解析
    Spring 管理Filter和Servlet
    pom.xml配置详解
    开发Java web应用程序的介绍
    java web构建学习(概念基础)
    题目1474:矩阵幂
    题目1473:二进制数
    Python strip()方法
  • 原文地址:https://www.cnblogs.com/a1439775520/p/13079135.html
Copyright © 2011-2022 走看看