打印NxN升序螺旋形[关闭]


12

数字应在长度=(N ^ 2的位数)的字段中以前导零开头。

输入(N):

4

输出:

01 12 11 10
02 13 16 09
03 14 15 08
04 05 06 07

我对算法和实现的清洁度感兴趣。因此,空格不计算在内,N的上限为42。


是的,根据我。
Dogbert 2011年

根据我的计算,字段长度L = floor(log10(N^2)) + 1是正确的吗?
赫里斯托(Hristo Hristov)

的上限是N多少?

我对算法和实现的清洁度感兴趣。因此,让我们不要烦恼很大的数字并将N的上限设置为42 :)
Hristo Hristov

这是出于Ulam Spiral的动机吗?(尽管您的螺旋线从内部开始却在下降)
smci 2012年

Answers:


6

蟒蛇

n=input()

matrix=[[j+1]*n for j in range(n)]

x=y=0
for i in range(n)[::-2]:
    x+=i*4;y+=1

    for j in range(i):
        matrix[j+y-1][y]=x+j

    matrix[y-1][y:y+i]=range(x,x-i,-1)

    R=matrix[n-y][y-1]+1
    matrix[n-y][y:n-y+1]=range(R,R+i)

    for j in range(y,y+i-1):
        matrix[j][n-y]=matrix[j-1][n-y]-1

for row in matrix:
    print ' '.join(`r`.zfill(len(`n*n`)) for r in row)
  • 一种预先计算角数的方法。例如,对于9x盒子,为32 56 72 80,为(n-1)* 4,其中n是这种情况下的盒子尺寸(9,7,5,3)。
  • 这些数字的右边是1,而上到下是1+,因此基本上是从左到右,从上到下,从下到右,从右到顶生成。

在此处输入图片说明

$ echo 9 | python codegolf-769-me.py
01 32 31 30 29 28 27 26 25
02 33 56 55 54 53 52 51 24
03 34 57 72 71 70 69 50 23
04 35 58 73 80 79 68 49 22
05 36 59 74 81 78 67 48 21
06 37 60 75 76 77 66 47 20
07 38 61 62 63 64 65 46 19
08 39 40 41 42 43 44 45 18
09 10 11 12 13 14 15 16 17

其他测试

$ echo 2 | python codegolf-769-me.py
1 4
2 3

$ echo 5 | python codegolf-769-me.py
01 16 15 14 13
02 17 24 23 12
03 18 25 22 11
04 19 20 21 10
05 06 07 08 09

$ echo 10 | python codegolf-769-me.py
001 036 035 034 033 032 031 030 029 028
002 037 064 063 062 061 060 059 058 027
003 038 065 084 083 082 081 080 057 026
004 039 066 085 096 095 094 079 056 025
005 040 067 086 097 100 093 078 055 024
006 041 068 087 098 099 092 077 054 023
007 042 069 088 089 090 091 076 053 022
008 043 070 071 072 073 074 075 052 021
009 044 045 046 047 048 049 050 051 020
010 011 012 013 014 015 016 017 018 019

5

在Ruby中:

N=gets.to_i

index = -N
width = N
result = []
n = 0
dir=-1

while n < N*N
        dir = (dir + 1) % 4
        dir_x, dir_y = [[0,1],[1,0],[0,-1],[-1,0]][dir]
        width -= 1 if [1,3].include?(dir)

        1.upto(width) { |m|
                n += 1
                index += dir_y * N + dir_x
                result[index] = n
        }
end

width = (N*N).to_s.size
result.each_slice(N) { |l|
        print l.map {|n| "%0#{width}d" % n }.join(" "), "\n"
}

测试:

$ ruby1.9 769.rb <<< 9
01 32 31 30 29 28 27 26 25
02 33 56 55 54 53 52 51 24
03 34 57 72 71 70 69 50 23
04 35 58 73 80 79 68 49 22
05 36 59 74 81 78 67 48 21
06 37 60 75 76 77 66 47 20
07 38 61 62 63 64 65 46 19
08 39 40 41 42 43 44 45 18
09 10 11 12 13 14 15 16 17

另一种使用此处计算的解决方案:

N=gets.to_i
r=[]

tr=->x,y{ x+(N-1)/2 + (y+(N-1)/2+(N-1)%2)*N }

r[tr[0,0]] = N*N

1.upto(N*N-1) { |n|
        shell = ((Math.sqrt(n)+1)/2).to_i
        leg = (n-(2*shell-1)**2)/(2*shell)
        element = (n-(2*shell-1)**2)-2*shell*leg-shell+1
        x,y = [[element,-shell],[shell,element],[-element,shell],[-shell,-element]][leg]
        r[tr[x,y]] = N*N-n
}

r.each_slice(N) {|l|
        puts l.map { |n|
                "%0#{(N*N).to_s.size}d" % (n or 0)
        }.join(" ")
}

测试:

$ ruby1.9 769-2.rb <<< 5
01 16 15 14 13
02 17 24 23 12
03 18 25 22 11
04 19 20 21 10
05 06 07 08 09

5

在Python3中:

n=int(input())
results = {}
val = 1
location = (0,0)
direction = (0,1)

def nxt():
    return (location[0]+direction[0], location[1]+direction[1])

while val<=n*n:
    if set([-1,n]).intersection(nxt()) or nxt() in results:
        direction = (direction[1],direction[0]*-1)

    results[location], location, val = str(val), nxt(), val+1

slen = len(str(n*n))
for y in range(n):
    print( *[results[(x,y)].rjust(slen,'0') for x in range(n)] )

7的样本输出:

01 24 23 22 21 20 19
02 25 40 39 38 37 18
03 26 41 48 47 36 17
04 27 42 49 46 35 16
05 28 43 44 45 34 15
06 29 30 31 32 33 14
07 08 09 10 11 12 13

编辑:递归解决方案-263字节

def a(m,s):
 b,r,t=m-s*s+1,s-1,range
 return[[[]],[[m]]][s]if s<2 else[[b]+list(t(b+4*r-1,b+3*r-1,-1))]+[[b+y+1]+a(m,s-2)[y]+[b+3*r-y-1]for y in t(s-2)]+[list(t(b+r,b+2*r+1))]
n=int(input())
for r in a(n*n,n):
 print(*[str(x).zfill(len(str(n*n)))for x in r])

不错的解决方案,我已经在ideone.com/u43VJ上进行了检查,它可与Python3配合使用(请注意这一点)。谢谢!
赫里斯托(Hristo Hristov)

4

Java解决方案

public static void main(String[] args) {
        int INPUT = 5;
        String[][] grid = new String[INPUT][INPUT];
        int xDirection = 0;
        int yDirection = 0;
        int flag = 1;
        for (int i = 0; i < INPUT * INPUT; i++) {
            String temp = "";
            for (int k = 0; k < (""+INPUT*INPUT).length() - ("" + (i + 1)).length(); k++) {
                temp += "" + 0;
            }
            temp += (i + 1);

            if (xDirection > INPUT-1)
                {flag=2; yDirection++; xDirection--; i--; continue;}
            else if (yDirection > INPUT -1)
                {flag=3; yDirection--; xDirection--; i--; continue;}
            else if (xDirection < 0)
                {flag=4; xDirection++; yDirection--; i--; continue;}



            if ( grid[xDirection][yDirection]==null ){
                    grid[xDirection][yDirection] = ""+temp;
            }
                else{
                if (flag ==1 ) {
                    flag=2;
                    xDirection--;
                }
                else if (flag ==2){
                    flag=3;
                    yDirection--;
                }
                else if (flag==3){
                    flag=4;
                    xDirection++;
                }
                else{
                    flag=1;
                    yDirection++;
                }
                i--;
            }
            switch(flag){
            case 1: xDirection++;break;
            case 2: yDirection++;break;
            case 3: xDirection--;break;
            case 4: yDirection--; break;
            }
        }
        for (int i = 0; i < INPUT; i++) {
            for (int k = 0; k < INPUT; k++)
                System.out.print(grid[i][k] + " ");
            System.out.println();
        }
    }

输入10的样本输出

001 036 035 034 033 032 031 030 029 028 
002 037 064 063 062 061 060 059 058 027 
003 038 065 084 083 082 081 080 057 026 
004 039 066 085 096 095 094 079 056 025 
005 040 067 086 097 100 093 078 055 024 
006 041 068 087 098 099 092 077 054 023 
007 042 069 088 089 090 091 076 053 022 
008 043 070 071 072 073 074 075 052 021 
009 044 045 046 047 048 049 050 051 020 
010 011 012 013 014 015 016 017 018 019 


但是,它会打印额外的信息,只需要所需的输出。而且,如果可以减少冗长的话,它将非常酷。
赫里斯托里斯托夫

此外,应将main函数放在一个类中。
赫里斯托(Hristo Hristov)

啊!..忘了评论调试sysouts了:(
Aman ZeeK Verma 2011年

也许您可以在代码中修复它?
赫里斯托(Hristo Hristov)

4

Perl,178个字符

使用Math :: Complex并在复数变量(1 / i / -1 / .i)中保持当前方向。运行:

$ perl -MMath::Complex spiral.pl

N$l

# $l = shift;
$d=i;
$x=0;
until($s{$x}){
    $s{$x}=++$n;
    $x+=$d;
    $d*=-i if
        Re($x)==Im($x)+(Re($x)<$l/2)
        ||Re($x)==$l-1-Im($x)
}
for$y(0..$l-1){
    printf'%0'.length($l**2).'d ',$s{$_+i*$y}for 0..$l-1;
    print"\n"
}

3

C

#include<stdio.h>
#include<math.h>

int main() {
    int A[42][42],i,j,N,c=1,k;
    scanf("%d",&N);
    for (i = 0, j = N - 1 ; j >= 0 ; i++, j--) {
            for(k = i ; k < j; k++)A[i][k]=c++;
            for(k = i ; k < j; k++)A[k][j]=c++;
            for(k = j ; k > i; k--)A[j][k]=c++;
            for(k = j ; k > i; k--)A[k][i]=c++;
    }
    if (N%2)
       A[N/2][N/2]=c;
    for (i=0;i<N;i++) {
        for (j=0;j<N;j++)
            printf("%0*d ",((int)log10(N*N)+1),A[j][i]);
        printf("\n");
    }
}

3

Python 2.7:

def spiral(n):
   rows = [[n * n]]
   current = n * n - 1

   while current:
      rows = zip(*([range(current, current - len(rows[0]), -1)] + rows))[::-1]
      current -= len(rows)

   digits = len(str(n * n))
   for row in rows:
      print" ".join(str(cell).zfill(digits) for cell in row)

spiral(5)

3

PHP,272个字符,包括注释

基于功能的递归版本-对我来说更有趣,因为它可以更好地表达意图。它也适用于不同的宽度和高度。

<?php

$n = $argv[1];

for($y = 0; $y<$n; $y++){
    for($x = 0; $x<$n; $x++)
        printf("%02d ", f($n, $n, $x, $y));

    echo "\n";
}

function f($w, $h, $x, $y){
    return ($y)
        ?$w + f($h - 1, $w, $y - 1, $w - $x - 1) //strip-off first row and "rotate"
        :$x;
}

输出:

C:\www>php -f golfed_spiral.php 8
00 01 02 03 04 05 06 07
27 28 29 30 31 32 33 08
26 47 48 49 50 51 34 09
25 46 59 60 61 52 35 10
24 45 58 63 62 53 36 11
23 44 57 56 55 54 37 12
22 43 42 41 40 39 38 13
21 20 19 18 17 16 15 14

2

C#,打了380球

我不介意粘贴高尔夫球版,因为我很确信这不会打破任何记录。但是我想稍微考虑一下。与其写出每条线或位置,不如说是将光标移到位置,写出起始中心号,然后从那里盘旋成螺旋形(这说明了一个有趣的方向变化的位置模式) )。

浪费了大量的字符空间,使控制台缓冲区可以接受较大的值以及计算左上角的位置(我敢肯定可以改进)。

无论如何,这是一个有趣的练习。

    static void Main(string[] p)
    {
        int squareSize = 4;
        Console.BufferHeight = 300;
        Console.BufferWidth = 300;

        int maxTravel = 0;
        int currentTravel = 0;
        int travelCounter = 0;
        var a = squareSize % 2 == 0;
        int direction = a ? 2 : 0;
        int pad = squareSize * squareSize;
        int padLength = (pad + "").Length;

        int y = a ? (squareSize / 2) - 1 : (squareSize - 1) / 2;
        int x = a ? y + 1 : y;
        x = x + (x * padLength);

        for (int i = pad; i > 0; i--)
        {
            Console.SetCursorPosition(x, y);
            Console.Write((i + "").PadLeft(padLength, '0') + " ");

            switch (direction)
            {
                case 0:
                    y--;
                    break;

                case 1:
                    x += padLength + 1;
                    break;

                case 2:
                    y++;
                    break;

                case 3:
                    x -= padLength + 1;
                    break;
            }

            if (++currentTravel > maxTravel)
            {
                currentTravel = 0;
                direction = ++direction % 4;

                if (++travelCounter == 2)
                {
                    travelCounter = 0;
                    maxTravel++;
                }
            }

        }
    }

2

红宝石

这不是一个特别好的高尔夫解决方案,但可能与算法有关。

我一直对类似的问题着迷,即通过NxM矩阵找到顺时针螺旋路径。解决该问题的一种非常直观的方法是保持逆时针旋转矩阵并将其像橘子一样剥离。我使用类似的方法-尽管不那么优雅-进行相反的操作:

def spiral_matrix(n)
  matrix = Array.new(n) { Array.new(n) }
  path = [*1..n*n]
  padding = (n*n).to_s.size
  layer = 0
  until path.empty?
    matrix[layer].map! { |l| l || path.shift }
    matrix = matrix.transpose.reverse
    layer += 1 unless matrix[layer].include?(nil)
  end
  matrix = matrix.transpose.reverse until matrix[0][0] == 1
  matrix.transpose.each do |row|
    row.each do |l|
      print "%0#{padding}d" % l, ' '
    end
    puts
  end
end

1

球拍

只是想尝试使用接近0内存的解决方案。没有数组,什么都没有。可以随时为任何位置生成该值。我们可以问一个任意大小的螺旋(如果接收到的输出流能够处理它)。希望有人需要巨大的螺旋。

这是代码

; number of chars required to write x in base 10
; defined for x > 0
(define log10
  (λ (x)
    (inexact->exact
     (+ 1 (floor (/ (log x) (log 10)))))))

; tells the square number
; works for squares of both even and odd sizes
; outer square # = 0
(define square#
  (λ (x y size) ; x and y begin at 0
    (min x y
         (- size 1 x)
         (- size 1 y))))

; tells the number of values in a square
(define square-val-qty
  (λ (sqr# size) ; size is the whole spiral size
    (let ((res (* 4 (- size (* 2 sqr#) 1))))
      (cond
        ((zero? res) 1)
        (else res)))))


; at which value a square starts
; works for odd/even spirals
(define square-1st-val
  (λ (sqr# size)
    (+ (* 4 sqr# (- size sqr#)) 1)))

; square size from spiral size
(define square-side
  (λ (sqr# size)
    (- size (* 2 sqr#))))

(define 1+
  (λ (n)
    (+ n 1)))

(define 1-
  (λ (n)
    (- n 1)))

; calculates the position on the square (from 0)
(define position-on-square
  (λ (x y size)
    (let* ((sqr#     (square# x y size))
           (sqr-x    (- x sqr#))
           (sqr-y    (- y sqr#))
           (sqr-side (square-side sqr# size)))
      (cond
        ((and (zero? sqr-x) (< sqr-y (1- sqr-side))) ; left part
         sqr-y)
        ((and (eq? sqr-y (1- sqr-side)) (< sqr-x (1- sqr-side))) ; bottom
         (+ (1- sqr-side) sqr-x))
        ((and (not (eq? sqr-y 0)) (eq? sqr-x (1- sqr-side))) ; right
         (+ (* 2 (1- sqr-side)) (- sqr-side sqr-y 1)))
        (else ; top
         (+ (* 3 (1- sqr-side)) (- sqr-side sqr-x 1)))))))

; returns the spiral value at the given position
(define spiral-value
  (λ (x y size)
    (+ (square-1st-val (square# x y size) size)
       (position-on-square x y size))))

; pads a string with char
(define left-pad
  (λ (str char width)
    (cond
      ((< (string-length str) width)
       (left-pad (string-append (string char) str) char width))
      (else
       str))))

; draws a spiral!
(define draw-spiral
  (λ (size)
    (let ((x 0)
          (y 0)
          (width (log10 (* size size))))
      (letrec ((draw
                (λ ()
                  (printf "~a " (left-pad (number->string (spiral-value x y size)) #\0 width))
                  (cond
                    ((and (eq? x (1- size)) (eq? y (1- size)))
                     (printf "~n~n"))
                    ((eq? x (1- size))
                     (set! x 0)
                     (set! y (1+ y))
                     (printf "~n")
                     (draw))
                    (else
                     (set! x (1+ x))
                     (draw))))))
        (draw)))))

用这个测试

(draw-spiral 1)
(draw-spiral 2)
(draw-spiral 3)
(draw-spiral 4)
(draw-spiral 5)
(draw-spiral 15)
(draw-spiral 16)

输出结果

1 

1 4 
2 3 

1 8 7 
2 9 6 
3 4 5 

01 12 11 10 
02 13 16 09 
03 14 15 08 
04 05 06 07 

01 16 15 14 13 
02 17 24 23 12 
03 18 25 22 11 
04 19 20 21 10 
05 06 07 08 09 

001 056 055 054 053 052 051 050 049 048 047 046 045 044 043 
002 057 104 103 102 101 100 099 098 097 096 095 094 093 042 
003 058 105 144 143 142 141 140 139 138 137 136 135 092 041 
004 059 106 145 176 175 174 173 172 171 170 169 134 091 040 
005 060 107 146 177 200 199 198 197 196 195 168 133 090 039 
006 061 108 147 178 201 216 215 214 213 194 167 132 089 038 
007 062 109 148 179 202 217 224 223 212 193 166 131 088 037 
008 063 110 149 180 203 218 225 222 211 192 165 130 087 036 
009 064 111 150 181 204 219 220 221 210 191 164 129 086 035 
010 065 112 151 182 205 206 207 208 209 190 163 128 085 034 
011 066 113 152 183 184 185 186 187 188 189 162 127 084 033 
012 067 114 153 154 155 156 157 158 159 160 161 126 083 032 
013 068 115 116 117 118 119 120 121 122 123 124 125 082 031 
014 069 070 071 072 073 074 075 076 077 078 079 080 081 030 
015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 

001 060 059 058 057 056 055 054 053 052 051 050 049 048 047 046 
002 061 112 111 110 109 108 107 106 105 104 103 102 101 100 045 
003 062 113 156 155 154 153 152 151 150 149 148 147 146 099 044 
004 063 114 157 192 191 190 189 188 187 186 185 184 145 098 043 
005 064 115 158 193 220 219 218 217 216 215 214 183 144 097 042 
006 065 116 159 194 221 240 239 238 237 236 213 182 143 096 041 
007 066 117 160 195 222 241 252 251 250 235 212 181 142 095 040 
008 067 118 161 196 223 242 253 256 249 234 211 180 141 094 039 
009 068 119 162 197 224 243 254 255 248 233 210 179 140 093 038 
010 069 120 163 198 225 244 245 246 247 232 209 178 139 092 037 
011 070 121 164 199 226 227 228 229 230 231 208 177 138 091 036 
012 071 122 165 200 201 202 203 204 205 206 207 176 137 090 035 
013 072 123 166 167 168 169 170 171 172 173 174 175 136 089 034 
014 073 124 125 126 127 128 129 130 131 132 133 134 135 088 033 
015 074 075 076 077 078 079 080 081 082 083 084 085 086 087 032 
016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 

如果您需要整个螺旋,则与预先计算的矩阵相比,CPU占用大量内存,但可能很有用。谁知道!例如:

(spiral-value 1234567 7654321 234567890)  ->  1152262488724319

没打高尔夫球...尽管有外观,它还是很小的。我使用了长名和注释。


1

Python 2,800字节

from collections import namedtuple
Crd = namedtuple('Crd',['row','col','val'])
C1 = Crd(1,1,1)
def add(c1, c2):
    return Crd(c1.row + c2.row, c1.col + c2.col, c1.val + c2.val)
def deltas(l):
    for i in xrange(1,l): yield Crd(0,1,1)
    for i in xrange(1,l): yield Crd(1,0,1)
    for i in xrange(1,l): yield Crd(0,-1,1)
    for i in xrange(1,l-1): yield Crd(-1,0,1)
def ring(c, l):
    yield c
    for d in deltas(l):
        c = add(c, d)
        yield c
def spiral(n):
    cur = C1
    while n > 0:
        for c in ring(cur, n):
            yield c
            cur = c
        cur = add(cur, Crd(0,1,1))
        n -= 2
n    = input()
fmt  = '%' + str(len(str(long(n*n)))) + 'd'
crds = sorted(list(spiral(n)))
for r in xrange(1,n+1):
    print ' '.join([fmt % c.val for c in crds if c.row == r])

在线尝试!

几年前,我的一个朋友在一次采访中被问到这个问题。他们在我们家人的感恩节晚餐上告诉我这件事,所以我认为这是“感恩节问题”。


1

PHP,172 171 + 1个字节,24个操作

创建一个遍历螺旋的数组;然后打印结果。

// 1) input squared -> 2) string length -> 3) $e = length of maximum number
for($e=strlen($argn**2);
    // 4) decrement input (line length) every second iteration; 5) loop while input>0
    $argn-=$i%2;
    // 24) post-increment iteration counter $i
    $i++)
    // 6,7,8) loop through current line
    for($p=$argn;$p--;)$r
        // 9) $i=$i modulo 4; 10,11) (1-$i)%2 == [1,0,-1,0][$i] -> 12) increment/decrement $y coordinate3
        [$y+=(1-$i%=4)%2]
        // 13,14) (2-$i)%2 == [0,1,0,-1][$i] -> 15) increment/decrement $x coordinate
        [$x+=(2-$i)%2]
        // 16) print formatted to string; 17) assign to field [$y,$x] in $r
        =sprintf("%0{$e}d ",++$n);
// 18) pre-increment row counter $z; 19) loop while row exists
for(;$r[++$z];
    // 21) join row; 22) append newline; 23) print
    print join($r[$z])."\n")
    // 20) sort row by indexes
    ksort($r[$z]);

与管道一起运行-nR在线尝试

添加一个分配以节省五个字节:将最终循环替换为

for(;$s=$r[++$z];print join($s)."\n")ksort($s);

0

示例代码:这适用于4x5,但无法通过3x5

while(k <m && l <n){/ *从其余行中打印第一行* / for(i = l; i <n; ++ i){printf(“%d”,a [k] [一世]); } k ++;

    /* Print the last column from the remaining columns */
    for (i = k; i < m; ++i)
    {
        printf("%d ", a[i][n-1]);
    }
    n--;

    /* Print the last row from the remaining rows */
    if ( k < m)
    {
        for (i = n-1; i >= l; --i)
        {
            printf("%d ", a[m-1][i]);
        }
        m--;
    }

    /* Print the first column from the remaining columns */
    if (l < n)
    {
        for (i = m-1; i >= k; --i)
        {
            printf("%d ", a[i][l]);
        }
        l++;    
    }        
}

2
欢迎来到PPCG!这是代码高尔夫。请尽力解决该问题,并尽可能减少字符。特别是,您可以使用单字符变量名称,删除不必要的空格和注释。减少提交后,还请包括提交的字节数。除了打高尔夫球以外,您始终可以保留可读版本。
马丁·恩德
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.