原文:

给定一个尺寸为 n * marr【】【】和一个整数 k ,任务是以螺旋形式对角打印矩阵中从左上角元素到 k 的所有元素。

示例:

输入: n=5,m=6,k=15,arr[][]={{1,2,3,4,5,6}, {7,8,9,10,11,12}, {13,14,15,16,17,18}, {19,20,21,22,23,24},

| one | two | three | four | five | six | | seven | eight | nine | ten | eleven | twelve | | thirteen | fourteen | fifteen | sixteen | seventeen | eighteen | | nineteen | twenty | twenty-one | twenty-two | twenty-three | twenty-four | | twenty-five | twenty-six | twenty-seven | twenty-eight | twenty-nine | thirty |

1 st 对角印花:{1} 2 nd 对角印花:{2,7} 3 rd 对角印花:{13,8,3} …… 5 th 对角印花{25,20,15}。 由于遇到 15,因此不再打印矩阵元素。

输入: n = 4,m = 3,k = 69,arr[][]={{4,87,24}, {17,1,18}, {25,69,97}, {19,27,85}} 输出: 4,87,17,25,1,24,18,69

方法:按照以下步骤解决问题:

  1. 矩阵中的对角线总数为n m–1
  2. 以螺旋方式逐个穿过对角线。
  3. 对于遍历的每个元素,检查是否等于 k 。如果发现是真的,打印该元素并终止。
  4. 否则,打印元素并计算要遍历的下一个索引。如果 ij 是当前指数:
    • 沿对角线向上移动时,i 将减少,而 j 将增加。
    • 对角向下移动时,i 将增加,而 j 将减少。
  5. 如果下一个索引不是有效的索引,则移动到下一个对角线。
  6. 否则,将当前位置更新到下一个位置。

下面是上述方法的实现:

c

// c   program for the above approach
#include 
using namespace std;
// function to check if the
// indices are valid or not
bool isvalid(int i, int j,
            int n, int m)
{
    return (i >= 0 && i < n
            && j >= 0 && j < m);
}
// function to evaluate the next
// index while moving diagonally up
pair up(int i, int j,
                int n, int m)
{
    if (isvalid(i - 1, j   1, n, m))
        return { i - 1, j   1 };
    else
        return { -1, -1 };
}
// function to evaluate the next
// index while moving diagonally down
pair down(int i, int j,
                    int n, int m)
{
    if (isvalid(i   1, j - 1, n, m))
        return { i   1, j - 1 };
    else
        return { -1, -1 };
}
// function to print matrix elements
// diagonally in spiral form
void spiraldiagonal(int n, int m, int k,
                    vector > a)
{
    int i = 0, j = 0;
    // total number of diagonals
    // = n   m - 1
    for (int diagonal = 0;
        diagonal < n   m - 1;
        diagonal  ) {
        while (1) {
            // stop when k is
            // encountered
            if (a[i][j] == k) {
                cout << k;
                return;
            }
            // print the integer
            cout << a[i][j] << ", ";
            // store the next index
            pair next;
            if (diagonal & 1) {
                next = down(i, j, n, m);
            }
            else {
                next = up(i, j, n, m);
            }
            // if current index is invalid
            if (next.first == next.second
                && next.first == -1) {
                // move to the next diagonal
                if (diagonal & 1) {
                    (i   1 < n) ?   i :   j;
                }
                else {
                    (j   1 < m) ?   j :   i;
                }
                break;
            }
            // otherwise move to the
            // next valid index
            else {
                i = next.first;
                j = next.second;
            }
        }
    }
}
// driver code
int main()
{
    int n = 5, m = 6, k = 15;
    vector > arr
        = { { 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, 26, 27, 28, 29, 30 } };
    // function call
    spiraldiagonal(n, m, k, arr);
    return 0;
}

java 语言(一种计算机语言,尤用于创建网站)

// java program for the
// above approach
import java.util.*;
import java.lang.*;
class gfg{
static class pair
{
    int first, second;
    pair(int f, int s)
    {
        this.first = f;
        this.second = s;
    }
}
// function to check if the
// indices are valid or not
static boolean isvalid(int i, int j,
                       int n, int m)
{
    return (i >= 0 && i < n &&
            j >= 0 && j < m);
}
// function to evaluate the next
// index while moving diagonally up
static int[] up(int i, int j,
                int n, int m)
{
    if (isvalid(i - 1, j   1, n, m))
        return new int[]{ i - 1, j   1 };
    else
        return new int[]{ -1, -1 };
}
// function to evaluate the next
// index while moving diagonally down
static int[] down(int i, int j,
                  int n, int m)
{
    if (isvalid(i   1, j - 1, n, m))
        return new int[]{ i   1, j - 1 };
    else
        return new int[]{ -1, -1 };
}
// function to print matrix elements
// diagonally in spiral form
static void spiraldiagonal(int n, int m,
                           int k, int[][] a)
{
    int i = 0, j = 0;
    // total number of diagonals
    // = n   m - 1
    for(int diagonal = 0;
            diagonal < n   m - 1;
            diagonal  )
    {
        while (true)
        {
            // stop when k is
            // encountered
            if (a[i][j] == k)
            {
                system.out.print(k);
                return;
            }
            // print the integer
            system.out.print(a[i][j]   ", ");
            // store the next index
            int[] next;
            if ((diagonal & 1) == 1)
            {
                next = down(i, j, n, m);
            }
            else
            {
                next = up(i, j, n, m);
            }
            // if current index is invalid
            if (next[0] == next[1] &&
                next[1] == -1)
            {
                // move to the next diagonal
                if ((diagonal & 1) == 1)
                {
                    if (i   1 < n)
                          i;
                    else
                          j;
                }
                else
                {
                    if (j   1 < m)
                          j;
                    else
                          i;
                }
                break;
            }
            // otherwise move to the
            // next valid index
            else
            {
                i = next[0];
                j = next[1];
            }
        }
    }
}
// driver code
public static void main (string[] args)
{
    int n = 5, m = 6, k = 15;
    int[][] arr = { { 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, 26, 27, 28, 29, 30 } };
    // function call
    spiraldiagonal(n, m, k, arr);
}
}
// this code is contributed by offbeat

python 3

# python3 program for the
# above approach
# function to check if the
# indices are valid or not
def isvalid(i, j, n, m):
    return (i >= 0 and i < n and j >= 0 and j < m)
# function to evaluate the next
# index while moving diagonally up
def up(i, j, n, m):
    if(isvalid(i - 1, j   1, n, m)):
        return [i - 1, j   1 ]
    else:
        return [-1, -1]
# function to evaluate the next
# index while moving diagonally down
def down(i, j, n, m):
    if(isvalid(i   1, j - 1, n, m)):
        return [i   1, j - 1 ]
    else:
        return [-1, -1]
# function to print matrix elements
# diagonally in spiral form
def spiraldiagonal(n, m, k, a):
    i = 0
    j = 0
    # total number of diagonals
    # = n   m - 1
    for diagonal in range(n   m - 1):
        while(true):
            # stop when k is
            # encountered
            if(a[i][j] == k):
                print(k, end = "")
                return
            # print the integer
            print(a[i][j], ", ", end="", sep="")
            # store the next index
            next = []
            if((diagonal & 1) == 1):
                next = down(i, j, n, m)
            else:
                next = up(i, j, n, m)
            # if current index is invalid
            if(next[0] == next[1] and next[1] == -1):
                # move to the next diagonal
                if((diagonal & 1) == 1):
                    if(i   1 < n):
                        i  = 1
                    else:
                        j  = 1
                else:
                    if(j   1 < m):
                        j  = 1
                    else:
                        i  = 1
                break
            # otherwise move to the
            # next valid index
            else:
                i = next[0]
                j = next[1]
# driver code
n = 5
m = 6
k = 15
arr = [[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, 26, 27, 28, 29, 30]]
# function call
spiraldiagonal(n, m, k, arr);
# this code is contributed by avanitrachhadiya2155

c

// c# program for the
// above approach
using system;
class gfg{
// function to check if the
// indices are valid or not
static bool isvalid(int i, int j,
                    int n, int m)
{
    return (i >= 0 && i < n &&
            j >= 0 && j < m);
}
// function to evaluate the next
// index while moving diagonally up
static int[] up(int i, int j, int n, int m)
{
    if (isvalid(i - 1, j   1, n, m))
        return new int[]{ i - 1, j   1 };
    else
        return new int[]{ -1, -1 };
}
// function to evaluate the next
// index while moving diagonally down
static int[] down(int i, int j, int n, int m)
{
    if (isvalid(i   1, j - 1, n, m))
        return new int[]{ i   1, j - 1 };
    else
        return new int[]{ -1, -1 };
}
// function to print matrix elements
// diagonally in spiral form
static void spiraldiagonal(int n, int m, int k,
                           int[, ] a)
{
    int i = 0, j = 0;
    // total number of diagonals
    // = n   m - 1
    for(int diagonal = 0;
            diagonal < n   m - 1;
            diagonal  )
    {
        while (true)
        {
            // stop when k is
            // encountered
            if (a[i, j] == k)
            {
                console.write(k);
                return;
            }
            // print the integer
            console.write(a[i, j]   ", ");
            // store the next index
            int[] next;
            if ((diagonal & 1) == 1)
            {
                next = down(i, j, n, m);
            }
            else
            {
                next = up(i, j, n, m);
            }
            // if current index is invalid
            if (next[0] == next[1] &&
                next[1] == -1)
            {
                // move to the next diagonal
                if ((diagonal & 1) == 1)
                {
                    if (i   1 < n)
                          i;
                    else
                          j;
                }
                else
                {
                    if (j   1 < m)
                          j;
                    else
                          i;
                }
                break;
            }
            // otherwise move to the
            // next valid index
            else
            {
                i = next[0];
                j = next[1];
            }
        }
    }
}
// driver code
public static void main(string[] args)
{
    int n = 5, m = 6, k = 15;
    int[, ] arr = { { 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, 26, 27, 28, 29, 30 } };
    // function call
    spiraldiagonal(n, m, k, arr);
}
}
// this code is contributed by grand_master

java 描述语言


输出:

1, 2, 7, 13, 8, 3, 4, 9, 14, 19, 25, 20, 15

时间复杂度: o(nm)* 辅助空间: o(1)