Submission #1987858


Source Code Expand

Copy
lines = $stdin.read
array = lines.split("\n")
$H,$W = array[0].split(" ").map(&:to_i)
$A    = array[1..$H+1].map{|s| s.split(" ").map(&:to_i)}
$MOD   = 10**9 + 7

$dy = [1,0,-1,0]
$dx = [0,1,0,-1]

$dp = Array.new($H).map{Array.new($W){-1}}

def dfs(y, x)
  if $dp[y][x] != -1
    $dp[y][x]
  else
    ans = 1
    for i in 0...4
      ny = y + $dy[i]
      nx = x + $dx[i]

      next if not (0 <= ny and ny < $H and 0 <= nx && nx < $W)

      if $A[y][x] < $A[ny][nx]
        ans += dfs(ny, nx)
        ans = ans % $MOD
      end
    end
    $dp[y][x] = ans
    ans
  end
end

ans = 0

for i in 0...$H
  for j in 0...$W
    #dp[i][j] = rec(dp,i,j,A,H,W)
    #ans += $dp[i][j]
    ans += dfs(i,j)
    ans = ans % $MOD
  end
end

puts ans

Submission Info

Submission Time
Task D - 経路
User hiroyuking
Language Ruby (2.3.3)
Score 0
Code Size 780 Byte
Status TLE
Exec Time 2110 ms
Memory 40060 KB

Judge Result

Set Name sample All
Score / Max Score 0 / 0 0 / 100
Status
AC × 2
AC × 15
TLE × 9
Set Name Test Cases
sample sample01.txt, sample02.txt
All 00.txt, 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, sample01.txt, sample02.txt, sample01.txt, sample02.txt
Case Name Status Exec Time Memory
00.txt TLE 2055 ms 32508 KB
01.txt TLE 2109 ms 37756 KB
02.txt TLE 2067 ms 23036 KB
03.txt AC 7 ms 1788 KB
04.txt AC 7 ms 1788 KB
05.txt AC 7 ms 1788 KB
06.txt AC 9 ms 1788 KB
07.txt AC 9 ms 1788 KB
08.txt AC 8 ms 1788 KB
09.txt AC 7 ms 1788 KB
10.txt AC 13 ms 1916 KB
11.txt TLE 2110 ms 37500 KB
12.txt TLE 2110 ms 37500 KB
13.txt TLE 2110 ms 37500 KB
14.txt TLE 2110 ms 39420 KB
15.txt AC 1991 ms 40060 KB
16.txt TLE 2110 ms 37500 KB
17.txt TLE 2110 ms 37500 KB
18.txt AC 1844 ms 23036 KB
19.txt AC 1612 ms 23036 KB
sample01.txt AC 7 ms 1788 KB
sample02.txt AC 7 ms 1788 KB