fork(1) download
  1. /* paiza POH! vol.2
  2.  * result:
  3.  * http://p...content-available-to-author-only...a.jp/poh/paizen/result/379e3765ea0eda8af9abac1f57125bff
  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.  
  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. }
  36. else
  37. {
  38. count = 0;
  39. }
  40. home[y][tx] = count;
  41. }
  42. }
  43.  
  44. final int N = Integer.parseInt(in.readLine()); // ウィジェット数
  45.  
  46. for (int i = 0; i < N; i++)
  47. {
  48. String[] st = in.readLine().split(" ");
  49. int s = Integer.parseInt(st[0]); // ウィジェットの縦サイズ
  50. int t = Integer.parseInt(st[1]); // ウィジェットの横サイズ
  51.  
  52. if (s == 1 && t == 1)
  53. {
  54. System.out.println(spacecount);
  55. continue;
  56. }
  57.  
  58. int count = 0;
  59. for (int hy = 0; hy < H - s + 1; hy++)
  60. {
  61. for (int hx = 0; hx < W - t + 1; hx++)
  62. {
  63. if (home[hy][hx] < t)
  64. {
  65. hx += home[hy][hx];
  66. continue;
  67. }
  68. int dy;
  69. for (dy = 1; dy < s; dy++)
  70. {
  71. int y = hy + dy;
  72. if (y >= H)
  73. {
  74. break;
  75. }
  76. if (home[y][hx] < t)
  77. {
  78. break;
  79. }
  80. }
  81. if (dy == s)
  82. {
  83. count++;
  84. }
  85. }
  86. }
  87. System.out.println(count);
  88. }
  89.  
  90. } // end of main(String[])
  91.  
  92. }
  93.  
Success #stdin #stdout 0.07s 381248KB
stdin
5 5
00000
00100
00010
10001
10000
3
2 2
1 1
3 2
stdout
6
20
2