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