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