Comment detail

マップの通り抜け (Nested Flatten)
Squeak Smalltalk で。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
| map width height mapString connecteds newOneFound mapFilename |
mapFilename := 'map.txt'.
mapString := (FileStream fileNamed: mapFilename) contents.
width := (mapString indexOf: Character cr) - 1.
height := mapString size + 1 // (width + 1).
map := Matrix rows: height columns: width contents: (mapString copyWithout: Character cr).
connecteds := OrderedCollection new.
1 to: width do: [:cIdx |
   (map at: 1 at: cIdx) = $+ ifTrue: [map at: 1 at: cIdx put: $*. connecteds add: 1@cIdx]].
[  newOneFound := false.
   map indicesDo: [:rIdx :cIdx |
      ((map at: rIdx at: cIdx) = $+ and: [
            (connecteds includes: rIdx@cIdx) not and: [
               {-1@0. 0@-1. 1@0. 0@1} anySatisfy: [:delta |
                  connecteds includes: rIdx@cIdx + delta]]]) ifTrue: [
         connecteds add: rIdx@cIdx.
         newOneFound := true]].
   newOneFound] whileTrue.
^connecteds anySatisfy: [:pt | pt x = height]
#1399 で、

map at: 1 at: cIdx put: $*.

は、不要でした(^_^;)。

改めまして、$* でなぞる版も。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
| map width height mapString mapFilename queue |
mapFilename := 'map.txt'.
mapString := (FileStream fileNamed: mapFilename) contents.
width := (mapString indexOf: Character cr) - 1.
height := mapString size + 1 // (width + 1).
map := Matrix rows: height columns: width contents: (mapString copyWithout: Character cr).
queue := OrderedCollection new.
1 to: width do: [:y | (map at: 1 at: y) = $+ ifTrue: [queue add: 1@y]].
[queue notEmpty] whileTrue: [
   | position |
   position := queue removeFirst.
   map at: position x at: position y put: $*.
   {-1@0. 0@-1. 1@0. 0@1} do: [:delta |
      | next |
      next := position + delta.
      (map at: next x at: next y ifInvalid: [nil]) = $+ ifTrue: [
         map at: next x at: next y put: $*.
         queue add: next]]].
^(map atRow: height) includes: $*

Index

Feed

Other

Link

Pathtraq

loading...