Skip to content

Instantly share code, notes, and snippets.

@r2dev
Created February 17, 2016 01:40
Show Gist options
  • Save r2dev/7530d3e1c57e9ee4cde5 to your computer and use it in GitHub Desktop.
Save r2dev/7530d3e1c57e9ee4cde5 to your computer and use it in GitHub Desktop.
import java.util.Scanner;
class Matrix {
private static int row;
private static int[][] matrix;
private static int[][] temp;
public static void main(String[] args) {
System.out.println("Enter the number of rows in the square matrix: ");
Scanner input = new Scanner(System.in);
row = input.nextInt();
matrix = new int[row][row];
temp = new int[row][row];
System.out.println("Enter the matrix row by row: ");
for (int i = 0; i != row; i++) {
for (int j = 0; j != row; j++) {
matrix[i][j] = input.nextInt();
}
}
System.out.println("input:");
for (int i = 0; i != row; i++) {
for (int j = 0; j != row ; j++) {
System.out.print(matrix[i][j]);
}
System.out.println("");
}
System.out.println("output:");
for (int i = row-1; i >= 0; i--) {
for (int j = row-1; j >= 0; j--) {
temp[i][j] = calc (i, j);
}
}
for (int i = 0; i != row; i++) {
for (int j = 0; j != row ; j++) {
System.out.print(temp[i][j]);
}
System.out.println("");
}
printResult();
}
private static int calc(int x, int y) {
if (matrix[x][y] == 0) {
return 0;
} else {
if ((x == row - 1) || (y == row - 1)) {
return 1;
} else {
if ((matrix[x+1][y] == 0) || (matrix[x][y+1] == 0) || (matrix[x+1][y+1] == 0)) {
return 1;
} else if ((temp[x+1][y] != temp[x+1][y+1]) || (temp[x][y+1] != temp[x+1][y+1])) {
return 1;
} else {
return calc(x+1, y+1) + 1;
}
}
}
}
private static void printResult() {
int x = 0;
int y = 0;
int min = 0;
for (int i = 0; i != row; i++) {
for (int j = 0; j != row; j++) {
if (temp[i][j] >= min) {
x = j;
y = i;
min = temp[i][j];
}
}
}
System.out.println("the largest block is at(" + x + ", " + y + ") with " + min +" rows.");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment