Guest User

Untitled

a guest
Jun 18th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.56 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.FileReader;
  3.  
  4. public class RunMe {
  5.  
  6.     public static int prestosInLargest = 0;
  7.    
  8.     public static void debugArrayPrint(int[][] input) {
  9.        
  10.         for (int i = 0; i < input.length; i++) {
  11.            
  12.             for (int j = 0; j < input[i].length; j++) {
  13.                
  14.                 System.out.print(input[i][j] + " ");
  15.             }
  16.             System.out.print("\n");
  17.         }
  18.     }
  19.     public static void checkSubSection(int[][] input, int i, int j) {
  20.        
  21.         //System.out.println(i + " " + j);
  22.         int iItor = i;
  23.         int jItor = j;
  24.         int width = 0;
  25.         int height = 0;
  26.         int minHeight = Integer.MAX_VALUE;
  27.         while ((jItor < input[iItor].length) && (input[iItor][jItor] == 1)) {
  28.        
  29.             while ((iItor < input.length) && (input[iItor][jItor] == 1) && (height < minHeight)) {
  30.            
  31.                 iItor++;
  32.                 height++;
  33.             }
  34.             jItor++;
  35.             width++;
  36.             minHeight = height;
  37.             iItor = i;
  38.             height = 0;
  39.             //System.out.println("min wid " + minHeight + " " + width);
  40.             if ((minHeight * width) > prestosInLargest) {
  41.                
  42.                 prestosInLargest = (minHeight * width);
  43.                 System.out.println("Found new largest! Size: " + prestosInLargest + " i: " + (i + 1) + " j: " + (j + 1) + " height: " + minHeight + " width: " + width);
  44.             }
  45.         }
  46.     }
  47.     public static void main(String[] args) {
  48.        
  49.         try {
  50.        
  51.             String line;
  52.             int totalLines = 0;
  53.             int width = 0;
  54.             int iterator = 0;
  55.             FileReader fileReader = new FileReader("city_grid.txt");
  56.             BufferedReader bufferedReader = new BufferedReader(fileReader);
  57.             // find the total number of objects per line
  58.             line = bufferedReader.readLine();
  59.             totalLines++;
  60.             width = line.split("   ").length;
  61.             // find the number of lines
  62.             while ((line = bufferedReader.readLine()) != null) { totalLines++; }
  63.             int inputArray[][] = new int[totalLines][width];
  64.             fileReader.close();
  65.             fileReader = new FileReader("city_grid.txt");
  66.             bufferedReader = new BufferedReader(fileReader);
  67.             while ((line = bufferedReader.readLine()) != null) {
  68.                
  69.                 String[] tokens = line.split("   ");
  70.                 for (int i = 0; i < tokens.length; i++) {
  71.                    
  72.                     if (tokens[i].equals("O")) {
  73.                    
  74.                         inputArray[iterator][i] = 0;
  75.                     }
  76.                     else if (tokens[i].equals("P")) {
  77.  
  78.                         inputArray[iterator][i] = 1;
  79.                     }
  80.                     else { System.err.println("UH OH! '" + tokens[i] + "'"); }
  81.                 }
  82.                 iterator++;
  83.             }
  84.             //debugArrayPrint(inputArray);
  85.             for (int i = 0; i < inputArray.length; i++) {
  86.                
  87.                 for (int j = 0; j < inputArray[i].length; j++) {
  88.                    
  89.                     checkSubSection(inputArray, i, j);
  90.                 }
  91.             }
  92.         } catch (Exception e) {
  93.            
  94.             e.printStackTrace();
  95.         }
  96.     }
  97. }
Add Comment
Please, Sign In to add comment