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