fork(1) download
  1. /* paiza POH! vol.2
  2.  * result:
  3.  * http://p...content-available-to-author-only...a.jp/poh/paizen/result/8eb31c6ace2a72a6ee8b279bb49c470f
  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[H][W];
  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 (result[s][t] > 0)
  64. {
  65. System.out.println(result[s][t] - 1);
  66. continue;
  67. }
  68.  
  69. if (s == 1 && t == 1)
  70. {
  71. System.out.println(spacecount);
  72. continue;
  73. }
  74.  
  75. int count = 0;
  76. if (W > H)
  77. {
  78. for (int hy = H - 1; hy >= s - 1; hy--)
  79. {
  80. for (int hx = 0; hx < W - t + 1; hx++)
  81. {
  82. if (home[hy][hx] < t)
  83. {
  84. hx += home[hy][hx];
  85. continue;
  86. }
  87. if (hoge[hy][hx] < s)
  88. {
  89. continue;
  90. }
  91. int dy;
  92. for (dy = 1; dy < s; dy++)
  93. {
  94. int y = hy - dy;
  95. if (y >= H)
  96. {
  97. break;
  98. }
  99. if (home[y][hx] < t)
  100. {
  101. break;
  102. }
  103. }
  104. if (dy == s)
  105. {
  106. count++;
  107. }
  108. }
  109. }
  110. }
  111. else
  112. {
  113. for (int hx = 0; hx < W - t + 1; hx++)
  114. {
  115. for (int hy = H - 1; hy >= s - 1; hy--)
  116. {
  117. if (home[hy][hx] < t)
  118. {
  119. continue;
  120. }
  121. if (hoge[hy][hx] < s)
  122. {
  123. hy -= hoge[hy][hx];
  124. continue;
  125. }
  126. int dy;
  127. for (dy = 1; dy < s; dy++)
  128. {
  129. int y = hy - dy;
  130. if (y >= H)
  131. {
  132. break;
  133. }
  134. if (home[y][hx] < t)
  135. {
  136. break;
  137. }
  138. }
  139. if (dy == s)
  140. {
  141. count++;
  142. }
  143. }
  144. }
  145. }
  146. System.out.println(count);
  147. result[s][t] = count + 1;
  148. }
  149.  
  150. } // end of main(String[])
  151.  
  152. }
  153.  
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