📜  从最后一列以蛇形打印矩阵

📅  最后修改于: 2021-06-26 21:20:25             🧑  作者: Mango

给定n行和n列的二维数组的矩阵。从n-1列开始以蛇形打印此矩阵,如下图所示。

matrix_traversal_snake

例子:

Input : mat[][] =  
1 2 3 
4 5 6
7 8 9
Output: 3 2 1 4 5 6 9 8 7

Input: mat[][] = 
1 2 3 4 
5 6 7 8 
9 10 11 12 
13 14 15 16
Output: 4 3 2 1 5 6 7 8 12 11 10 9 13 14 15 16

算法:

  1. 从属于第0行和n-1列的右上角单元格开始遍历。
  2. 第一个动作始终是向LEFT(WEST)方向的水平移动。
  3. 或者,在矩阵遍历期间进行水平和垂直移动。
  4. 在单个水平移动中,我们遍历多个单元,直到到达矩阵的任何壁。
  5. 在水平移动中,如果行是奇数,则我们向RIGHT(EAST)方向移动,否则我们向LEFT(WEST)方向移动
  6. 在单个垂直移动中,我们沿DOWNWARDS方向遍历单个单元格。

下面是上述算法的实现:

C++
// C++ program for traversing a matrix from column n-1
#include 
using namespace std;
  
// Function used for traversing over the given matrix
void traverseMatrix(vector > mat, int n)
{
  
    for (int i = 0; i < n; i++) {
        if (i%2 == 1)
            for (int j = 0; j < n; j++)
                printf("%d ", mat[i][j]);
  
        else
            for (int j = n - 1; j >= 0; j--)
                printf("%d ", mat[i][j]);
    }
}
  
// Driver function
int main()
{
  
    // number of rows and columns
    int n = 5;
  
    // 5x5 matrix
    vector > mat{
        { 1, 2, 3, 4, 5 },
        { 6, 7, 8, 9, 10 },
        { 11, 12, 13, 14, 15 },
        { 16, 17, 18, 19, 20 },
        { 21, 22, 23, 24, 25 }
    };
  
    traverseMatrix(mat, n);
  
    return 0;
}


Java
// Java program for traversing a matrix from column n-1
  
class GFG {
  
    // Function used for traversing over the given matrix
    static void traverseMatrix(int[][] mat, int n)
    {
  
        for (int i = 0; i < n; i++) {
            if (i % 2 == 1) {
                for (int j = 0; j < n; j++) {
                    System.out.print(
                        Integer.toString(mat[i][j]) + " ");
                }
            }
            else {
                for (int j = n - 1; j >= 0; j--) {
                    System.out.print(
                        Integer.toString(mat[i][j]) + " ");
                }
            }
        }
    }
  
    // Driver function
    public static void main(String[] args)
    {
  
        // number of rows and columns
        int n = 5;
  
        // 5x5 matrix
        int[][] mat = {
            { 1, 2, 3, 4, 5 },
            { 6, 7, 8, 9, 10 },
            { 11, 12, 13, 14, 15 },
            { 16, 17, 18, 19, 20 },
            { 21, 22, 23, 24, 25 }
        };
  
        traverseMatrix(mat, n);
  
        System.exit(0);
    }
}


Python3
# Python3 program for traversing a matrix from column n-1
import sys;
  
# Function used for traversing over the given matrix
def traverseMatrix(mat, n):
  
    for i in range(n): 
        if i & 1:
            for j in range(n):
                print(str(mat[i][j])+ "", end = " ")
        else:
            for j in range(n-1, -1, -1):
                print(str(mat[i][j])+ "", end = " ")
  
# Driver function
if __name__ == '__main__':
  
    # number of rows and columns
    n = 5
  
    # 5x5 matrix
    mat =[
         [1,  2,  3,  4,  5],
         [6,  7,  8,  9,  10],
         [11, 12, 13, 14, 15],
         [16, 17, 18, 19, 20],
         [21, 22, 23, 24, 25]
    ]
  
    traverseMatrix(mat, n)


C#
// CSHARP program for traversing a matrix from column n-1
  
using System;
using System.Linq;
  
class GFG {
  
    // Function used for traversing over the given matrix
    static void traverseMatrix(int[, ] mat, int n)
    {
  
        for (int i = 0; i < n; i++) {
            if (i % 2 == 1) {
                for (int j = 0; j < n; j++) {
                    Console.Write(mat[i, j].ToString() + " ");
                }
            }
            else {
                for (int j = n - 1; j >= 0; j--) {
                    Console.Write(mat[i, j].ToString() + " ");
                }
            }
        }
    }
  
    // Driver function
    public static void Main()
    {
  
        // number of rows and columns
        int n = 5;
  
        // 5x5 matrix
        int[, ] mat = {
            { 1, 2, 3, 4, 5 },
            { 6, 7, 8, 9, 10 },
            { 11, 12, 13, 14, 15 },
            { 16, 17, 18, 19, 20 },
            { 21, 22, 23, 24, 25 }
        };
  
        traverseMatrix(mat, n);
    }
}


PHP
= 0; $j--) {
                print($mat[$i][$j]." ");
            }    
        }
    }
} 
  
  
// Driver function
  
# number of rows and columns
$n = 5;
  
#  5x5 matrix
$mat = array(
     array(1,  2,  3,  4,  5),
     array(6,  7,  8,  9,  10),
     array(11, 12, 13, 14, 15),
     array(16, 17, 18, 19, 20),
     array(21, 22, 23, 24, 25)
);
  
traverseMatrix($mat, $n);
  
?>


输出:
5 4 3 2 1 6 7 8 9 10 15 14 13 12 11 16 17 18 19 20 25 24 23 22 21

时间复杂度:O(N ^ 2)
空间复杂度:O(1)

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。