Submission #750666
Source Code Expand
# 下記の改変
# http://abc037.contest.atcoder.jp/submissions/750568
class Keiro
MOD = 100_000_007
def initialize(h, w, grid)
@h = h
@h2 = h + 2
@w = w
@w2 = w + 2
# @gridと@combは1次元
@grid = grid
@comb = Array.new(@h2 * @w2, nil)
# @nexts = [-1, 1, -@w2, @w2]
end
def dfs(ij)
# 動かない場合もあるのでres=1
res = 1
# 何度も呼ぶのは1度だけにする
gij = @grid[ij]
# 既に探索した木ならその値を返す
#return @comb[i][j] if @comb[i][j]>0
return @comb[ij] if @comb[ij]>0
# 上下左右のほうが大きければ移動する
# 上下左右への計算も工夫
# @nexts.each do |x|
# x += ij
# res += @comb[x] || dfs(x) if gij < @grid[x]
# end
# res += @comb[ij - 1] || dfs(ij - 1) if gij < @grid[ij - 1]
# res += @comb[ij + 1] || dfs(ij + 1) if gij < @grid[ij + 1]
# res += @comb[ij - @w2] || dfs(ij - @w2) if gij < @grid[ij - @w2]
# res += @comb[ij + @w2] || dfs(ij + @w2) if gij < @grid[ij + @w2]
x = ij - 1
res += @comb[x] || dfs(x) if gij < @grid[x]
x = ij + 1
res += @comb[x] || dfs(x) if gij < @grid[x]
x = ij - @w2
res += @comb[x] || dfs(x) if gij < @grid[x]
x = ij + @w2
res += @comb[x] || dfs(x) if gij < @grid[x]
# 移動経路数を2次元配列に格納し、値を返す
# Array#[]で呼び直さない
# res %= MOD
@comb[ij] = res
res
end
def dfs_each
ij = @w2 + 1
@h.times do
@w.times do
yield (@comb[ij] || dfs(ij))
ij += 1
end
ij += 2
end
end
end
# 高さ、幅の読み取り
h, w = gets.chomp.split.map(&:to_i)
# マス目を格納する2次元配列及び、経路数を格納する2次元配列を用意
grid = []
# 最初
grid.concat(Array.new(w + 2, 0))
# マス目のよみとり
h.times do
grid << 0
grid.concat(gets.split.map(&:to_i))
grid << 0
end
# 最後
grid.concat(Array.new(w + 2, 0))
keiro = Keiro.new(h, w, grid)
ans = 0
keiro.dfs_each do |dfs|
ans += dfs
# MODより大きければ引く
# ans %= Keiro::MOD
# ans -= Keiro::MOD if ans > Keiro::MOD
end
puts(ans % Keiro::MOD)
Submission Info
| Submission Time | |
|---|---|
| Task | D - 経路 |
| User | iwamoto |
| Language | Ruby (2.3.3) |
| Score | 0 |
| Code Size | 2286 Byte |
| Status | RE |
| Exec Time | 908 ms |
| Memory | 19196 KiB |
Judge Result
| Set Name | sample | All | ||||
|---|---|---|---|---|---|---|
| Score / Max Score | 0 / 0 | 0 / 100 | ||||
| Status |
|
|
| 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 |
| Case Name | Status | Exec Time | Memory |
|---|---|---|---|
| 00.txt | RE | 792 ms | 18684 KiB |
| 01.txt | RE | 835 ms | 18684 KiB |
| 02.txt | RE | 737 ms | 18172 KiB |
| 03.txt | RE | 18 ms | 1788 KiB |
| 04.txt | RE | 18 ms | 1788 KiB |
| 05.txt | RE | 17 ms | 1788 KiB |
| 06.txt | RE | 19 ms | 1916 KiB |
| 07.txt | RE | 19 ms | 1916 KiB |
| 08.txt | RE | 18 ms | 1788 KiB |
| 09.txt | RE | 18 ms | 1788 KiB |
| 10.txt | RE | 20 ms | 1916 KiB |
| 11.txt | RE | 902 ms | 19196 KiB |
| 12.txt | RE | 906 ms | 19196 KiB |
| 13.txt | RE | 903 ms | 19196 KiB |
| 14.txt | RE | 904 ms | 19196 KiB |
| 15.txt | RE | 869 ms | 19068 KiB |
| 16.txt | RE | 908 ms | 19196 KiB |
| 17.txt | RE | 906 ms | 19196 KiB |
| 18.txt | RE | 739 ms | 18172 KiB |
| 19.txt | RE | 739 ms | 18172 KiB |
| sample01.txt | RE | 18 ms | 1788 KiB |
| sample02.txt | RE | 18 ms | 1788 KiB |