Comment detail

マップの通り抜け (Nested Flatten)
とりあえず。
case e:Solvedのあとに

maze.map(x=>x.mkString("")).foreach(println)

を入れると経路が表示できます。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
def can_run_through(map:String):boolean = {
  val lines @ (head::tail) = map.trim().split("\n").toList
  val maze = lines.map(x => x.map(y=>y).toArray).toArray

  class Solved extends Throwable{}
  def get(x:int, y:int) = try {maze(y)(x)}catch{case e:Throwable => '.'}
  def walk(x:int, y:int):unit = {
      maze(y)(x) = '#'
      if(y == maze.length - 1){ throw new Solved }
      if(get(x, y+1)=='+'){walk(x,y+1)}
      if(get(x+1, y)=='+'){walk(x+1,y)}
      if(get(x-1, y)=='+'){walk(x-1,y)}
      if(get(x, y-1)=='+'){walk(x,y-1)}
  }
  
  try{
    (0 to head.length-1).foreach(x => if(head(x)=='+') walk(x,0))
    false
  }catch{ 
    case e:Solved => true
  }
} 
val map = """
.+.....
.+.+++.
.+.+.+.
.+++.+.
.....+.
"""

val map2 = """
..+...+
++.+++.
.+...++
++++.+.
.+..+.+
"""

val map3 = """
++++++++++++++++
++++++++++++++++
++++++++++++++++
++++++++++++++++
++++++++++++++++
++++++++++++++++
++++++++++++++++
++++++++++++++++
++++++++++++++++
++++++++++++++++
................
"""

println(can_run_through(map))

Index

Feed

Other

Link

Pathtraq

loading...