fork(1) download
  1. /* paiza POH! vol.2
  2.  * result:
  3.  * http://p...content-available-to-author-only...a.jp/poh/paizen/result/01c5aa602db291b371046ecb654e6cbb
  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. if (widget.getS() > widget.getT()) {
  59. return resolve2bottom(widget);
  60. } else {
  61. return resolve2right(widget);
  62. }
  63. }
  64.  
  65. private int resolve2right(Paiza.Widget widget) {
  66. int count = 0;
  67. for (int y = 0; y <= home.getH() - widget.getS(); y++) {
  68. for (int x = 0; x <= home.getW() - widget.getT(); x++) {
  69. if (space2bottom[y][x] >= widget.getS()) {
  70. boolean placeable = true;
  71. for (int dy = 0; placeable && dy < widget.getS(); dy++) {
  72. if (space2right[y + dy][x] < widget.getT()) {
  73. placeable = false;
  74. x += space2right[y + dy][x];
  75. }
  76. }
  77. if (placeable) {
  78. count++;
  79. }
  80. } else if (space2right[y][x] < widget.getT()) {
  81. x += space2right[y][x];
  82. }
  83. }
  84. }
  85. return count;
  86. }
  87.  
  88. private int resolve2bottom(Paiza.Widget widget) {
  89. int count = 0;
  90. for (int x = 0; x <= home.getW() - widget.getT(); x++) {
  91. for (int y = 0; y <= home.getH() - widget.getS(); y++) {
  92. if (space2right[y][x] >= widget.getT()) {
  93. boolean placeable = true;
  94. for (int dx = 0; placeable && dx < widget.getT(); dx++) {
  95. if (space2bottom[y][x + dx] < widget.getS()) {
  96. placeable = false;
  97. y += space2bottom[y][x + dx];
  98. }
  99. }
  100. if (placeable) {
  101. count++;
  102. }
  103. } else if (space2bottom[y][x] < widget.getS()) {
  104. y += space2bottom[y][x];
  105. }
  106. }
  107. }
  108. return count;
  109. }
  110.  
  111. }
  112.  
  113.  
  114. abstract class Resolver
  115. {
  116. protected Paiza.Home home;
  117.  
  118. public void setHome(Paiza.Home home) {
  119. this.home = home;
  120. }
  121.  
  122. public abstract int resolve(Paiza.Widget widget);
  123. }
  124.  
  125. final class Paiza
  126. {
  127. public static abstract class Resolver
  128. {
  129. protected Paiza.Home home;
  130.  
  131. public void setHome(Paiza.Home home) {
  132. this.home = home;
  133. }
  134.  
  135. public abstract int resolve(Paiza.Widget widget);
  136. }
  137.  
  138. public static Paiza getInstance() throws java.lang.Exception {
  139. if (paiza == null) {
  140. paiza = new Paiza();
  141. }
  142. return paiza;
  143. }
  144.  
  145. public void resolve(Resolver resolver) {
  146. init(resolver);
  147. for (Widget widget : widgets) {
  148. answer(resolver.resolve(widget));
  149. }
  150. flush();
  151. }
  152.  
  153.  
  154. public final class Home
  155. {
  156. private final int H;
  157. private final int W;
  158. private int[][] display;
  159. private Home(int H, int W) {
  160. this.H = H;
  161. this.W = W;
  162. display = new int[H][W];
  163. }
  164.  
  165. private void setDisplay(int x, int y, int e) throws java.lang.Exception {
  166. if (x < 0 || x >= W) {
  167. throw new ArrayIndexOutOfBoundsException("x : " + x);
  168. }
  169. if (y < 0 || y >= H) {
  170. throw new ArrayIndexOutOfBoundsException("y : " + y);
  171. }
  172. if (e != 0 && e != 1) {
  173. throw new IllegalArgumentException("e");
  174. }
  175. display[y][x] = e;
  176. }
  177.  
  178. public int getH() {
  179. return H;
  180. }
  181.  
  182. public int getW() {
  183. return W;
  184. }
  185.  
  186. public boolean isSpace(int x, int y) {
  187. if (x < 0 || y < 0 || x >= W || y >= H) {
  188. return false;
  189. }
  190. return display[y][x] == 0;
  191. }
  192. }
  193.  
  194. public final class Widget
  195. {
  196. private final int s;
  197. private final int t;
  198. private Widget(int s, int t) {
  199. this.s = s;
  200. this.t = t;
  201. }
  202.  
  203. public int getS() {
  204. return s;
  205. }
  206.  
  207. public int getT() {
  208. return t;
  209. }
  210. }
  211.  
  212. private static Paiza paiza = null;
  213.  
  214. private Home home;
  215. private ArrayList<Widget> widgets;
  216.  
  217. private Paiza() throws java.lang.Exception {
  218. String[] hw = in.readLine().split(" ");
  219. home = new Home(Integer.parseInt(hw[0]), Integer.parseInt(hw[1]));
  220. for (int y = 0; y < home.getH(); y++) {
  221. String line = in.readLine();
  222. for (int x = 0; x < home.getW(); x++) {
  223. home.setDisplay(x, y, (int)(line.charAt(x) - '0'));
  224. }
  225. }
  226. int N = Integer.parseInt(in.readLine());
  227. widgets = new ArrayList<Widget>(N);
  228. for (int i = 0; i< N; i++) {
  229. String[] st = in.readLine().split(" ");
  230. widgets.add(new Widget(Integer.parseInt(st[0]), Integer.parseInt(st[1])));
  231. }
  232. }
  233.  
  234. private StringBuilder output = null;
  235. private static final String NEWLINE = System.getProperty("line.separator");
  236.  
  237. private void init(Resolver resolver) {
  238. resolver.setHome(home);
  239. output = new StringBuilder(widgets.size() * 6);
  240. }
  241.  
  242. private void answer(int count) {
  243. output.append(count);
  244. output.append(NEWLINE);
  245. }
  246.  
  247. private void flush() {
  248. System.out.print(output);
  249. }
  250. }
  251.  
Success #stdin #stdout 0.07s 380224KB
stdin
5 5
00000
00100
00010
10001
10000
3
2 2
1 1
3 2
stdout
6
20
2