第八章第三十五题(最大块)(The biggest piece)

第八章第三十五题(最大块)(The biggest piece)

  • **8.35(最大块)给定一个元素为0或1的方阵,编写一个程序,找到一个元素都为1的最大子方阵。程序提示用户输入矩阵的行数。然后显示最大的子方阵的第一个元素,以及该子方阵中的行数。下面是一个运行示例。
    Enter the number of rows in the matrix:5
    Enter the matrix row by row:
    1 0 1 0 1
    1 1 1 0 1
    1 0 1 1 1
    1 0 1 1 1
    1 0 1 1 1
    The maximum square submatrix is at (2,2) with size 3
    程序需要实现和使用下面的方法来找到最大的子方阵:
    public static boolean allOne(int pinr,int pinc, int range,int[][] m)
    返回值是一个包含三个值的数组。前面两个是子方阵中的行和列的下标, 第3个值是子方阵中的行数。
    **8.35(The biggest piece)Given an element of 0 or 1 square matrix, write a program to find the largest sub matrix elements are 1. The number of user input lines of the program. The first element of the largest subarray is then displayed, along with the number of rows in that subarray. Here is a running example.
    Enter the number of rows in the matrix:5
    Enter the matrix row by row:
    1 0 1 0 1
    1 1 1 0 1
    1 0 1 1 1
    1 0 1 1 1
    1 0 1 1 1
    The maximum square submatrix is at (2,2) with size 3
    The program needs to implement and use the following methods to find the largest subarray:
    public static boolean allOne(int pinr,int pinc, int range,int[][] m)
    The return value is an array of three values. The first two are the subscripts of the rows and columns in the subarray, and the third value is the number of rows in the subarray.

  • 参考代码:

    package chapter08;
    
    import java.util.Scanner;
    
    public class Code_35 {
            public static void main(String[] args)
                throws Exception
            {
                Scanner input = new Scanner(System.in);
                System.out.print("Enter the number of rows in the matrix:");
                int ass = input.nextInt();
                int[][] test = new int[ass][ass];
                System.out.println("Enter the matrix row by row:");
                for(int i=0;i<ass;i++)
                    for(int j=0;j<ass;j++)
                        test[i][j]=input.nextInt();
                int[] jesus = findLargestBlock(test);
                System.out.printf("The maximum square submatrix is at (%d,%d) with size %d\n",jesus[0],jesus[1],jesus[2]);
            }
            public static int[] findLargestBlock(int[][] m){
                int[] res = new int[3];
                int n = m.length;
                for(int i=0;i<n;i++)
                    for(int j=0;j<n;j++){
                        for(int r=1;r<=Math.min(n-i,n-j);r++){
                            if(allOne(i,j,r,m)){
                                if(r>res[2]){
                                    res[0]=i;
                                    res[1]=j;
                                    res[2]=r;
                                }
                            }
                        }
                    }
                return res;
            }
            public static boolean allOne(int pinr,int pinc, int range,int[][] m){
                boolean all1 = true;
                for(int i=0;i<range;i++)
                    for(int j=0;j<range;j++){
                        if(m[pinr+i][pinc+j]==0)
                        {
                            all1 = false;
                            break;
                        }
                    }
                return all1;
            }
    }
    
    
  • 结果显示:

    Enter the number of rows in the matrix:5
    Enter the matrix row by row:
    1 0 1 0 1
    1 1 1 0 1
    1 0 1 1 1
    1 0 1 1 1
    1 0 1 1 1
    The maximum square submatrix is at (2,2) with size 3
    
    Process finished with exit code 0
    
    
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值