原文:

给定 2d 数组,任务是以反螺旋形式打印矩阵:

示例

输出:16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1

input : arr[][4] = {1, 2, 3, 4
                    5, 6, 7, 8
                    9, 10, 11, 12
                    13, 14, 15, 16};
output : 10 11 7 6 5 9 13 14 15 16 12 8 4 3 2 1
input :arr[][6] = {1, 2, 3, 4, 5, 6
                  7, 8, 9, 10, 11, 12
                  13, 14, 15, 16, 17, 18};
output : 11 10 9 8 7 13 14 15 16 17 18 12 6 5 4 3 2 1

想法很简单,我们 遍历矩阵,并将所有遍历的元素放入栈中。 最后从栈中逐一列出并打印它们。

c

// c   program to print matrix in anti-spiral form 
#include  
using namespace std; 
#define r 4 
#define c 5 
void antispiraltraversal(int m, int n, int a[r][c]) 
{ 
    int i, k = 0, l = 0; 
    /*  k - starting row index 
        m - ending row index 
        l - starting column index 
        n - ending column index 
        i - iterator  */
    stack stk; 
    while (k <= m && l <= n) 
    { 
        /* print the first row from the remaining rows */
        for (i = l; i <= n;   i) 
            stk.push(a[k][i]); 
        k  ; 
        /* print the last column from the remaining columns */
        for (i = k; i <= m;   i) 
            stk.push(a[i][n]); 
        n--; 
        /* print the last row from the remaining rows */
        if ( k <= m) 
        { 
            for (i = n; i >= l; --i) 
                stk.push(a[m][i]); 
            m--; 
        } 
        /* print the first column from the remaining columns */
        if (l <= n) 
        { 
            for (i = m; i >= k; --i) 
                stk.push(a[i][l]); 
            l  ; 
        } 
    } 
    while (!stk.empty()) 
    { 
        cout << stk.top() << " "; 
        stk.pop(); 
    } 
} 
/* driver program to test above functions */
int main() 
{ 
    int mat[r][c] = 
    { 
        {1,  2,  3,  4,  5}, 
        {6,  7,  8,  9,  10}, 
        {11, 12, 13, 14, 15}, 
        {16, 17, 18, 19, 20} 
    }; 
    antispiraltraversal(r-1, c-1, mat); 
    return 0; 
} 

java

// java code for print matrix in antispiral form 
import java.util.*; 
  
class gfg { 
      
    public static void antispiraltraversal(int m, int n,  
                                             int a[][]) 
    { 
        int i, k = 0, l = 0; 
       
        /*  k - starting row index 
            m - ending row index 
            l - starting column index 
            n - ending column index 
            i - iterator  */
        stack stk=new stack(); 
       
        while (k <= m && l <= n) 
        { 
            /* print the first row from the remaining  
             rows */
            for (i = l; i <= n;   i) 
                stk.push(a[k][i]); 
            k  ; 
       
            /* print the last column from the remaining 
            columns */
            for (i = k; i <= m;   i) 
                stk.push(a[i][n]); 
            n--; 
       
            /* print the last row from the remaining  
            rows */
            if ( k <= m) 
            { 
                for (i = n; i >= l; --i) 
                    stk.push(a[m][i]); 
                m--; 
            } 
       
            /* print the first column from the remaining  
            columns */
            if (l <= n) 
            { 
                for (i = m; i >= k; --i) 
                    stk.push(a[i][l]); 
                l  ; 
            } 
        } 
       
        while (!stk.empty()) 
        { 
            system.out.print(stk.peek()   " "); 
            stk.pop(); 
        } 
    } 
      
    /* driver program to test above function */
    public static void main(string[] args)  
    { 
         int mat[][] = 
                { 
                    {1,  2,  3,  4,  5}, 
                    {6,  7,  8,  9,  10}, 
                    {11, 12, 13, 14, 15}, 
                    {16, 17, 18, 19, 20} 
                }; 
               
        antispiraltraversal(mat.length - 1, mat[0].length - 1,  
                                                       mat); 
    } 
  } 
// this code is contributed by arnav kr. mandal.

python 3

# python 3 program to print 
# matrix in anti-spiral form 
r = 4
c = 5
  
def antispiraltraversal(m, n, a): 
    k = 0
    l = 0
  
    # k - starting row index 
    # m - ending row index 
    # l - starting column index 
    # n - ending column index 
    # i - iterator  
    stk = [] 
  
    while (k <= m and l <= n): 
          
        # print the first row  
        # from the remaining rows  
        for i in range(l, n   1): 
            stk.append(a[k][i]) 
        k  = 1
  
        # print the last column  
        # from the remaining columns  
        for i in range(k, m   1): 
            stk.append(a[i][n]) 
        n -= 1
  
        # print the last row 
        # from the remaining rows  
        if ( k <= m): 
            for i in range(n, l - 1, -1): 
                stk.append(a[m][i]) 
            m -= 1
  
        # print the first column  
        # from the remaining columns  
        if (l <= n): 
            for i in range(m, k - 1, -1): 
                stk.append(a[i][l]) 
            l  = 1
          
    while len(stk) != 0: 
        print(str(stk[-1]), end = " ") 
        stk.pop() 
  
# driver code 
mat = [[1, 2, 3, 4, 5], 
       [6, 7, 8, 9, 10], 
       [11, 12, 13, 14, 15], 
       [16, 17, 18, 19, 20]]; 
  
antispiraltraversal(r - 1, c - 1, mat) 
  
# this code is contributed 
# by chitranayal

c

using system; 
using system.collections.generic; 
  
// c# code for print matrix in antispiral form  
  
public class gfg 
{ 
  
    public static void antispiraltraversal(int m, int n, int[][] a) 
    { 
        int i, k = 0, l = 0; 
  
        /*  k - starting row index  
            m - ending row index  
            l - starting column index  
            n - ending column index  
            i - iterator  */
        stack stk = new stack(); 
  
        while (k <= m && l <= n) 
        { 
            /* print the first row from the remaining   
             rows */
            for (i = l; i <= n;   i) 
            { 
                stk.push(a[k][i]); 
            } 
            k  ; 
  
            /* print the last column from the remaining  
            columns */
            for (i = k; i <= m;   i) 
            { 
                stk.push(a[i][n]); 
            } 
            n--; 
  
            /* print the last row from the remaining   
            rows */
            if (k <= m) 
            { 
                for (i = n; i >= l; --i) 
                { 
                    stk.push(a[m][i]); 
                } 
                m--; 
            } 
  
            /* print the first column from the remaining   
            columns */
            if (l <= n) 
            { 
                for (i = m; i >= k; --i) 
                { 
                    stk.push(a[i][l]); 
                } 
                l  ; 
            } 
        } 
  
        while (stk.count > 0) 
        { 
            console.write(stk.peek()   " "); 
            stk.pop(); 
        } 
    } 
  
    /* driver program to test above function */
    public static void main(string[] args) 
    { 
         int[][] mat = new int[][] 
         { 
             new int[] {1, 2, 3, 4, 5}, 
             new int[] {6, 7, 8, 9, 10}, 
             new int[] {11, 12, 13, 14, 15}, 
             new int[] {16, 17, 18, 19, 20} 
         }; 
  
        antispiraltraversal(mat.length - 1, mat[0].length - 1, mat); 
    } 
} 
  
// this code is contributed by shrikant13

输出:

12 13 14 9 8 7 6 11 16 17 18 19 20 15 10 5 4 3 2 1