文字列の八方向検索
Posted feedbacks - Scheme
Schemeならまだ一番のり! 1:user> (uori) => ((2 0 "左") (0 1 #0="右") (0 1 "下") (3 1 #0#) (4 3 "左上")) 2:user> (write (uori)) ((2 0 "左") (0 1 "右") (0 1 "下") (3 1 "右") (4 3 "左上"))=> #<undef>
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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | (use gauche.array)
(use gauche.test)
(define (array-ref* array row col)
(guard (exc (<error> #f))
(array-ref array row col)))
(define (make-string-from-matrix matrix length x y delta-x delta-y)
(let loop ((i length) (x x) (y y) (chars '()))
(cond ((<= i 0) (apply string (reverse chars)))
((array-ref* matrix y x) =>
(lambda (ch)
(loop (- i 1) (delta-x x) (delta-y y) (cons ch chars))))
(else #f))))
(define (search8 str1 matrix col row)
(define len (string-length str1))
(define (inc x) (+ x 1))
(define (dec x) (- x 1))
(define (string<-matrix delta-x delta-y)
(make-string-from-matrix matrix len col row delta-x delta-y))
(define (make-search-proc value make-string-proc)
(and-let* ((str2 (make-string-proc))
((string=? str1 str2)))
value))
(define (search-right)
(make-search-proc "右" (cut string<-matrix inc identity)))
(define (search-left)
(make-search-proc "左" (cut string<-matrix dec identity)))
(define (search-upper)
(make-search-proc "上" (cut string<-matrix identity dec)))
(define (search-lower)
(make-search-proc "下" (cut string<-matrix identity inc)))
(define (search-upper-right)
(make-search-proc "右上" (cut string<-matrix inc dec)))
(define (search-upper-left)
(make-search-proc "左上" (cut string<-matrix dec dec)))
(define (search-lower-right)
(make-search-proc "右下" (cut string<-matrix inc inc)))
(define (search-lower-left)
(make-search-proc "左下" (cut string<-matrix dec inc)))
(filter-map (lambda (proc) (proc))
(list search-right
search-left
search-upper
search-lower
search-upper-right
search-upper-left
search-lower-right
search-lower-left)))
(define input-string
(string-append
"リオウウリウ"
"ウオリウオリ"
"オリリオリウ"
"リリオオウオ"))
(define input
(apply array (shape 0 4 0 6) (string->list input-string)))
(define (uori)
(let1 r '()
(dotimes (y 4)
(dotimes (x 6)
(cond ((search8 "ウオリ" input x y) =>
(cut for-each (lambda (dir) (push! r (list x y dir))) <>))
(else #f))))
(reverse r)))
|


kuromin #4400() Rating0/2=0.00
[ reply ]