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