fork(1) download
  1. /* paiza POH! vol.2
  2.  * result:
  3.  * http://p...content-available-to-author-only...a.jp/poh/paizen/result/a696b103c76284d247e6b48cb5285069
  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. int H = Integer.parseInt(hw[0]); // ホーム画面縦の区画数
  17. int W = Integer.parseInt(hw[1]); // ホーム画面横の区画数
  18.  
  19. int[][] home = new int[H][W];
  20. int[][] result = new int[301][301];
  21. int count = 0;
  22.  
  23. for (int y = 0; y < H; y++)
  24. {
  25. String line = in.readLine();
  26. for (int x = 0; x < W; x++)
  27. {
  28. if ((home[y][x] = (int)(line.charAt(x) - '0')) == 0)
  29. {
  30. count++;
  31. }
  32. }
  33. }
  34. result[1][1] = count;
  35.  
  36. int[][][] table = new int[301][][];
  37. int right;
  38.  
  39. int ww = W, hh;
  40. int[][] temp1, temp2;
  41. temp1 = table[1] = home;
  42. for (right = 2; right <= W; right++)
  43. {
  44. count = 0;
  45. ww--;
  46. temp2 = table[right] = new int[H][ww];
  47. for (int y = 0; y < H; y++)
  48. {
  49. for (int x = 0; x < ww; x++)
  50. {
  51. if ((temp2[y][x] = temp1[y][x] + temp1[y][x + 1]) == 0)
  52. {
  53. count++;
  54. }
  55. }
  56. }
  57. result[1][right] = count;
  58. if (count < 2)
  59. {
  60. break;
  61. }
  62. temp1 = temp2;
  63. }
  64.  
  65. ww = W + 1;
  66. for (int i = 1; i < right; i++)
  67. {
  68. ww--;
  69. temp1 = table[i];
  70. hh = H;
  71. for (int bottom = 2; bottom <= H; bottom++)
  72. {
  73. count = 0;
  74. hh--;
  75. for (int y = 0; y < hh; y++)
  76. {
  77. for (int x = 0; x < ww; x++)
  78. {
  79. if ((temp1[y][x] += temp1[y + 1][x]) == 0)
  80. {
  81. count++;
  82. }
  83. }
  84. }
  85. result[bottom][i] = count;
  86. if (count < 2)
  87. {
  88. break;
  89. }
  90. }
  91. }
  92.  
  93. int N = Integer.parseInt(in.readLine()); // ウィジェット数
  94.  
  95. for (int i = 0; i < N; i++)
  96. {
  97. String[] st = in.readLine().split(" ");
  98. int s = Integer.parseInt(st[0]); // ウィジェットの縦サイズ
  99. int t = Integer.parseInt(st[1]); // ウィジェットの横サイズ
  100.  
  101. System.out.println(result[s][t]);
  102. }
  103.  
  104. } // end of main(String[])
  105.  
  106.  
  107. static void print2DArray(int[][] array)
  108. {
  109. print2DArray(array, array.length, array[0].length);
  110. }
  111.  
  112. static void print2DArray(int[][] array, int h, int w)
  113. {
  114. System.out.println("Array --------- ");
  115. for (int i = 0; i < h; i++)
  116. {
  117. for (int j = 0; j < w; j++)
  118. {
  119. System.out.printf("%3d ", array[i][j]);
  120. }
  121. System.out.println();
  122. }
  123. System.out.println("--------------- ");
  124. }
  125. }
  126.  
Success #stdin #stdout 0.07s 380224KB
stdin
5 5
00000
00100
00010
10001
10000
7
2 2
1 1
3 2
3 2
2 3
3 1
1 3
stdout
6
20
2
2
1
6
7