Source code for submission s936

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. String[] map = new String[rows * 3];
  57.  
  58. String rowAddon = "";
  59. String colAddon = "";
  60.  
  61. for (int ra = 0; ra < cols; ra++) {
  62. rowAddon += ".";
  63. }
  64.  
  65. for (int ra = 0; ra < cols * 3; ra++) {
  66. colAddon += ".";
  67. }
  68.  
  69. for (int i = 0; i < rows; i++) {
  70. map[i] = colAddon;
  71. }
  72.  
  73. for (int i = rows; i < rows * 2; i++) {
  74. map[i] = rowAddon + nextToken() + rowAddon;
  75. }
  76.  
  77. for (int i = rows*2; i < rows*3; i++) {
  78. map[i] = colAddon;
  79. }
  80.  
  81. /*
  82.   System.out.println("");
  83.   for(int i = 0; i < rows*3 ; i++){
  84.   System.out.println(map[i]);
  85.   }
  86.   System.out.println("");
  87.   * */
  88.  
  89. TileStructure ts = new TileStructure(rows, cols, tileRows, tileCols, map);
  90.  
  91. int min = Integer.MAX_VALUE;
  92.  
  93. for (int i = 0; i < tileRows; i++) {
  94. for (int j = 0; j < tileCols; j++) {
  95. //System.out.println("Iteration[" + (-i) + "," + (-j) + "]");
  96. min = Math.min(min,ts.getNumber(-i, -j));
  97. }
  98. }
  99.  
  100. System.out.println(min);
  101.  
  102. return true;
  103. }
  104. }
  105.  
  106. class TileStructure {
  107.  
  108. int tilesH;
  109. int tilesW;
  110. int tileRows;
  111. int tileCols;
  112. int cols;
  113. int rows;
  114. String[] map;
  115.  
  116. public TileStructure(int rows, int cols, int tileRows, int tileCols, String[] map) {
  117. tilesH = rows / tileRows + ((rows % tileRows == 0) ? (0) : (1)) + 1;
  118. tilesW = cols / tileCols + ((cols % tileCols == 0) ? (0) : (1)) + 1;
  119.  
  120. this.tileCols = tileCols;
  121. this.tileRows = tileRows;
  122. this.map = map;
  123.  
  124. this.cols = cols;
  125. this.rows = rows;
  126. }
  127.  
  128. public int getNumber(int xOffset, int yOffset) {
  129. int count = 0;
  130.  
  131. for (int h = 0; h < tilesH; h++) {
  132. for (int w = 0; w < tilesW; w++) {
  133. boolean hasX = false;
  134. for (int ht = h * tileRows; ht < h * tileRows + tileRows && !hasX; ht++) {
  135. for (int wt = w * tileCols; wt < w * tileCols + tileCols && !hasX; wt++) {
  136. //System.out.print("T[" + h + "," + w + "]-[" + (ht+rows) + "," + (wt+cols) + "] = ");
  137. if (map[ht+rows+xOffset].charAt(wt+cols+yOffset) == 'X') {
  138. hasX = true;
  139. }
  140. //System.out.println("X");
  141. //}else{
  142. //System.out.println(".");
  143. //}
  144. }
  145. }
  146. if (hasX) {
  147. count++;
  148. }
  149. }
  150. }
  151.  
  152. return count;
  153. }
  154. }
  155.