首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >按螺旋顺序打印二维数组

按螺旋顺序打印二维数组
EN

Stack Overflow用户
提问于 2009-04-07 17:23:33
回答 36查看 97.4K关注 0票数 59

如何按螺旋顺序打印5×5的二维数组?

有没有什么公式可以让我按螺旋顺序打印任意大小的数组?

EN

回答 36

Stack Overflow用户

回答已采纳

发布于 2011-02-16 12:48:18

其思想是将矩阵视为一系列层,右上层和左下层。要螺旋打印矩阵,我们可以从这些矩阵剥离层,打印剥离的部分,并递归调用左侧部分的打印。当我们没有更多的层可打印时,递归终止。

输入矩阵:

代码语言:javascript
复制
1 2 3 4 
5 6 7 8
9 0 1 2   
3 4 5 6 
7 8 9 1

剥离右上角图层后的

代码语言:javascript
复制
 1 2 3 4 
       8
5 6 7  2
9 0 1  6   
3 4 5  1 
7 8 9

从子矩阵中剥离左下层后的

代码语言:javascript
复制
   6 7
5  0 1   
9  4 5
3 
7 8 9 

从子矩阵中剥离右上层后的

代码语言:javascript
复制
    6 7
      1   
   0  5
   4

从子矩阵中剥离左下层后的

代码语言:javascript
复制
  0
  4

递归终止。

C函数:

代码语言:javascript
复制
// function to print the top-right peel of the matrix and 
// recursively call the print bottom-left on the submatrix.
void printTopRight(int a[][COL], int x1, int y1, int x2, int y2) {
    int i = 0, j = 0;

    // print values in the row.
    for(i = x1; i<=x2; i++) {
        printf("%d ", a[y1][i]);
    }

    // print values in the column.
    for(j = y1 + 1; j <= y2; j++)         {
        printf("%d ", a[j][x2]);
    }

    // see if more layers need to be printed.
    if(x2-x1 > 0) {
        // if yes recursively call the function to 
        // print the bottom left of the sub matrix.
        printBottomLeft(a, x1, y1 + 1, x2-1, y2);
    }
}

// function to print the bottom-left peel of the matrix and 
// recursively call the print top-right on the submatrix.
void printBottomLeft(int a[][COL], int x1, int y1, int x2, int y2) {
    int i = 0, j = 0;

    // print the values in the row in reverse order.
    for(i = x2; i>=x1; i--) {
        printf("%d ", a[y2][i]);
    }

    // print the values in the col in reverse order.
    for(j = y2 - 1; j >= y1; j--) {
        printf("%d ", a[j][x1]);
    }

    // see if more layers need to be printed.
    if(x2-x1 > 0) {
        // if yes recursively call the function to 
        // print the top right of the sub matrix.
        printTopRight(a, x1+1, y1, x2, y2-1);
    }
}

void printSpiral(int arr[][COL]) {
    printTopRight(arr,0,0,COL-1,ROW-1);
    printf("\n");
}
票数 81
EN

Stack Overflow用户

发布于 2014-06-18 00:43:23

  1. 弹出窗口顶行颠倒转置和翻转(与旋转90度counter-clockwise)
  2. Go to 1

相同

Python 2代码:

代码语言:javascript
复制
import itertools

arr = [[1,2,3,4],
       [12,13,14,5],
       [11,16,15,6],
       [10,9,8,7]]

def transpose_and_yield_top(arr):
    while arr:
        yield arr[0]
        arr = list(reversed(zip(*arr[1:])))

print list(itertools.chain(*transpose_and_yield_top(arr)))

对于python 3x:

代码语言:javascript
复制
import itertools

arr = [[1,2,3,4],
       [12,13,14,5],
       [11,16,15,6],
       [10,9,8,7]]

def transpose_and_yield_top(arr):
while arr:
    yield arr[0]
    arr = list(reversed(list(zip(*arr[1:]))))


print(list(itertools.chain(*transpose_and_yield_top(arr))))
票数 34
EN

Stack Overflow用户

发布于 2012-11-17 10:53:35

下面是三种有趣的方式

螺旋方式的

  1. 阅读可以被视为一条蛇,它朝边界移动,然后开始攻击边界或自身(我发现它优雅且最有效的方式是N次迭代的单循环)

ar =[ 0,1,2,3,4,15,16,17,18,5,14,23,24,19,6,13,22,21,20,7,12,11,10,9,8] def print_spiral(ar):“”假设一个矩形数组“”print_spiral,cols = len(ar),len(ar) r,c= 0,-1 # start here nextturn = stepsx = cols #移动这么多步stepsy = rows-1 \f25 inc_c,inc_r = 1,0#在每一步移动这么多转数=0#我们的蛇在范围(行*cols)内为i转弯了多少次:C += inc_c r += inc_r print arr,if I == nextturn-1: turns += 1#在每个转弯时减少我们下一步的步数如果turns%2==0: nextturn += stepsx stepsy -= 1 else: nextturn += stepsx -= 1# change directions inc_c,inc_r = -inc_r,inc_c print_spiral(ar)

输出:

代码语言:javascript
复制
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

  1. 递归方法是打印外层,并为内部矩形e.g.def print_spiral(ar,sr=0,sc=0,er=None,ec=None)调用相同的函数: er = er或e.g.def(Ar)-1 ec = ec或len(ar)-1如果sr > er或sc > ec:打印返回#打印外层的顶部,底部,左侧,右侧= [],[]对于范围(sc,ec+1):top.append(arsr) if sr != er: bottom.append( sr+1,er):right.append(arr) if ec != sc: left.append(r-sr).join“”print_spiral(字符串(A)表示上+右+下+左),#剥离洋葱的下一层(ar,sr+1,sc+1,er-1,ec-1)

最后是一小段代码,效率不高,但很有趣:),基本上它打印顶行,并逆时针旋转整个矩形并重复

代码语言:javascript
复制
def print_spiral(ar):
    if not ar: return
    print " ".join(str(a) for a in ar[0]),
    ar = zip(*[ reversed(row) for row in ar[1:]])
    print_spiral(ar)
票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/726756

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档