12 lines scala bfs recursion
\nobject Solution {\n  import collection.immutable.{TreeMap,TreeSet}\n  def minimumTime(grid: Array[Array[Int]]): Int = {\n    def f(q:Set[(Int,(Int,Int))], v:M
```\nobject Solution {\n  import collection.immutable.{TreeMap,TreeSet}\n  def minimumTime(grid: Array[Array[Int]]): Int = {\n    def f(q:Set[(Int,(Int,Int))], v:Map[(Int,Int),Int]): Int = {\n      lazy val ((t0:Int,(x0:Int,y0:Int)),t) = (q.head, q.tail)\n      lazy val aa = List((x0-1)->y0, x0->(y0+1), x0->(y0-1), (x0+1)->y0)\n        .collect{case (x:Int,y:Int) if x>=0 && y>=0 && x < grid.size && y < grid.head.size =>\n          lazy val tm = grid(x)(y)\n          lazy val inc = 1 - (tm-t0)%2\n          ((t0+1) max (tm+inc)) -> (x,y)                    \n        }.filterNot{case (tm,xy) => v.contains(xy) && v(xy) <= tm }\n      if(q.isEmpty) v((grid.size-1),(grid.head.size-1))\n      else f(t ++ aa, v ++ (aa.map{case (tm,xy) => xy -> tm }))\n    }\n    if((grid(1)(0) min grid(0)(1)) > 1 ) -1 else f(TreeSet(grid(0)(0) -> (0,0)), Map((0,0) -> grid(0)(0)))\n  }\n}\n\n```
['Scala']