开始,以蛇形图案打印矩阵

原文:

给定一个由 n 行 n 列组成的二维矩阵。如下图所示,从第 n-1 列开始以蛇形方式打印该矩阵。

示例:

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. 第一次移动总是向左(西)方向水平移动。
  3. 或者,在矩阵遍历期间进行水平和垂直移动。
  4. 在一次水平移动中,我们遍历多个单元格,直到到达矩阵的任何一个壁。
  5. 在水平移动中,如果行是奇数,我们向右(东)方向移动,否则我们向左(西)方向移动
  6. 在单次垂直移动中,我们沿向下方向遍历单个单元格。

以下是上述算法的实现:

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);
    }
}

python 3

# 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);
    }
}

服务器端编程语言(professional hypertext preprocessor 的缩写)

= 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);
?>

output:

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)