📜  使用递归以对角线形式遍历矩阵

📅  最后修改于: 2021-05-24 21:03:43             🧑  作者: Mango

给定大小为N x N的矩阵mat [] [] ,任务是使用递归以自下而上的方式对角地遍历矩阵。
对角自下而上的遍历:

  • 遍历矩阵的大对角线。
  • 将底部对角线遍历到矩阵的大对角线。
  • 将上对角线遍历到矩阵的大对角线。
  • 同样,遍历每个对角线的矩阵。

下图显示了矩阵的自下而上遍历。

例子:

Input: 
M[][] = {{11, 42, 25, 51}, 
         {14, 17, 61, 23},
         {22, 38, 19, 12},
         {27, 81, 29, 71}} 
Output: 
11 17 19 71 
14 38 29 
42 61 12 
22 81 
25 23 
27 
51 

Input: 
M[][] = {{3, 2, 5}, 
         {4, 7, 6},
         {2, 8, 9}}
Output: 
3 7 9 
4 8 
2 6 
2 
5  

方法:想法是遍历矩阵的主对角线元素,然后递归地调用矩阵的底部对角线和上方对角线到矩阵的主对角线。该方法的递归定义描述如下:

  • 函数定义:对于此问题,将有以下参数,如下所示:
    • mat [] [] //要遍历的矩阵
    • 当前行(比如i )//要遍历的当前行
    • 当前列(例如j )//要遍历的当前列
    • 行数(例如row )
    • 列数(例如col )
  • 基本情况:此问题的基本情况可以是当前行或当前列超出范围时。在这种情况下,遍历另一个底部对角线,或者如果最后一次选择了底部对角线,则遍历它上方的大对角线。
if (i >= row or j >= col)
    if (flag)
        // Change the Current index
        // to the bottom diagonal
    else
        // Change the current index
        // to the up diagonal of matrix


  • 递归情况:矩阵的递归遍历有两种情况,定义如下:
    • 当前对角线的遍历要遍历当前对角线,请同时将当前行和列增加1,然后递归调用该函数。
    • 底部/上对角线的遍历要遍历底部/上对角线,请调用带有静态变量的递归函数,该静态变量存储矩阵的下一个遍历起点。

下面是上述方法的实现:

C++
// C++ implementation to traverse the
// matrix in the bottom-up fashion
// using Recursion
 
#include 
 
using namespace std;
 
// Recursive function to traverse the
// matrix Diagonally Bottom-up
bool traverseMatrixDiagonally(int m[][5],
          int i, int j, int row, int col)
{
     
    // Static variable for changing
    // Row and coloumn
    static int k1 = 0, k2 = 0;
     
    // Flag variable for handling
    // Bottum up diagonal traversing
    static bool flag = true;
     
    // Base Condition
    if (i >= row || j >= col) {
         
        // Condition when to traverse
        // Bottom Diagonal of the matrix
        if (flag) {
            int a = k1;
            k1 = k2;
            k2 = a;
            flag = !flag;
            k1++;
        }
        else {
 
            int a = k1;
            k1 = k2;
            k2 = a;
            flag = !flag;
        }
        cout << endl;
        return false;
    }
     
    // Print matrix cell value
    cout << m[i][j] << " ";
     
    // Recursive function to traverse
    // The matrix diagonally
    if (traverseMatrixDiagonally(
           m, i + 1, j + 1, row, col)) {
        return true;
    }
    // Recursive function
    // to change diagonal
    if (traverseMatrixDiagonally(
            m, k1, k2, row, col)) {
        return true;
    }
     
    return true;
}
 
// Driver Code
int main()
{
    // Intialize the 5 x 5 matrix
    int mtrx[5][5] = {
        { 10, 11, 12, 13, 14 },
        { 15, 16, 17, 18, 19 },
        { 20, 21, 22, 23, 24 },
        { 25, 26, 27, 28, 29 },
        { 30, 31, 32, 33, 34 }
    };
 
    // Function call
    // for traversing matrix
    traverseMatrixDiagonally(
            mtrx, 0, 0, 5, 5);
}


Java
// Java implementation to traverse
// the matrix in the bottom-up
// fashion using recursion
class GFG{
     
// Static variable for changing
// row and coloumn
static int k1 = 0, k2 = 0;
 
// Flag variable for handling
// bottum up diagonal traversing
static boolean flag = true;
 
// Recursive function to traverse the
// matrix diagonally bottom-up
static boolean traverseMatrixDiagonally(int m[][], int i,
                                        int j, int row,
                                        int col)
{
     
    // Base Condition
    if (i >= row || j >= col)
    {
         
        // Condition when to traverse
        // Bottom Diagonal of the matrix
        if (flag)
        {
            int a = k1;
            k1 = k2;
            k2 = a;
            flag = !flag;
            k1++;
        }
        else
        {
            int a = k1;
            k1 = k2;
            k2 = a;
            flag = !flag;
        }
         
        System.out.println();
        return false;
    }
     
    // Print matrix cell value
    System.out.print(m[i][j] + " ");
     
    // Recursive function to traverse
    // The matrix diagonally
    if (traverseMatrixDiagonally(m, i + 1,
                                    j + 1, row, col))
    {
        return true;
    }
     
    // Recursive function
    // to change diagonal
    if (traverseMatrixDiagonally(m, k1, k2, row, col))
    {
        return true;
    }
     
    return true;
}
 
// Driver Code
public static void main(String[] args)
{
    // Intialize the 5 x 5 matrix
    int mtrx[][] = { { 10, 11, 12, 13, 14 },
                     { 15, 16, 17, 18, 19 },
                     { 20, 21, 22, 23, 24 },
                     { 25, 26, 27, 28, 29 },
                     { 30, 31, 32, 33, 34 } };
 
    // Function call
    // for traversing matrix
    traverseMatrixDiagonally(mtrx, 0, 0, 5, 5);
}
}
 
// This code is contributed by sapnasingh4991


Python3
# Python3 implementation to traverse the
# matrix in the bottom-up fashion
# using Recursion
 
# Static variable for changing
# Row and coloumn
k1 = 0
k2 = 0
 
# Flag variable for handling
# Bottum up diagonal traversing
flag = True
 
# Recursive function to traverse the
# matrix Diagonally Bottom-up
def traverseMatrixDiagonally(m, i,
                             j, row, col):
    global k1
    global k2
    global flag
 
    # Base Condition
    if(i >= row or j >= col):
 
        # Condition when to traverse
        # Bottom Diagonal of the matrix
        if(flag):
            a = k1
            k1 = k2
            k2 = a
            if(flag):
                flag = False
            else:
                flag = True
            k1 += 1
        else:
            a = k1
            k1 = k2
            k2 = a
            if(flag):
                flag = False
            else:
                flag = True
        print()
        return False
       
    # Print matrix cell value
    print(m[i][j], end = " ")
 
    # Recursive function to traverse
    # The matrix diagonally
    if (traverseMatrixDiagonally(m, i + 1,
                                 j + 1,
                                 row, col)):
        return True
 
    # Recursive function 
    # to change diagonal
    if(traverseMatrixDiagonally(m, k1,
                                k2, row, col)):
        return True
    return True
 
# Driver Code
 
# Intialize the 5 x 5 matrix
mtrx=[[10, 11, 12, 13, 14],
      [15, 16, 17, 18, 19],
      [20, 21, 22, 23, 24],
      [25, 26, 27, 28, 29],
      [30, 31, 32, 33, 34]]
 
# Function call 
# for traversing matrix
traverseMatrixDiagonally(mtrx, 0,
                         0, 5, 5)
 
#This code is contributed by avanitrachhadiya2155


C#
// C# implementation to traverse
// the matrix in the bottom-up
// fashion using recursion
using System;
 
class GFG{
     
// Static variable for changing
// row and coloumn
static int k1 = 0, k2 = 0;
 
// Flag variable for handling
// bottum up diagonal traversing
static bool flag = true;
 
// Recursive function to traverse the
// matrix diagonally bottom-up
static bool traverseMatrixDiagonally(int [,]m, int i,
                                     int j, int row,
                                     int col)
{
     
    // Base Condition
    if (i >= row || j >= col)
    {
         
        // Condition when to traverse
        // Bottom Diagonal of the matrix
        if (flag)
        {
            int a = k1;
            k1 = k2;
            k2 = a;
            flag = !flag;
            k1++;
        }
        else
        {
            int a = k1;
            k1 = k2;
            k2 = a;
            flag = !flag;
        }
         
        Console.WriteLine();
        return false;
    }
     
    // Print matrix cell value
    Console.Write(m[i, j] + " ");
     
    // Recursive function to traverse
    // The matrix diagonally
    if (traverseMatrixDiagonally(m, i + 1,
                                    j + 1, row, col))
    {
        return true;
    }
     
    // Recursive function
    // to change diagonal
    if (traverseMatrixDiagonally(m, k1, k2, row, col))
    {
        return true;
    }
    return true;
}
 
// Driver Code
public static void Main(String[] args)
{
     
    // Intialize the 5 x 5 matrix
    int [,]mtrx = { { 10, 11, 12, 13, 14 },
                    { 15, 16, 17, 18, 19 },
                    { 20, 21, 22, 23, 24 },
                    { 25, 26, 27, 28, 29 },
                    { 30, 31, 32, 33, 34 } };
 
    // Function call for 
    // traversing matrix
    traverseMatrixDiagonally(mtrx, 0, 0, 5, 5);
}
}
 
// This code is contributed by Amit Katiyar


输出
10 16 22 28 34 
15 21 27 33 
11 17 23 29 
20 26 32 
12 18 24 
25 31 
13 19 
30 
14 


时间复杂度: O(N 2 )