fork(1) download
  1. /* paiza POH! vol.2
  2.  * result:
  3.  * http://p...content-available-to-author-only...a.jp/poh/paizen/result/79416a22f9f6ab7b5ed2248a4d7b54e0
  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. for (int hy = H - 1; hy >= s - 1; hy--)
  70. {
  71. for (int hx = 0; hx < W - t + 1; hx++)
  72. {
  73. if (home[hy][hx] < t)
  74. {
  75. hx += home[hy][hx];
  76. continue;
  77. }
  78. if (hoge[hy][hx] < s)
  79. {
  80. continue;
  81. }
  82. int dy;
  83. for (dy = 1; dy < s; dy++)
  84. {
  85. int y = hy - dy;
  86. if (y >= H)
  87. {
  88. break;
  89. }
  90. if (home[y][hx] < t)
  91. {
  92. break;
  93. }
  94. }
  95. if (dy == s)
  96. {
  97. count++;
  98. }
  99. }
  100. }
  101. System.out.println(count);
  102. }
  103.  
  104. } // end of main(String[])
  105.  
  106. }
  107.  
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