fork download
  1. <?php
  2. // H, W取得
  3. list($height, $width) = explode(' ', trim(fgets(STDIN)));
  4.  
  5. // D取得
  6. for ($h = 0; $h < $height; $h++) {
  7. $home[] = trim(fgets(STDIN));
  8. }
  9.  
  10. // N取得
  11. $cnt = trim(fgets(STDIN));
  12.  
  13. // 指定座標を左上として入る大きさを探索する
  14. function search($h, $w) {
  15. global $home, $available, $height, $width;
  16. $available[1][1]++;
  17. $t_max = $width - $w;
  18. for ($s = 1; $s <= $height - $h; $s++) {
  19. for ($t = (1 === $s) ? 2 : 1; $t <= $t_max; $t++) {
  20. if ('0' === substr($home[$h + $s - 1], $w + $t - 1, 1)) {
  21. $available[$s][$t]++;
  22. } else if (1 === $t) {
  23. return;
  24. } else {
  25. $t_max = $t - 1;
  26. break;
  27. }
  28. }
  29. }
  30. }
  31.  
  32. // 探索用データ初期化
  33. $available = array();
  34. for ($s = 1; $s <= $height; $s++) {
  35. for ($t = 1; $t <= $width; $t++) {
  36. $available[$s][$t] = 0;
  37. }
  38. }
  39.  
  40. // 検索用データ生成
  41. for ($h = 0; $h < $height; $h++) {
  42. for ($w = 0; $w < $width; $w++) {
  43. if (substr($home[$h], $w, 1) === '0') {
  44. search($h, $w);
  45. }
  46. }
  47. }
  48.  
  49. // S, T取得
  50. $output = '';
  51. for ($n = 0; $n < $cnt; $n++) {
  52. list($h, $w) = explode(' ', trim(fgets(STDIN)));
  53.  
  54. if ($h > $height or $w > $width) {
  55. $output .= "0\n";
  56. } else {
  57. $output .= $available[$h][$w]."\n";
  58. }
  59. }
  60. echo $output;
Success #stdin #stdout 0.01s 20520KB
stdin
5 5
00000
00100
00010
10001
10000
3
2 2
1 1
3 2
stdout
6
20
2