fork(2) download
  1. /* paiza POH! vol.2
  2.  * result:
  3.  * http://p...content-available-to-author-only...a.jp/poh/paizen/result/0bf10df52c3155b2048dd41ef28f648a
  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. int[][] result = new int[300][300];
  56.  
  57. for (int i = 0; i < N; i++)
  58. {
  59. String[] st = in.readLine().split(" ");
  60. int s = Integer.parseInt(st[0]); // ウィジェットの縦サイズ
  61. int t = Integer.parseInt(st[1]); // ウィジェットの横サイズ
  62.  
  63. if (s > H || t > W)
  64. {
  65. System.out.println(0);
  66. continue;
  67. }
  68.  
  69. if (result[s][t] > 0)
  70. {
  71. System.out.println(result[s][t] - 1);
  72. continue;
  73. }
  74.  
  75. if (s == 1 && t == 1)
  76. {
  77. System.out.println(spacecount);
  78. continue;
  79. }
  80.  
  81. int count = 0;
  82. if (W > H)
  83. {
  84. for (int hy = H - 1; hy >= s - 1; hy--)
  85. {
  86. for (int hx = 0; hx < W - t + 1; hx++)
  87. {
  88. if (home[hy][hx] < t)
  89. {
  90. hx += home[hy][hx];
  91. continue;
  92. }
  93. if (hoge[hy][hx] < s)
  94. {
  95. continue;
  96. }
  97. int dy;
  98. for (dy = 1; dy < s; dy++)
  99. {
  100. int y = hy - dy;
  101. if (y >= H)
  102. {
  103. break;
  104. }
  105. if (home[y][hx] < t)
  106. {
  107. break;
  108. }
  109. }
  110. if (dy == s)
  111. {
  112. count++;
  113. }
  114. }
  115. }
  116. }
  117. else
  118. {
  119. for (int hx = 0; hx < W - t + 1; hx++)
  120. {
  121. for (int hy = H - 1; hy >= s - 1; hy--)
  122. {
  123. if (home[hy][hx] < t)
  124. {
  125. continue;
  126. }
  127. if (hoge[hy][hx] < s)
  128. {
  129. hy -= hoge[hy][hx];
  130. continue;
  131. }
  132. int dy;
  133. for (dy = 1; dy < s; dy++)
  134. {
  135. int y = hy - dy;
  136. if (y >= H)
  137. {
  138. break;
  139. }
  140. if (home[y][hx] < t)
  141. {
  142. break;
  143. }
  144. }
  145. if (dy == s)
  146. {
  147. count++;
  148. }
  149. }
  150. }
  151. }
  152. System.out.println(count);
  153. result[s][t] = count + 1;
  154. }
  155.  
  156. } // end of main(String[])
  157.  
  158. }
  159.  
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