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