具有最高边界和的二维矩阵平方子矩阵

时间:2018-03-08 09:41:00

标签: java

考虑从0到9的数字2D矩阵,其宽度和高度可变。找到具有最高边界元素总和的方形子矩阵。

输入:

矩阵的输入宽度和高度:6 8

输入数字从0到9的矩阵:

2 0 6 1 2 5 
1 0 5 0 1 3 
3 0 1 2 4 1 
0 1 3 1 1 9 
4 1 0 8 5 2 
0 1 0 1 2 3
6 5 3 1 0 2
0 0 1 6 0 4 

输入方形子矩阵的最大宽度(对于方形子矩阵的高度和宽度相同):3

输出:

由于突出显示的子矩阵的总和最大(边界元素的计算总和仅为2,4,1,9,2,5,8,1),

 2 4 1  
 1 1 9  
 8 5 2

输出应为:

 func updateProcessStatus(isCompleted : Bool){
    if isCompleted{
        self.labelStatus.text = "Process is completed"
    }else{
        self.labelStatus.text = "Process is in progress"
    }
}

5 个答案:

答案 0 :(得分:1)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class MatrixMaxSum {
    public static void main(String[] args) {
        MatrixMaxSum maxSum = new MatrixMaxSum();
        Matrix matrix = maxSum.readInput();

        List<SubMatrixSum> subMatrices = maxSum.findAllSubMatrices(matrix);
        System.out.println("Total SubMatrices: " + subMatrices.size());
        SubMatrixSum subMatrixSum = subMatrices.stream().sorted((s1, s2) -> s2.sum.compareTo(s1.sum)).findFirst().get();

        System.out.println(subMatrixSum);
    }

    private List<SubMatrixSum> findAllSubMatrices(Matrix m) {
        List<SubMatrixSum> subs = new ArrayList<>();
        int row = 0, column;

        while (row < m.matrix.length - m.subMatrixSize + 1) {
            column = 0;
            while (column < m.matrix[0].length - m.subMatrixSize + 1) {
                int[][] sub = new int[m.subMatrixSize][m.subMatrixSize];
                int sum = 0;
                if (row == 0 || row == m.matrix.length || column == 0 || column == m.matrix[0].length) {
                    for (int i = 0, mi = row; i < m.subMatrixSize; i++, mi++) {
                        for (int j = 0, mj = column; j < m.subMatrixSize; j++, mj++) {
                            sub[i][j] = m.matrix[mi][mj];
                            sum += sub[i][j];
                        }
                    }
                    subs.add(new SubMatrixSum(sub, sum));
                }
                column++;
            }
            row++;
        }

        return subs;
    }

    private Matrix readInput() {
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            System.out.print("Input width and height of matrix:");
            String line = br.readLine();
            String[] wh = line.split("\\s+");
            if (wh.length == 2) {
                int w = Integer.parseInt(wh[0]);
                int h = Integer.parseInt(wh[1]);
                int[][] m = new int[h][w];
                System.out.println("Input matrix with numbers from 0-9 :");
                for (int i = 0; i < h; i++) {
                    line = br.readLine();
                    String[] row = line.split("\\s+");
                    if (row.length == w) {
                        for (int j = 0; j < w; j++) {
                            m[i][j] = Integer.parseInt(row[j]);
                        }
                    } else {
                        throw new RuntimeException("Invalid input");
                    }
                }
                System.out.println("Input maximum width of square submatrix:");
                line = br.readLine();
                int sm = Integer.parseInt(line);
                if (sm > w || sm > h) {
                    throw new RuntimeException("Invalid input");
                }

                return new Matrix(m, sm);
            } else {
                throw new RuntimeException("Invalid input");
            }
        } catch (IOException e) {

        }
        return null;
    }
}

class SubMatrixSum {
    int[][] subMatrix;
    Integer sum;

    public SubMatrixSum(int[][] subMatrix, Integer sum) {
        super();
        this.subMatrix = subMatrix;
        this.sum = sum;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("Matrix: \n");
        for (int i = 0; i < subMatrix.length; i++) {
            for (int j = 0; j < subMatrix[i].length; j++) {
                sb.append(subMatrix[i][j] + " ");
            }
            sb.append("\n");
        }
        sb.append("Sum: " + sum);
        return sb.toString();
    }
}

class Matrix {
    int[][] matrix;
    int subMatrixSize;

    public Matrix(int[][] matrix, int subMatrixSize) {
        super();
        this.matrix = matrix;
        this.subMatrixSize = subMatrixSize;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[i].length; j++) {
                sb.append(matrix[i][j] + " ");
            }
            sb.append("\n");
        }
        return sb.toString();
    }
}

答案 1 :(得分:0)

$this->db->select('*');
$this->db->from('otslm');
$this->db->from('pic');
$this->db->where('tid', $postid);
$this->db->where('nama', $postpic);

答案 2 :(得分:0)

import java.lang.*;

导入java.util。*;

答案 3 :(得分:0)

我在这里放置了一个可在各种数据集上动态工作的代码。

import java.io.*;
import java.util.*;
public class Main
{
    public static int[][] getSubmatrix(int matrix[][],int hight, int width,int n)
    {
        int result[][] = new int[n][n];
        int max=0;
        int h=0,w=0;
        while(h<hight-n)
        {
            int temp[][] = new int[n][n];
            for(int i=0; i<n;i++)
            {
                for(int j=0; j<n; j++)
                {
                    temp[i][j]=matrix[h+i][w+j];
                }
            }
            int tempSum = getSum(temp,n);
            if(tempSum>=max)
            {
                max = tempSum;
                result = temp;
            }
            if(h<hight-n)
            {
                if(w<width-n)
                {
                    w++;
                }
                else
                {
                   w=0;
                   h++;
                }
            }
        }
        return result;
    }
    public static int getSum(int mat[][],int n)
    {
        int sum=0;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                sum += mat[i][j];
            }
        }
        return sum;
    }
    public static void main(String[] args) 
    {
        Scanner scan = new Scanner(System.in);
        // this programme works on stdin stout
        // if you want to run on manual input then remove comments for Sysout() fun
        // System.out.println("Input hight and width of matrix: ");
        int width = scan.nextInt();
        int hight = scan.nextInt();
        int matrix[][] = new int[hight][width];
        // this is for inputting value of matrix
        // System.out.println("input matrix with number 0 to 9: ");
        for(int i=0;i<hight;i++)
            for(int j=0;j<width;j++)
                matrix[i][j]=scan.nextInt();
        // this is for required length of submatrix
        // System.out.println("Input maximum width of square matrix");
        int n = scan.nextInt();
        //calling function submatrix
        int resultMatrix[][] = new int[n][n];
        resultMatrix = getSubmatrix(matrix,hight,width,n);
        for(int i=0; i<n; i++)
        {
            for(int j=0;j<n; j++)
            {
                 System.out.print(resultMatrix[i][j]+" ");
            }
            System.out.println();
        }
    }
}

答案 4 :(得分:-1)

package demo;

import java.util.Scanner;

public class MaximumSumSubMatrix {

static void subMatrix(int[][] arr){
    int row = arr.length-2;
    int col = arr[0].length-2;
    int sum,maxSum=0,subRow=0,subCol=0;
    for(int i=0;i<row;i++) {
        sum = 0;
        for(int j=0;j<col;j++) {
            sum = arr[i][j] + arr[i][j+1] + arr[i][j+2]
                    + arr[i+1][j] + arr[i+1][j+1] + arr[i+1][j+2]
                            +arr[i+2][j] + arr[i+2][j+1] + arr[i+2][j+2];
            if(maxSum < sum) {
                maxSum = sum;
                subRow = i;
                subCol = j;
            }
        }
    }

    for(int i=subRow;i<subRow+3;i++) {
        for(int j=subCol;j<subCol+3;j++) {
            System.out.print(arr[i][j]+" ");
        }
        System.out.println();
    }

}
public static void main(String[] args) {
    System.out.print("Input width and height of matrix:");
    Scanner sc = new Scanner(System.in);
    int row = sc.nextInt();
    int col = sc.nextInt();
    int[][] inputMat = new int[row][col];

    System.out.println("Input Matrix with numbers from 0 to 9");
    for(int i=0;i<row;i++) {
        for(int j=0;j<col;j++) {
            inputMat[i][j] = sc.nextInt();
        }
    }
    subMatrix(inputMat);//this method will print required sumatrix
 }
}