(defparameter +dx+ (make-array 4 :element-type 'integer :initial-contents '(1 0 -1 0))) (defparameter +dy+ (make-array 4 :element-type 'integer :initial-contents '(0 -1 0 1))) (defvar *que* (make-array 1010101)) (defvar *que-front* 0) (defvar *que-back* 0) (defun main (&rest argv) (declare (ignorable argv)) (let* ((n (read)) (taro-hp (read)) (start-x (1- (read))) (start-y (1- (read))) (goal-x (1- (read))) (goal-y (1- (read))) (desert-levels (make-array (list n n) :element-type 'integer)) (dp1 (make-array (list n n) :element-type 'integer :initial-element 0)) (dp2 (make-array (list n n) :element-type 'integer :initial-element 0))) (dotimes (i n) (dotimes (j n) (setf (aref desert-levels i j) (read)))) (setf (aref dp1 start-y start-x) taro-hp) (setf (aref *que* *que-front*) (cons start-x start-y)) (incf *que-front*) (loop while (> *que-front* *que-back*) do (let* ((now (aref *que* *que-back*)) (x (car now)) (y (cdr now))) (incf *que-back*) (dotimes (k 4) (let ((xx (+ x (aref +dx+ k))) (yy (+ y (aref +dy+ k)))) (unless (or (or (< xx 0) (<= n xx) (< yy 0) (<= n yy)) (>= (aref dp1 yy xx) (- (aref dp1 y x) (aref desert-levels yy xx)))) (setf (aref dp1 yy xx) (- (aref dp1 y x) (aref desert-levels yy xx)) (aref dp2 yy xx) (1+ (aref dp2 y x)) (aref *que* *que-front*) (cons xx yy)) (incf *que-front*) (when (and (= xx goal-x) (= yy goal-y)) (format t "~d~%" (aref dp2 yy xx)) (return-from main 0))))))) (format t "-1~%")) 0) (main)