Source code for submission s1011

Go to diff to previous submission

Fm.java

  1. /*
  2.  * CTU Open 2013
  3.  * HES
  4.  */
  5. package template;
  6.  
  7. import java.io.BufferedReader;
  8. import java.io.InputStreamReader;
  9. import java.util.StringTokenizer;
  10.  
  11. /**
  12.  *
  13.  * @author cteam007
  14.  */
  15. public class Fm {
  16.  
  17.  
  18. void useLine(String s) {
  19. st = new StringTokenizer(s);
  20. }
  21.  
  22. String nextToken() throws Exception {
  23. while (!st.hasMoreTokens()) {
  24. st = new StringTokenizer(input.readLine());
  25. }
  26. return st.nextToken();
  27. }
  28.  
  29. int nextInt() throws Exception {
  30. return Integer.parseInt(nextToken());
  31. }
  32.  
  33. /**
  34.   * @param args the command line arguments
  35.   */
  36. public static void main(String[] args) throws Exception {
  37. Fm instance = new Fm();
  38. while (instance.run()) {
  39. }
  40. }
  41.  
  42. boolean run() throws Exception {
  43. String in = input.readLine();
  44. if (in == null) {
  45. return false;
  46. }
  47.  
  48. useLine(in);
  49.  
  50. int rows = nextInt();
  51. int cols = nextInt();
  52.  
  53. int tileRows = nextInt();
  54. int tileCols = nextInt();
  55.  
  56. /*
  57.   int colsapper = Math.max(cols, tileCols);
  58.   int rowsapper = Math.max(rows, tileRows);
  59.   */
  60.  
  61. String[] map = new String[rows]; //+ rowsapper + rowsapper + tileRows];
  62.  
  63. /*
  64.   String rowAddon = "";
  65.   String colAddon = "";
  66.   String rowAddon2 = "";
  67.   for (int ra = 0; ra < colsapper; ra++) {
  68.   rowAddon += ".";
  69.   }
  70.   for (int ra = 0; ra < tileRows; ra++) {
  71.   rowAddon2 += ".";
  72.   }
  73.   for (int ra = 0; ra < colsapper + colsapper + cols + tileCols; ra++) {
  74.   colAddon += ".";
  75.   }
  76.   for (int i = 0; i < rowsapper; i++) {
  77.   map[i] = colAddon;
  78.   }
  79.   for (int i = rowsapper; i < rowsapper + rows; i++) {
  80.   map[i] = rowAddon + nextToken() + rowAddon;
  81.   }
  82.   for (int i = rowsapper + rows; i < rows + rowsapper + rowsapper; i++) {
  83.   map[i] = colAddon;
  84.   }*/
  85.  
  86. for (int i = 0; i < rows; i++) {
  87. map[i] = nextToken();
  88. }
  89.  
  90. /*
  91.   System.out.println("");
  92.   for(int i = 0; i < rows*3 ; i++){
  93.   System.out.println(map[i]);
  94.   }
  95.   System.out.println("");
  96.   * */
  97.  
  98. TileStructure ts = new TileStructure(rows, cols, tileRows, tileCols, map);
  99.  
  100. int min = Integer.MAX_VALUE;
  101.  
  102. for (int i = 0; i < tileRows; i++) {
  103. for (int j = 0; j < tileCols; j++) {
  104. //System.out.println("Iteration[" + (-i) + "," + (-j) + "]");
  105. min = Math.min(min, ts.getNumber(-i, -j));
  106. }
  107. }
  108.  
  109. System.out.println(min);
  110.  
  111. return true;
  112. }
  113. }
  114.  
  115. class TileStructure {
  116.  
  117. int tilesH;
  118. int tilesW;
  119. int tileRows;
  120. int tileCols;
  121. String[] map;
  122.  
  123. public TileStructure(int rows, int cols, int tileRows, int tileCols, String[] map) {
  124. tilesH = rows / tileRows + ((rows % tileRows == 0) ? (0) : (1)) + 1;
  125. tilesW = cols / tileCols + ((cols % tileCols == 0) ? (0) : (1)) + 1;
  126.  
  127. this.tileCols = tileCols;
  128. this.tileRows = tileRows;
  129. this.map = map;
  130. }
  131.  
  132. public int getNumber(int xOffset, int yOffset) {
  133. int count = 0;
  134.  
  135. for (int h = 0; h < tilesH; h++) {
  136. for (int w = 0; w < tilesW; w++) {
  137. boolean hasX = false;
  138. for (int ht = h * tileRows; ht < h * tileRows + tileRows && !hasX; ht++) {
  139. for (int wt = w * tileCols; wt < w * tileCols + tileCols && !hasX; wt++) {
  140. //System.out.print("T[" + h + "," + w + "]-[" + (ht+rows) + "," + (wt+cols) + "] = ");
  141. if((ht + xOffset >= 0) && (ht + xOffset < map.length) && (wt + yOffset >= 0) && (wt + yOffset < map[0].length())){
  142. if (map[ht + xOffset].charAt(wt + yOffset) == 'X') {
  143. hasX = true;
  144. }
  145. }
  146. //System.out.println("X");
  147. //}else{
  148. //System.out.println(".");
  149. //}
  150. }
  151. }
  152. if (hasX) {
  153. count++;
  154. }
  155. }
  156. }
  157.  
  158. return count;
  159. }
  160. }
  161.  

Diff to submission s936

Fm.java

--- c5.s936.cteam007.fm.java.0.Fm.java
+++ c5.s1011.cteam007.fm.java.0.Fm.java
@@ -56,50 +56,59 @@
         int tileCols = nextInt();
 
-        String[] map = new String[rows * 3];
+        /*
+        int colsapper = Math.max(cols, tileCols);
+        int rowsapper = Math.max(rows, tileRows);
+         */
 
+        String[] map = new String[rows]; //+ rowsapper + rowsapper + tileRows];
+
+        /*
         String rowAddon = "";
         String colAddon = "";
-
-        for (int ra = 0; ra < cols; ra++) {
-            rowAddon += ".";
+        String rowAddon2 = "";
+        for (int ra = 0; ra < colsapper; ra++) {
+        rowAddon += ".";
         }
-
-        for (int ra = 0; ra < cols * 3; ra++) {
-            colAddon += ".";
+        for (int ra = 0; ra < tileRows; ra++) {
+        rowAddon2 += ".";
         }
+        for (int ra = 0; ra < colsapper + colsapper + cols + tileCols; ra++) {
+        colAddon += ".";
+        }
+        for (int i = 0; i < rowsapper; i++) {
+        map[i] = colAddon;
+        }
+        for (int i = rowsapper; i < rowsapper + rows; i++) {
+        map[i] = rowAddon + nextToken() + rowAddon;
+        }
+        for (int i = rowsapper + rows; i < rows + rowsapper + rowsapper; i++) {
+        map[i] = colAddon;
+        }*/
 
         for (int i = 0; i < rows; i++) {
-            map[i] = colAddon;
+            map[i] = nextToken();
         }
 
-        for (int i = rows; i < rows * 2; i++) {
-            map[i] = rowAddon + nextToken() + rowAddon;
-        }
-        
-        for (int i = rows*2; i < rows*3; i++) {
-            map[i] = colAddon;
-        }
-        
         /*
         System.out.println("");
         for(int i = 0; i < rows*3 ; i++){
-            System.out.println(map[i]);
+        System.out.println(map[i]);
         }   
         System.out.println("");
          * */
-        
+
         TileStructure ts = new TileStructure(rows, cols, tileRows, tileCols, map);
 
         int min = Integer.MAX_VALUE;
-                
+
         for (int i = 0; i < tileRows; i++) {
             for (int j = 0; j < tileCols; j++) {
                 //System.out.println("Iteration[" + (-i) + "," + (-j) + "]");
-                min = Math.min(min,ts.getNumber(-i, -j));
+                min = Math.min(min, ts.getNumber(-i, -j));
             }
         }
 
         System.out.println(min);
-        
+
         return true;
     }
@@ -112,6 +121,4 @@
     int tileRows;
     int tileCols;
-    int cols;
-    int rows;
     String[] map;
 
@@ -123,7 +130,4 @@
         this.tileRows = tileRows;
         this.map = map;
-        
-        this.cols = cols;
-        this.rows = rows;
     }
 
@@ -137,11 +141,13 @@
                     for (int wt = w * tileCols; wt < w * tileCols + tileCols && !hasX; wt++) {
                         //System.out.print("T[" + h + "," + w + "]-[" + (ht+rows) + "," + (wt+cols) + "] = ");
-                        if (map[ht+rows+xOffset].charAt(wt+cols+yOffset) == 'X') {
-                            hasX = true;
+                        if((ht + xOffset >= 0) && (ht + xOffset < map.length) && (wt + yOffset >= 0) && (wt + yOffset < map[0].length())){
+                            if (map[ht + xOffset].charAt(wt + yOffset) == 'X') {
+                                hasX = true;
+                            }
                         }
-                            //System.out.println("X");
-                        //}else{
-                            //System.out.println(".");
-                        //}
+                    //System.out.println("X");
+                    //}else{
+                    //System.out.println(".");
+                    //}
                     }
                 }