fork download
  1. /* paiza POH! vol.2
  2.  * result:
  3.  * http://p...content-available-to-author-only...a.jp/poh/paizen/result/a29b5a39cf6ef5b2fbb4f8ae472ff275
  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.  
  15. String[] hw = in.readLine().split(" ");
  16. final int H = Integer.parseInt(hw[0]); // ホーム画面縦の区画数
  17. final int W = Integer.parseInt(hw[1]); // ホーム画面横の区画数
  18.  
  19. int[][] home = new int[H][W];
  20. int[][] hoge = new int[H][W];
  21. int spacecount = 0;
  22.  
  23. for (int y = 0; y < H; y++)
  24. {
  25. String line = in.readLine();
  26. int count = 0;
  27. for (int x = 0; x < W; x++)
  28. {
  29. int tx = W - x - 1;
  30. char ch = line.charAt(tx);
  31. if (ch == '0')
  32. {
  33. spacecount++;
  34. count++;
  35. if (y > 0)
  36. {
  37. hoge[y][tx] = hoge[y - 1][tx] + 1;
  38. }
  39. else
  40. {
  41. hoge[y][tx] = hoge[y][tx] = 1;
  42. }
  43. }
  44. else
  45. {
  46. count = 0;
  47. hoge[y][tx] = 0;
  48. }
  49. home[y][tx] = count;
  50. }
  51. }
  52.  
  53. final int N = Integer.parseInt(in.readLine()); // ウィジェット数
  54.  
  55.  
  56. int[][] result = new int[300][300];
  57.  
  58. if (W > H)
  59. {
  60. for (int i = 0; i < N; i++)
  61. {
  62. String[] st = in.readLine().split(" ");
  63. int s = Integer.parseInt(st[0]); // ウィジェットの縦サイズ
  64. int t = Integer.parseInt(st[1]); // ウィジェットの横サイズ
  65.  
  66. if (s > H || t > W)
  67. {
  68. System.out.println(0);
  69. continue;
  70. }
  71.  
  72. if (result[s][t] > 0)
  73. {
  74. System.out.println(result[s][t] - 1);
  75. continue;
  76. }
  77.  
  78. if (s == 1 && t == 1)
  79. {
  80. System.out.println(spacecount);
  81. continue;
  82. }
  83.  
  84. int count = 0;
  85.  
  86. for (int hy = H - 1; hy >= s - 1; hy--)
  87. {
  88. for (int hx = 0; hx < W - t + 1; hx++)
  89. {
  90. if (home[hy][hx] < t)
  91. {
  92. hx += home[hy][hx];
  93. continue;
  94. }
  95. if (hoge[hy][hx] < s)
  96. {
  97. continue;
  98. }
  99. int dy;
  100. for (dy = 1; dy < s; dy++)
  101. {
  102. int y = hy - dy;
  103. if (y >= H)
  104. {
  105. break;
  106. }
  107. if (home[y][hx] < t)
  108. {
  109. break;
  110. }
  111. }
  112. if (dy == s)
  113. {
  114. count++;
  115. }
  116. }
  117. }
  118. System.out.println(count);
  119. result[s][t] = count + 1;
  120. }
  121. }
  122. else
  123. {
  124. for (int i = 0; i < N; i++)
  125. {
  126. String[] st = in.readLine().split(" ");
  127. int s = Integer.parseInt(st[0]); // ウィジェットの縦サイズ
  128. int t = Integer.parseInt(st[1]); // ウィジェットの横サイズ
  129.  
  130. if (s > H || t > W)
  131. {
  132. System.out.println(0);
  133. continue;
  134. }
  135.  
  136. if (result[s][t] > 0)
  137. {
  138. System.out.println(result[s][t] - 1);
  139. continue;
  140. }
  141.  
  142. if (s == 1 && t == 1)
  143. {
  144. System.out.println(spacecount);
  145. continue;
  146. }
  147.  
  148. int count = 0;
  149.  
  150. for (int hx = 0; hx < W - t + 1; hx++)
  151. {
  152. for (int hy = H - 1; hy >= s - 1; hy--)
  153. {
  154. if (home[hy][hx] < t)
  155. {
  156. continue;
  157. }
  158. if (hoge[hy][hx] < s)
  159. {
  160. hy -= hoge[hy][hx];
  161. continue;
  162. }
  163. int dy;
  164. for (dy = 1; dy < s; dy++)
  165. {
  166. int y = hy - dy;
  167. if (y >= H)
  168. {
  169. break;
  170. }
  171. if (home[y][hx] < t)
  172. {
  173. break;
  174. }
  175. }
  176. if (dy == s)
  177. {
  178. count++;
  179. }
  180. }
  181. }
  182. System.out.println(count);
  183. result[s][t] = count + 1;
  184. }
  185. }
  186.  
  187. } // end of main(String[])
  188.  
  189. }
  190.  
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