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