-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBucees.java
More file actions
44 lines (36 loc) · 1.32 KB
/
Bucees.java
File metadata and controls
44 lines (36 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class Bucees {
public static void main(String[] args) throws IOException {
File inputFile = new File("land.dat");
Scanner file = new Scanner(inputFile);
// get the size of the table
int rows = file.nextInt();
int cols = file.nextInt();
// read in the table
int[][] plots = new int[rows][cols];
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
plots[i][j] = file.nextInt();
// find the max side and display the results
int maxSide = findMaxSide(plots);
System.out.println("The maximum square area takes up " + maxSide * maxSide + " plots.");
file.close(); // close the file
}
public static int findMaxSide(int[][] plots) {
int rows = plots.length;
int cols = plots[0].length;
int max = 0;
for (int i = 1; i < rows; i++)
for (int j = 1; j < cols; j++) {
if (plots[i][j] == 1) {
plots[i][j] = Math.min(Math.min(plots[i - 1][j], plots[i - 1][j - 1]), plots[i][j - 1]) + 1;
}
if (plots[i][j] > max) {
max = plots[i][j];
}
} // end for
return max;
}
}