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