fork(1) download
  1. /* paiza POH! vol.2
  2.  * result:
  3.  * http://p...content-available-to-author-only...a.jp/poh/paizen/result/cfe6e3175171c1f82183765fdba207cf
  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. Paiza.getInstance().resolve(new MyResolver());
  15. }
  16. }
  17.  
  18. class MyResolver extends Paiza.Resolver
  19. {
  20. int[][] space2left = null;
  21.  
  22. @Override
  23. public void setHome(Paiza.Home home) {
  24. super.setHome(home);
  25.  
  26. space2left = new int[home.getH()][home.getW()];
  27.  
  28. for (int y = 0; y < home.getH(); y++) {
  29. int count = 0;
  30. for (int x = home.getW() - 1; x >= 0; x--) {
  31. if (home.isSpace(x, y)) {
  32. count++;
  33. } else {
  34. count = 0;
  35. }
  36. space2left[y][x] = count;
  37. }
  38. }
  39. }
  40.  
  41. @Override
  42. public int resolve(Paiza.Widget widget) {
  43. int count = 0;
  44. for (int y = 0; y <= home.getH() - widget.getS(); y++) {
  45. for (int x = 0; x <= home.getW() - widget.getT(); x++) {
  46. boolean placeable = true;
  47. for (int dy = 0; placeable && dy < widget.getS(); dy++) {
  48. if (space2left[y + dy][x] < widget.getT()) {
  49. placeable = false;
  50. }
  51. }
  52. if (placeable) {
  53. count++;
  54. }
  55. }
  56. }
  57. return count;
  58. }
  59. }
  60.  
  61.  
  62. abstract class Resolver
  63. {
  64. protected Paiza.Home home;
  65.  
  66. public void setHome(Paiza.Home home) {
  67. this.home = home;
  68. }
  69.  
  70. public abstract int resolve(Paiza.Widget widget);
  71. }
  72.  
  73. final class Paiza
  74. {
  75. public static abstract class Resolver
  76. {
  77. protected Paiza.Home home;
  78.  
  79. public void setHome(Paiza.Home home) {
  80. this.home = home;
  81. }
  82.  
  83. public abstract int resolve(Paiza.Widget widget);
  84. }
  85.  
  86. public static Paiza getInstance() throws java.lang.Exception {
  87. if (paiza == null) {
  88. paiza = new Paiza();
  89. }
  90. return paiza;
  91. }
  92.  
  93. public void resolve(Resolver resolver) {
  94. init(resolver);
  95. for (Widget widget : widgets) {
  96. answer(resolver.resolve(widget));
  97. }
  98. flush();
  99. }
  100.  
  101.  
  102. public final class Home
  103. {
  104. private final int H;
  105. private final int W;
  106. private int[][] display;
  107. private Home(int H, int W) {
  108. this.H = H;
  109. this.W = W;
  110. display = new int[H][W];
  111. }
  112.  
  113. private void setDisplay(int x, int y, int e) throws java.lang.Exception {
  114. if (x < 0 || x >= W) {
  115. throw new ArrayIndexOutOfBoundsException("x : " + x);
  116. }
  117. if (y < 0 || y >= H) {
  118. throw new ArrayIndexOutOfBoundsException("y : " + y);
  119. }
  120. if (e != 0 && e != 1) {
  121. throw new IllegalArgumentException("e");
  122. }
  123. display[y][x] = e;
  124. }
  125.  
  126. public int getH() {
  127. return H;
  128. }
  129.  
  130. public int getW() {
  131. return W;
  132. }
  133.  
  134. public boolean isSpace(int x, int y) {
  135. if (x < 0 || y < 0 || x >= W || y >= H) {
  136. return false;
  137. }
  138. return display[y][x] == 0;
  139. }
  140. }
  141.  
  142. public final class Widget
  143. {
  144. private final int s;
  145. private final int t;
  146. private Widget(int s, int t) {
  147. this.s = s;
  148. this.t = t;
  149. }
  150.  
  151. public int getS() {
  152. return s;
  153. }
  154.  
  155. public int getT() {
  156. return t;
  157. }
  158. }
  159.  
  160. private static Paiza paiza = null;
  161.  
  162. private Home home;
  163. private ArrayList<Widget> widgets;
  164.  
  165. private Paiza() throws java.lang.Exception {
  166. String[] hw = in.readLine().split(" ");
  167. home = new Home(Integer.parseInt(hw[0]), Integer.parseInt(hw[1]));
  168. for (int y = 0; y < home.getH(); y++) {
  169. String line = in.readLine();
  170. for (int x = 0; x < home.getW(); x++) {
  171. home.setDisplay(x, y, (int)(line.charAt(x) - '0'));
  172. }
  173. }
  174. int N = Integer.parseInt(in.readLine());
  175. widgets = new ArrayList<Widget>(N);
  176. for (int i = 0; i< N; i++) {
  177. String[] st = in.readLine().split(" ");
  178. widgets.add(new Widget(Integer.parseInt(st[0]), Integer.parseInt(st[1])));
  179. }
  180. }
  181.  
  182. private StringBuilder output = null;
  183. private static final String NEWLINE = System.getProperty("line.separator");
  184.  
  185. private void init(Resolver resolver) {
  186. resolver.setHome(home);
  187. output = new StringBuilder(widgets.size() * 6);
  188. }
  189.  
  190. private void answer(int count) {
  191. output.append(count);
  192. output.append(NEWLINE);
  193. }
  194.  
  195. private void flush() {
  196. System.out.print(output);
  197. }
  198. }
  199.  
Success #stdin #stdout 0.08s 380160KB
stdin
5 5
00000
00100
00010
10001
10000
3
2 2
1 1
3 2
stdout
6
20
2