fork(2) download
  1. /* paiza POH! vol.2
  2.  * result:
  3.  * http://p...content-available-to-author-only...a.jp/poh/paizen/result/52163a4e1d60e34a8e854214187c4699
  4.  * author: Leonardone @ NEETSDKASU
  5.  */
  6. import java.util.*;
  7. import java.lang.*;
  8. import java.io.*;
  9.  
  10. class Main
  11. {
  12. public static void main (String[] args) throws java.lang.Exception
  13. {
  14.  
  15. String[] hw = in.readLine().split(" ");
  16. final int H = Integer.valueOf(hw[0]); // ホーム画面縦の区画数
  17. final int W = Integer.valueOf(hw[1]); // ホーム画面横の区画数
  18.  
  19. int[][] space2left = new int[H][W];
  20. int[][] space2top = new int[H][W];
  21. int spacecount = 0;
  22.  
  23. List<Position> startposition2left = new ArrayList<Position>();
  24. List<Position> startposition2top = new ArrayList<Position>();
  25.  
  26. for (int y = 0; y < H; y++)
  27. {
  28. String line = in.readLine();
  29. int space2leftSize = 0;
  30. for (int x = 0; x < W; x++)
  31. {
  32. char ch = line.charAt(x);
  33. if (ch == '1')
  34. {
  35. space2leftSize = 0;
  36. space2top[y][x] = 0;
  37. if (x > 0 && space2left[y][x - 1] > 0)
  38. {
  39. startposition2left.add(
  40. new Position(y, x - 1));
  41. }
  42. if (y > 0 && space2top[y - 1][x] > 0)
  43. {
  44. startposition2top.add(
  45. new Position(y - 1, x));
  46. }
  47. }
  48. else // if (ch == '0')
  49. {
  50. spacecount++;
  51. space2leftSize++;
  52. if (y > 0)
  53. {
  54. space2top[y][x] = space2top[y - 1][x] + 1;
  55. if (y == H - 1)
  56. {
  57. startposition2top.add(
  58. new Position(y, x));
  59. }
  60. }
  61. else // if (y == 0)
  62. {
  63. space2top[y][x] = 1;
  64. }
  65. if (x == W - 1)
  66. {
  67. startposition2left.add(
  68. new Position(y, x));
  69. }
  70. }
  71. space2left[y][x] = space2leftSize;
  72. }
  73. }
  74. // print2dArray(space2left);
  75. // print2dArray(space2top);
  76. // System.out.println(startposition2left);
  77. // System.out.println(startposition2top);
  78.  
  79. final int N = Integer.valueOf(in.readLine()); // ウィジェット数
  80. HashSet<Position> placeablePosition = new HashSet<Position>();
  81.  
  82. for (int i = 0; i < N; i++)
  83. {
  84. String[] st = in.readLine().split(" ");
  85. int s = Integer.valueOf(st[0]); // ウィジェットの縦サイズ
  86. int t = Integer.valueOf(st[1]); // ウィジェットの横サイズ
  87. int placeableCount = 0;
  88.  
  89. if (s == 1) // 横長のウィジェット
  90. {
  91. if (t == 1) // 1x1のウィジェット
  92. {
  93. placeableCount = spacecount;
  94. }
  95. else // if (t != 1) // 横長のウィジェット
  96. {
  97. for (Position position : startposition2left)
  98. {
  99. int spaceWSize = space2left[position.y][position.x];
  100. if (spaceWSize >= t)
  101. {
  102. placeableCount += spaceWSize - t + 1;
  103. }
  104. }
  105. }
  106. }
  107. else if (t == 1) // 縦長のウィジェット
  108. {
  109. for (Position position : startposition2top)
  110. {
  111. int spaceHSize = space2top[position.y][position.x];
  112. if (spaceHSize >= s)
  113. {
  114. placeableCount += spaceHSize - s + 1;
  115. }
  116. }
  117. }
  118. else // if (s != 1 && t != 1)
  119. {
  120. placeablePosition.clear();
  121. // 横に走査していく
  122. for (Position position : startposition2left)
  123. {
  124. int spaceWSize = space2left[position.y][position.x];
  125. for (int dx = 0; dx < spaceWSize; dx++)
  126. {
  127. int x = position.x - dx;
  128. int spaceHSize = space2top[position.y][x];
  129. if (spaceHSize < s)
  130. {
  131. continue; // 縦サイズが足りない
  132. }
  133. // 縦に走査して横サイズ以上の連続する空所を数える
  134. int count = 0;
  135. for (int dy = 0; dy < spaceHSize; dy++)
  136. {
  137. int y = position.y - dy;
  138. if (space2left[y][x] >= t)
  139. {
  140. count++;
  141. if (count >= s) // ウィジェットが確保できる
  142. {
  143. boolean placed = placeablePosition.add(
  144. new Position(y, x));
  145. if (placed == false)
  146. {
  147. break; // これ以上の縦の走査は走査済み
  148. }
  149. }
  150. }
  151. else
  152. {
  153. count = 0; // 連続の途切れ
  154. }
  155. }
  156. }
  157. }
  158. placeableCount = placeablePosition.size();
  159. }
  160. System.out.println(placeableCount);
  161. }
  162.  
  163. } // end of main(String[])
  164.  
  165. static void print2dArray(int[][] array)
  166. {
  167. for (int i = 0; i < array.length; i++)
  168. {
  169. for (int j = 0; j < array[i].length; j++)
  170. {
  171. System.out.print(array[i][j] + " ");
  172. }
  173. System.out.println();
  174. }
  175. System.out.println();
  176. }
  177.  
  178. static class Position
  179. {
  180. final int y;
  181. final int x;
  182. Position(int y, int x)
  183. {
  184. this.y = y;
  185. this.x = x;
  186. }
  187.  
  188. @Override public String toString()
  189. {
  190. return "(" + x + ", " + y + ")";
  191. }
  192.  
  193. @Override public int hashCode()
  194. {
  195. return y * (x + y);
  196. }
  197.  
  198. @Override public boolean equals(Object obj)
  199. {
  200. if (this == obj)
  201. {
  202. return true;
  203. }
  204. if (obj == null)
  205. {
  206. return false;
  207. }
  208. if (this.getClass().equals(obj.getClass()) == false)
  209. {
  210. return false;
  211. }
  212. Position pos = (Position)obj;
  213. return y == pos.y && x == pos.x;
  214. }
  215. } // end of class Position
  216. }
  217.  
Success #stdin #stdout 0.08s 380224KB
stdin
5 5
00000
00100
00010
10001
10000
3
2 2
1 1
3 2
stdout
6
20
2