R, C, T = gets.split.map &:to_i Sy, Sx = gets.split.map &:to_i Gy, Gx = gets.split.map &:to_i Board = $<.map{|s|s.chomp.chars} dirs = R.times.map{|y| C.times.map{|x| [1,0,-1,0,1].each_cons(2).select{|dy,dx| Board.fetch(y+dy,[])[x+dx] != ?# } } } dp = R.times.map{[0.0]*C} dp[Sy][Sx] = 1.0 [T, 5000+T%2].min.times{ next_dp = R.times.map{ [0.0]*C } R.times{|y| C.times{|x| next if Board[y][x] == ?# d = dirs[y][x] if d.empty? next_dp[y][x] += dp[y][x] next end d.each{|(dy, dx)| next_dp[y+dy][x+dx] += dp[y][x] / d.size } } } dp = next_dp } p dp[Gy][Gx].to_f