結果

問題 No.971 いたずらっ子
ユーザー sansaquasansaqua
提出日時 2020-01-17 21:42:18
言語 Common Lisp
(sbcl 2.3.8)
結果
WA  
実行時間 -
コード長 10,681 bytes
コンパイル時間 1,406 ms
コンパイル使用メモリ 56,336 KB
実行使用メモリ 103,896 KB
最終ジャッジ日時 2024-06-25 19:12:46
合計ジャッジ時間 8,923 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 AC 668 ms
92,672 KB
testcase_04 AC 626 ms
91,520 KB
testcase_05 AC 651 ms
92,032 KB
testcase_06 AC 501 ms
88,576 KB
testcase_07 WA -
testcase_08 AC 179 ms
60,544 KB
testcase_09 AC 171 ms
58,752 KB
testcase_10 AC 16 ms
24,704 KB
testcase_11 AC 11 ms
23,424 KB
testcase_12 AC 11 ms
23,424 KB
testcase_13 AC 11 ms
23,552 KB
testcase_14 WA -
testcase_15 AC 10 ms
23,680 KB
testcase_16 AC 11 ms
23,552 KB
testcase_17 AC 10 ms
23,296 KB
testcase_18 AC 11 ms
23,424 KB
testcase_19 AC 10 ms
23,296 KB
testcase_20 AC 10 ms
23,424 KB
testcase_21 AC 11 ms
23,552 KB
testcase_22 AC 10 ms
23,296 KB
testcase_23 AC 11 ms
23,296 KB
testcase_24 AC 11 ms
23,424 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
; compiling file "/home/judge/data/code/Main.lisp" (written 25 JUN 2024 07:12:36 PM):

; file: /home/judge/data/code/Main.lisp
; in: DEFINE-BINARY-HEAP HEAP
;     (DEFINE-BINARY-HEAP HEAP :ORDER
;                         (LAMBDA (V1 V2)
;                           (< (THE UINT62 (CAR V1)) (THE UINT62 (CAR V2))))
;                         :ELEMENT-TYPE LIST)
; --> DEFSTRUCT PROGN SB-C:XDEFUN PROGN SB-IMPL::%DEFUN SB-IMPL::%DEFUN 
; --> SB-INT:NAMED-LAMBDA FUNCTION 
; ==>
;   (MAKE-ARRAY (1+ SIZE) :ELEMENT-TYPE 'LIST)
; 
; caught STYLE-WARNING:
;   The default initial element 0 is not a LIST.
;   See also:
;     The ANSI Standard, Function MAKE-ARRAY
;     The ANSI Standard, Function UPGRADED-ARRAY-ELEMENT-TYPE

; --> DEFUN PROGN SB-IMPL::%DEFUN SB-IMPL::%DEFUN SB-INT:NAMED-LAMBDA FUNCTION 
; --> BLOCK SYMBOL-MACROLET WHEN IF SETF LET SB-KERNEL:THE* ADJUST-ARRAY * 
; ==>
;   1
; 
; note: doing unsigned word to integer coercion (cost 20), for:
;       the first result of inline ASH
; 
; compilation unit finished
;   caught 1 STYLE-WARNING condition
;   printed 1 note


; wrote /home/judge/data/code/Main.fasl
; compilation finished in 0:00:00.128

ソースコード

diff #

;; -*- coding: utf-8 -*-
(eval-when (:compile-toplevel :load-toplevel :execute)
  (sb-int:defconstant-eqx OPT
    #+swank '(optimize (speed 3) (safety 2))
    #-swank '(optimize (speed 3) (safety 0) (debug 0))
    #'equal)
  #+swank (ql:quickload '(:cl-debug-print :fiveam) :silent t)
  #-swank (set-dispatch-macro-character
           ;; enclose the form with VALUES to avoid being captured by LOOP macro
           #\# #\> (lambda (s c p) (declare (ignore c p)) `(values ,(read s nil nil t)))))
#+swank (cl-syntax:use-syntax cl-debug-print:debug-print-syntax)
#-swank (disable-debugger) ; for CS Academy

;; BEGIN_INSERTED_CONTENTS
;;;
;;; Binary heap
;;;

(define-condition heap-empty-error (error)
  ((heap :initarg :heap :reader heap-empty-error-heap))
  (:report
   (lambda (condition stream)
     (format stream "Attempted to pop empty heap ~W" (heap-empty-error-heap condition)))))

(defmacro define-binary-heap (name &key (order '#'>) (element-type 'fixnum))
  "Defines the binary heap specialized for the given order and the element
type. This macro defines a structure of the name NAME and relevant functions:
MAKE-<NAME>, <NAME>-PUSH, <NAME>-POP, <NAME>-REINITIALIZE, <NAME>-EMPTY-P,
<NAME>-COUNT, and <NAME>-PEEK."
  (check-type name symbol)
  (let* ((string-name (string name))
         (fname-push (intern (format nil "~A-PUSH" string-name)))
         (fname-pop (intern (format nil "~A-POP" string-name)))
         (fname-reinitialize (intern (format nil "~A-REINITIALIZE" string-name)))
         (fname-empty-p (intern (format nil "~A-EMPTY-P" string-name)))
         (fname-count (intern (format nil "~A-COUNT" string-name)))
         (fname-peek (intern (format nil "~A-PEEK" string-name)))
         (fname-make (intern (format nil "MAKE-~A" string-name)))
         (acc-position (intern (format nil "~A-POSITION" string-name)))
         (acc-data (intern (format nil "~A-DATA" string-name))))
    `(progn
       (defstruct (,name
                   (:constructor ,fname-make
                       (size
                        &aux (data ,(if (eql element-type '*)
                                        `(make-array (1+ size))
                                        `(make-array (1+ size) :element-type ',element-type))))))
         (data #() :type (simple-array ,element-type (*)))
         (position 1 :type (integer 1 #.most-positive-fixnum)))

       (declaim #+sbcl (sb-ext:maybe-inline ,fname-push))
       (defun ,fname-push (obj heap)
         "Adds OBJ to the end of HEAP."
         (declare (optimize (speed 3))
                  (type ,name heap))
         (symbol-macrolet ((position (,acc-position heap)))
           (when (>= position (length (,acc-data heap)))
             (setf (,acc-data heap)
                   (adjust-array (,acc-data heap) (* position 2))))
           (let ((data (,acc-data heap)))
             (declare ((simple-array ,element-type (*)) data))
             (labels ((update (pos)
                        (declare (optimize (speed 3) (safety 0)))
                        (unless (= pos 1)
                          (let ((parent-pos (ash pos -1)))
                            (when (funcall ,order (aref data pos) (aref data parent-pos))
                              (rotatef (aref data pos) (aref data parent-pos))
                              (update parent-pos))))))
               (setf (aref data position) obj)
               (update position)
               (incf position)
               heap))))

       (declaim #+sbcl (sb-ext:maybe-inline ,fname-pop))
       (defun ,fname-pop (heap)
         "Removes and returns the element at the top of HEAP."
         (declare (optimize (speed 3))
                  (type ,name heap))
         (symbol-macrolet ((position (,acc-position heap)))
           (let ((data (,acc-data heap)))
             (declare ((simple-array ,element-type (*)) data))
             (labels ((update (pos)
                        (declare (optimize (speed 3) (safety 0))
                                 ((integer 1 #.most-positive-fixnum) pos))
                        (let* ((child-pos1 (+ pos pos))
                               (child-pos2 (1+ child-pos1)))
                          (when (<= child-pos1 position)
                            (if (<= child-pos2 position)
                                (if (funcall ,order (aref data child-pos1) (aref data child-pos2))
                                    (unless (funcall ,order (aref data pos) (aref data child-pos1))
                                      (rotatef (aref data pos) (aref data child-pos1))
                                      (update child-pos1))
                                    (unless (funcall ,order (aref data pos) (aref data child-pos2))
                                      (rotatef (aref data pos) (aref data child-pos2))
                                      (update child-pos2)))
                                (unless (funcall ,order (aref data pos) (aref data child-pos1))
                                  (rotatef (aref data pos) (aref data child-pos1))))))))
               (when (= position 1)
                 (error 'heap-empty-error :heap heap))
               (prog1 (aref data 1)
                 (decf position)
                 (setf (aref data 1) (aref data position))
                 (update 1))))))

       (declaim (inline ,fname-reinitialize))
       (defun ,fname-reinitialize (heap)
         "Makes HEAP empty."
         (setf (,acc-position heap) 1)
         heap)

       (declaim (inline ,fname-empty-p))
       (defun ,fname-empty-p (heap)
         "Returns true iff HEAP is empty."
         (= 1 (,acc-position heap)))

       (declaim (inline ,fname-count))
       (defun ,fname-count (heap)
         "Returns the current number of the elements in HEAP."
         (- (,acc-position heap) 1))

       (declaim (inline ,fname-peek))
       (defun ,fname-peek (heap)
         "Returns the topmost element of HEAP."
         (if (= 1 (,acc-position heap))
             (error 'heap-empty-error :heap heap)
             (aref (,acc-data heap) 1))))))

(declaim (inline read-schar))
(defun read-schar (&optional (stream *standard-input*))
  (declare #-swank (sb-kernel:ansi-stream stream)
           (inline read-byte))
  #+swank (read-char stream nil #\Newline) ; on SLIME
  #-swank (code-char (read-byte stream nil #.(char-code #\Newline))))

(defmacro dbg (&rest forms)
  #+swank
  (if (= (length forms) 1)
      `(format *error-output* "~A => ~A~%" ',(car forms) ,(car forms))
      `(format *error-output* "~A => ~A~%" ',forms `(,,@forms)))
  #-swank (declare (ignore forms)))

(defmacro define-int-types (&rest bits)
  `(progn
     ,@(mapcar (lambda (b) `(deftype ,(intern (format nil "UINT~A" b)) () '(unsigned-byte ,b))) bits)
     ,@(mapcar (lambda (b) `(deftype ,(intern (format nil "INT~A" b)) () '(signed-byte ,b))) bits)))
(define-int-types 2 4 7 8 15 16 31 32 62 63 64)

(declaim (inline println))
(defun println (obj &optional (stream *standard-output*))
  (let ((*read-default-float-format* 'double-float))
    (prog1 (princ obj stream) (terpri stream))))

(defconstant +mod+ 1000000007)

;;;
;;; Body
;;;


;; dist y . x
(define-binary-heap heap
  :order (lambda (v1 v2)
           (< (the uint62 (car v1))
              (the uint62 (car v2))))
  :element-type list)

(defconstant +inf+ #xffffffff)
(defun main ()
  (let* ((h (read))
         (w (read))
         (as (make-array (list h w) :element-type 'bit))
         (que (make-heap (* h w)))
         (dists (make-array (list h w) :element-type 'uint32 :initial-element +inf+)))
    (declare (uint31 h w))
    (dotimes (i h)
      (dotimes (j w (read-schar))
        (when (char= #\k (read-schar))
          (setf (aref as i j) 1))))
    (heap-push (list* 0 0 0) que)
    (setf (aref dists 0 0) 0)
    (labels ((visit (y x current-dist)
               (declare (int32 y x current-dist))
               (when (and (<= 0 y (- h 1))
                          (<= 0 x (- w 1)))
                 (let ((new-dist (if (zerop (aref as y x))
                                     (+ 1 current-dist)
                                     (+ 1 current-dist y x))))
                   (when (< new-dist (aref dists y x))
                     (setf (aref dists y x) new-dist)
                     (heap-push (list* new-dist y x) que))))))
      #>as
      (loop until (heap-empty-p que)
            for (dist y . x) = (heap-pop que)
            when (= dist (aref dists y x))
            do (visit (- y 1) x dist)
               (visit (+ y 1) x dist)
               (visit y (- x 1) dist)
               (visit y (+ x 1) dist))
      #>dists
      (println (aref dists (- h 1) (- w 1))))))

#-swank (main)

;;;
;;; Test and benchmark
;;;

#+swank
(defun io-equal (in-string out-string &key (function #'main) (test #'equal))
  "Passes IN-STRING to *STANDARD-INPUT*, executes FUNCTION, and returns true if
the string output to *STANDARD-OUTPUT* is equal to OUT-STRING."
  (labels ((ensure-last-lf (s)
             (if (eql (uiop:last-char s) #\Linefeed)
                 s
                 (uiop:strcat s uiop:+lf+))))
    (funcall test
             (ensure-last-lf out-string)
             (with-output-to-string (out)
               (let ((*standard-output* out))
                 (with-input-from-string (*standard-input* (ensure-last-lf in-string))
                   (funcall function)))))))

#+swank
(defun get-clipbrd ()
  (with-output-to-string (out)
    ;; (run-program "C:/Windows/System32/WindowsPowerShell/v1.0/powershell.exe" '("get-clipboard") :output out)
    (run-program "powershell.exe" '("-Command" "Get-Clipboard") :output out :search t)))

#+swank (defparameter *this-pathname* (uiop:current-lisp-file-pathname))
#+swank (defparameter *dat-pathname* (uiop:merge-pathnames* "test.dat" *this-pathname*))

#+swank
(defun run (&optional thing (out *standard-output*))
  "THING := null | string | symbol | pathname

null: run #'MAIN using the text on clipboard as input.
string: run #'MAIN using the string as input.
symbol: alias of FIVEAM:RUN!.
pathname: run #'MAIN using the text file as input."
  (let ((*standard-output* out))
    (etypecase thing
      (null
       (with-input-from-string (*standard-input* (delete #\Return (get-clipbrd)))
         (main)))
      (string
       (with-input-from-string (*standard-input* (delete #\Return thing))
         (main)))
      (symbol (5am:run! thing))
      (pathname
       (with-open-file (*standard-input* thing)
         (main))))))

#+swank
(defun gen-dat ()
  (uiop:with-output-file (out *dat-pathname* :if-exists :supersede)
    (format out "")))

#+swank
(defun bench (&optional (out (make-broadcast-stream)))
  (time (run *dat-pathname* out)))
0