Comment detail

マップの通り抜け (Nested Flatten)
経路を保存しとかないでいいので、通常の迷路探索よりも楽。
 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
(use srfi-1)
(use srfi-42)
(use file.util)
(use gauche.sequence)
(use gauche.array)

(define (read-map file)
  (let1 rows (file->string-list file)
    (apply array (shape 0 (size-of rows) 0 (size-of (car rows)))
           (concatenate (map string->list rows)))))

(define (map-width m)  (array-length m 1))
(define (map-height m) (array-length m 0))

(define (search m x y)
  (define (try x y)
    (and (< -1 x (map-width m))
         (< -1 y (map-height m))
         (eqv? (array-ref m y x) #\+)
         (or (= y (- (map-height m) 1))
             (search m x y))))
  (array-set! m y x #\*)
  (or (try (- x 1) y) (try (+ x 1) y) (try x (+ y 1)) (try x (- y 1))))

(define (connected? m)
  (any?-ec (: x 0 (map-width m))
           (and (eqv? (array-ref m 0 x) #\+) (search m x 0))))

(define (main args)
  (print (connected? (read-map (cadr args)))))

Index

Feed

Other

Link

Pathtraq

loading...