Let's write β

趣味で書いたこととか、RustとLispが好き

JAG2007ProblemD

Square Route

(defun read-town (n m)
  (list
   (loop for i from 1 upto n
      collect (read))
   (loop for i from 1 upto m
      collect (read))))

(defun collect-cdr (list)
  (loop for lst = list then (cdr lst)
     until (null lst)
       collect lst))
(defun count-square (h-list w-list)
  (let ((h-cdr-list (collect-cdr h-list))
	(w-cdr-list (collect-cdr w-list)))
    (loop for w-list in w-cdr-list
       sum
	 (loop
	    for h-list in h-cdr-list
	    sum
	      (loop
		 for h in h-list
		 sum h into sumH
		 sum
		   (loop for w in w-list
		      sum w into sumW
			until (> sumW sumH)
		      count (= sumH sumW)))))))
(defun main ()
  (loop for n = (read)
        for m = (read)
     until (and (zerop m) (zerop n))
     do (format t "~A~%" (apply #'count-square (read-town n m)))))      count (= sumH sumW)))))))

Loopでちゃんと書いてやって可能な辺の長さの組あわせをやってます。
一方が一方の長さを越えた時点で枝を狩るようにしてます。

僕が働いているAzit.incでは一緒に働けるエンジニアを募集しています!
採用情報 — 株式会社アジット|Azit Inc.