sicp学习 迷宫

很简单的问题,最笨的算法,自己第一个用scheme写的稍微算长点的程序.

废话不多说,直接上代码.

(begin
    (load "ex2.scm")
    (define (make-maze)
    (define (for-each proc things)
        (cond ((null? things) nil)
              (else    
                (let ((ret (proc (car things))))
                    (if (null? ret) (for-each proc (cdr things)) ret)))))
    ;迷宫
    (define maze  '((1 1 1 1 1 1 1 1 1)
                    (1 0 1 0 0 0 1 0 1)
                    (1 0 1 0 1 0 1 0 1)
                    (1 0 1 0 1 0 1 0 1)
                    (1 0 0 0 0 0 0 0 1)
                    (1 1 1 1 1 1 1 1 1)))
    (define direction '((0 -1)(0 1)(-1 0)(1 0))) ;上下左右
    (define (get-x-y array-2d x y)
        (list-ref (list-ref array-2d x) y))
    (define (is-close cur path);是否已经走过的路    
        (= 1 (accumulate 
                (lambda (pos sum) 
                (if (and (= (car pos) (car cur)) (= (cadr pos) (cadr cur))) (+ sum 1) sum)) 
             0 path)))
    ;检查是否合法路径
    (define (check cur dir path)
        (let ((x (+ (car dir) (car cur)))
              (y (+ (cadr dir) (cadr cur))))
         (cond ((is-close (list x y) path) nil)
               ((= (get-x-y maze x y) 1) nil);阻挡
               (else (list x y)))))   ;返回下一步合法的坐标    

    ;返回一条路径
    (define (find-path-one start target)
        (define (iter cur-step path)
            (define (move dir)
                (let ((next (check cur-step (list-ref direction dir) path)))
                    (cond ((null? next) nil)
                          (else (iter next (cons cur-step path))))))                          
            (if (and (= (car target) (car cur-step))
                     (= (cadr target) (cadr cur-step))) (cons cur-step path)
                (for-each move (enumerate-interval 0 3)))     
        )
        (reverse (iter start nil))
    )
    ;返回所有路径
    (define (find-path-all start target)    
        (define (iter cur-step path)
            (define (move dir)
                (let ((next (check cur-step (list-ref direction dir) path)))
                    (cond ((null? next) nil)
                          (else (iter next (cons cur-step path))))))                          
            (cond ((and (= (car target) (car cur-step)) (= (cadr target) (cadr cur-step))) 
                (list (cons cur-step path))) ;到达目的地,返回路劲
                  (else
                      (accumulate (lambda (dir p) (append (move dir) p)) nil (enumerate-interval 0 3))))
        )
        (map reverse (iter start nil))
    )
    (lambda (op start target)
        (cond ((eq? op 'find-path-all) (find-path-all start target))
              ((eq? op 'find-path-one) (find-path-one start target))
              (else "bad op"))
    ))    
)
;(define maze make-maze)
;(maze 'find-path-one '(1 1) '(1 7))

 

转载于:https://www.cnblogs.com/sniperHW/archive/2013/05/23/3095977.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值