fork(1) download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4.  
  5. class Main
  6. {
  7. public static void main (String[] args) throws java.lang.Exception
  8. {
  9. Paiza.getInstance().resolve(new MyResolver());
  10. }
  11. }
  12.  
  13. class MyResolver extends Resolver
  14. {
  15. @Override
  16. public int resolve(Paiza.Widget widget) {
  17. int count = 0;
  18. for (int y = 0; y < home.getH(); y++) {
  19. for (int x = 0; x < home.getW(); x++) {
  20. boolean flag = true;
  21. for (int dy = 0; flag && dy < widget.getS(); dy++) {
  22. for (int dx = 0; flag && dx < widget.getT(); dx++) {
  23. if (home.isSpace(x + dx, y + dy) == false) {
  24. flag = false;
  25. }
  26. }
  27. }
  28. if (flag) {
  29. count++;
  30. }
  31. }
  32. }
  33. return count;
  34. }
  35. }
  36.  
  37.  
  38. abstract class Resolver
  39. {
  40. protected Paiza.Home home;
  41.  
  42. public void setHome(Paiza.Home home) {
  43. this.home = home;
  44. }
  45.  
  46. public abstract int resolve(Paiza.Widget widget);
  47. }
  48.  
  49. final class Paiza
  50. {
  51. private static Paiza paiza = null;
  52.  
  53. private Home home;
  54. private ArrayList<Widget> widgets;
  55.  
  56. private Paiza() throws java.lang.Exception {
  57. String[] hw = in.readLine().split(" ");
  58. home = new Home(Integer.parseInt(hw[0]), Integer.parseInt(hw[1]));
  59. for (int y = 0; y < home.getH(); y++) {
  60. String line = in.readLine();
  61. for (int x = 0; x < home.getW(); x++) {
  62. home.setDisplay(x, y, (int)(line.charAt(x) - '0'));
  63. }
  64. }
  65. int N = Integer.parseInt(in.readLine());
  66. widgets = new ArrayList<Widget>(N);
  67. for (int i = 0; i< N; i++) {
  68. String[] st = in.readLine().split(" ");
  69. widgets.add(new Widget(Integer.parseInt(st[0]), Integer.parseInt(st[1])));
  70. }
  71. }
  72.  
  73. private void answer(int count) {
  74. System.out.println(count);
  75. }
  76.  
  77. public void resolve(Resolver resolver) {
  78. resolver.setHome(home);
  79. for (Widget widget : widgets) {
  80. int count = resolver.resolve(widget);
  81. answer(count);
  82. }
  83. }
  84.  
  85. public static Paiza getInstance() throws java.lang.Exception {
  86. if (paiza == null) {
  87. paiza = new Paiza();
  88. }
  89. return paiza;
  90. }
  91.  
  92. public final class Home
  93. {
  94. private final int H;
  95. private final int W;
  96. private int[][] display;
  97. private Home(int H, int W) {
  98. this.H = H;
  99. this.W = W;
  100. display = new int[H][W];
  101. }
  102.  
  103. private void setDisplay(int x, int y, int e) throws java.lang.Exception {
  104. if (x < 0 || x >= W) {
  105. throw new ArrayIndexOutOfBoundsException("x : " + x);
  106. }
  107. if (y < 0 || y >= H) {
  108. throw new ArrayIndexOutOfBoundsException("y : " + y);
  109. }
  110. if (e != 0 && e != 1) {
  111. throw new IllegalArgumentException("e");
  112. }
  113. display[y][x] = e;
  114. }
  115.  
  116. public int getH() {
  117. return H;
  118. }
  119.  
  120. public int getW() {
  121. return W;
  122. }
  123.  
  124. public boolean isSpace(int x, int y) {
  125. if (x < 0 || y < 0 || x >= W || y >= H) {
  126. return false;
  127. }
  128. return display[y][x] == 0;
  129. }
  130. }
  131.  
  132. public final class Widget
  133. {
  134. private final int s;
  135. private final int t;
  136. private Widget(int s, int t) {
  137. this.s = s;
  138. this.t = t;
  139. }
  140.  
  141. public int getS() {
  142. return s;
  143. }
  144.  
  145. public int getT() {
  146. return t;
  147. }
  148. }
  149.  
  150. }
  151.  
Success #stdin #stdout 0.07s 380160KB
stdin
5 5
00000
00100
00010
10001
10000
3
2 2
1 1
3 2
stdout
6
20
2