Source code for submission s1051

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. boolean xFound = false;
  87. for (int i = 0; i < rows; i++) {
  88. map[i] = nextToken();
  89.  
  90. if(!xFound){
  91. xFound = map[i].indexOf("X") != -1;
  92. }
  93. }
  94.  
  95. if(!xFound){
  96. System.out.println("0");
  97. return true;
  98. }
  99.  
  100. if(tileRows >= rows && tileCols >= cols){
  101. System.out.println("1");
  102. return true;
  103. }
  104.  
  105. /*
  106.   System.out.println("");
  107.   for(int i = 0; i < rows*3 ; i++){
  108.   System.out.println(map[i]);
  109.   }
  110.   System.out.println("");
  111.   * */
  112.  
  113. TileStructure ts = new TileStructure(rows, cols, tileRows, tileCols, map);
  114.  
  115. int min = Integer.MAX_VALUE;
  116.  
  117. for (int i = 0; i < ((tileRows >= rows)?(1):(tileRows)); i++) {
  118. for (int j = 0; j < ((tileCols >= cols)?(1):(tileCols)); j++) {
  119. //System.out.println("Iteration[" + (-i) + "," + (-j) + "]");
  120. min = Math.min(min, ts.getNumber(-i, -j));
  121. }
  122. }
  123.  
  124. System.out.println(min);
  125.  
  126. return true;
  127. }
  128. }
  129.  
  130. class TileStructure {
  131.  
  132. int tilesH;
  133. int tilesW;
  134. int tileRows;
  135. int tileCols;
  136. String[] map;
  137.  
  138. public TileStructure(int rows, int cols, int tileRows, int tileCols, String[] map) {
  139. tilesH = rows / tileRows + ((rows % tileRows == 0) ? (0) : (1)) + 1;
  140. tilesW = cols / tileCols + ((cols % tileCols == 0) ? (0) : (1)) + 1;
  141.  
  142. this.tileCols = tileCols;
  143. this.tileRows = tileRows;
  144. this.map = map;
  145. }
  146.  
  147. public int getNumber(int xOffset, int yOffset) {
  148. int count = 0;
  149.  
  150. for (int h = 0; h < tilesH; h++) {
  151. for (int w = 0; w < tilesW; w++) {
  152. boolean hasX = false;
  153. for (int ht = h * tileRows; ht < h * tileRows + tileRows && !hasX; ht++) {
  154. for (int wt = w * tileCols; wt < w * tileCols + tileCols && !hasX; wt++) {
  155. //System.out.print("T[" + h + "," + w + "]-[" + (ht+rows) + "," + (wt+cols) + "] = ");
  156. if((ht + xOffset >= 0) && (ht + xOffset < map.length) && (wt + yOffset >= 0) && (wt + yOffset < map[0].length())){
  157. if (map[ht + xOffset].charAt(wt + yOffset) == 'X') {
  158. hasX = true;
  159. }
  160. }
  161. //System.out.println("X");
  162. //}else{
  163. //System.out.println(".");
  164. //}
  165. }
  166. }
  167. if (hasX) {
  168. count++;
  169. }
  170. }
  171. }
  172.  
  173. return count;
  174. }
  175. }
  176.  

Diff to submission s1036

Fm.java

--- c5.s1036.cteam007.fm.java.0.Fm.java
+++ c5.s1051.cteam007.fm.java.0.Fm.java
@@ -117,6 +117,6 @@
         int min = Integer.MAX_VALUE;
 
-        for (int i = 0; i < tileRows; i++) {
-            for (int j = 0; j < tileCols; j++) {
+        for (int i = 0; i < ((tileRows >= rows)?(1):(tileRows)); i++) {
+            for (int j = 0; j < ((tileCols >= cols)?(1):(tileCols)); j++) {
                 //System.out.println("Iteration[" + (-i) + "," + (-j) + "]");
                 min = Math.min(min, ts.getNumber(-i, -j));