(def n (read-string (read-line))) (defn solve [n i j] (if (> i j) (solve n j i) (if (= i j) (if (zero? (mod (dec n) i)) (/ (dec n) i) -1) (let [dn (dec n) done? #{[dn dn]} moves [[i j] [j i] [(- i) j] [i (- j)] [(- j) i] [j (- i)] [(- i) (- j)] [(- j) (- i)]]] (loop [depth 1 seen? #{[0 0]} latest [[0 0]]] (let [next (->> (mapcat #(map (fn [[x y]] [(+ x (first %)) (+ y (second %))]) latest) moves) (distinct) (filter #(every? (fn [x] (and (>= x 0) (< x n))) (flatten %))) (remove seen?))] (cond (some done? next) depth (seq next) (recur (inc depth) (into seen? next) next) :else -1))))))) (def solve (memoize solve)) (doseq [i (range (dec n))] (println (clojure.string/join " " (for [j (range (dec n))] (solve n (inc i) (inc j))))))