Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.FileReader;
- public class RunMe {
- public static int prestosInLargest = 0;
- public static void debugArrayPrint(int[][] input) {
- for (int i = 0; i < input.length; i++) {
- for (int j = 0; j < input[i].length; j++) {
- System.out.print(input[i][j] + " ");
- }
- System.out.print("\n");
- }
- }
- public static void checkSubSection(int[][] input, int i, int j) {
- //System.out.println(i + " " + j);
- int iItor = i;
- int jItor = j;
- int width = 0;
- int height = 0;
- int minHeight = Integer.MAX_VALUE;
- while ((jItor < input[iItor].length) && (input[iItor][jItor] == 1)) {
- while ((iItor < input.length) && (input[iItor][jItor] == 1) && (height < minHeight)) {
- iItor++;
- height++;
- }
- jItor++;
- width++;
- minHeight = height;
- iItor = i;
- height = 0;
- //System.out.println("min wid " + minHeight + " " + width);
- if ((minHeight * width) > prestosInLargest) {
- prestosInLargest = (minHeight * width);
- System.out.println("Found new largest! Size: " + prestosInLargest + " i: " + (i + 1) + " j: " + (j + 1) + " height: " + minHeight + " width: " + width);
- }
- }
- }
- public static void main(String[] args) {
- try {
- String line;
- int totalLines = 0;
- int width = 0;
- int iterator = 0;
- FileReader fileReader = new FileReader("city_grid.txt");
- BufferedReader bufferedReader = new BufferedReader(fileReader);
- // find the total number of objects per line
- line = bufferedReader.readLine();
- totalLines++;
- width = line.split(" ").length;
- // find the number of lines
- while ((line = bufferedReader.readLine()) != null) { totalLines++; }
- int inputArray[][] = new int[totalLines][width];
- fileReader.close();
- fileReader = new FileReader("city_grid.txt");
- bufferedReader = new BufferedReader(fileReader);
- while ((line = bufferedReader.readLine()) != null) {
- String[] tokens = line.split(" ");
- for (int i = 0; i < tokens.length; i++) {
- if (tokens[i].equals("O")) {
- inputArray[iterator][i] = 0;
- }
- else if (tokens[i].equals("P")) {
- inputArray[iterator][i] = 1;
- }
- else { System.err.println("UH OH! '" + tokens[i] + "'"); }
- }
- iterator++;
- }
- //debugArrayPrint(inputArray);
- for (int i = 0; i < inputArray.length; i++) {
- for (int j = 0; j < inputArray[i].length; j++) {
- checkSubSection(inputArray, i, j);
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
Add Comment
Please, Sign In to add comment