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