📌  相关文章
📜  根据给定条件遍历矩阵的方法计数

📅  最后修改于: 2021-09-06 05:30:20             🧑  作者: Mango

给定一个表示N x N方阵的整数N ,任务是通过以下条件打印从方阵左上角到右下角的移动方式的数量:

  • 如果指针的当前位置在方阵的边缘,那么下一步可以是垂直或水平移动,任意步数(就像棋盘上的)。
  • 如果指针的当前位置在方阵的对角线上,那么下一步也应该在对角线上。 (就像棋盘上的主教)。
  • 如果指针的当前位置在上述两个位置之外的任何其他位置,那么下一步可能的步骤可以是任何方式,就像一个骑士在棋盘上移动,但新位置的行和列>旧位置的行和列位置。

例子:

方法:这个想法是递归检查每种可能的情况是否到达方阵的末尾。为此,定义了一个递归函数,它将当前位置和最后一个位置作为输入参数。以下是递归函数的条件:

  • Base Case:函数的基本情况是检查当前指针是否到达方阵右下角位置。如果达到,则记录总路数的计数器计数增加。如果无法到达最后一个位置,则应停止该函数,并且不应为下一次迭代调用该函数。这是实现为:
if (cr == er && cc == ec) {
    count++;
    return;
}

if (cr > er || cc > ec) {
   return;
}
  • 递归案例:鉴于三种类型的遍历是可能的。因此,递归情况是通过检查当前位置是否递归调用函数。如果当前位置在方阵的边缘,则指针只能水平或垂直移动。并且对于每个后续的垂直遍历,水平和垂直遍历的另外两种选择是可能的。因此,为了跟踪总路数,这两种方法都在循环中递归调用。
for (int i = 1; i <= er; i++) {
    if (cc == 0 || cr == 0 
       || cr == er || cc == ec) {
        chessboard1(cr, cc + i, er, ec);
    }
}

for (int i = 1; i <= er; i++) {
    if (cc == 0 || cr == 0 
       || cr == er || cc == ec) {
        chessboard1(cr + i, cc, er, ec);
    }
}
  • 除此之外,如果当前指针在对角线上,则指针可以对角移动。然而,对于每个后续对角线遍历,另一组后续对角线是可能的。因此,为了跟踪总路数,使用 for 循环递归调用函数。
for (int i = 1; i <= er; i++) {
    if (cr == cc || cr + cc == er) {
        chessboard1(cr + i, cc + i,
                    er, ec);
    }
}
  • 如果以上情况都不满足,则可以通过以下方式移动下一个位置:
    • 新行是 > 旧行。
    • 新列 > 旧列。
  • 执行完上述函数, count变量中存储的值为遍历方阵的总路数。

下面是上述方法的实现:

C++
// C++ program to count the number
// of ways to traverse the given
// N x N Square Matrix recursively
#include
using namespace std;
   
// Variable to store the
// total number of ways to
// traverse the Square Matrix
     
// Function to recursively
// count the total number
// of ways to traverse the
// given N x N Square Matrix
void chessboard1(int cr, int cc,
                 int er, int ec,
                 int& count)
{
 
  // If the last index has been
  // reached, then the count is
  // incremented and returned
  if (cr == er && cc == ec)
  {
    count++;
    return;
  }
 
  // If the last index cannot
  // be reached
  if (cr > er || cc > ec)
  {
    return;
  }
 
  // If the current position is
  // neither on the edges nor
  // on the diagonal, then the
  // pointer moves like a knight
  chessboard1(cr + 2, cc + 1,
              er, ec,count);
  chessboard1(cr + 1, cc + 2,
              er, ec,count);
 
  // If the pointer is on the
  // edges of the Square Matrix
  // next move can be horizontal
  // or vertical
 
  // This for loop is used to include
  // all the horizontal traversal cases
  // recursively
  for (int i = 1; i <= er; i++)
  {
    if (cc == 0 || cr == 0||
        cr == er || cc == ec)
    {
      chessboard1(cr, cc + i,
                  er, ec,count);
    }
  }
 
  // This for loop is used to include
  // all the vertical traversal cases
  // recursively
  for (int i = 1; i <= er; i++)
  {
    if (cc == 0 || cr == 0||
        cr == er || cc == ec)
    {
      chessboard1(cr + i, cc,
                  er, ec,count);
    }
  }
 
  // If the pointer is on the
  // diagonal of the Square Matrix
  // next move is also diagonal.
  // For loop is used to include
  // all the diagonal traversal cases.
  for (int i = 1; i <= er; i++)
  {
    if (cr == cc || cr + cc == er)
    {
      chessboard1(cr + i,  cc + i, 
                  er, ec, count);
    }
  }
}
   
// Driver code
int main()
{
  int N = 3;
  int count=0;
  chessboard1(0, 0, N - 1, N - 1,count);
  cout<


Java
// Java program to count the number
// of ways to traverse the given
// N x N Square Matrix recursively
 
public class GFG {
 
    // Variable to store the total number
    // of ways to traverse the Square Matrix
    static int count = 0;
 
    // Function to recursively
    // count the total number
    // of ways to traverse the
    // given N x N Square Matrix
    public static void chessboard1(
        int cr, int cc,
        int er, int ec)
    {
 
        // If the last index has been reached, then
        // the count is incremented and returned
        if (cr == er && cc == ec) {
            count++;
            return;
        }
 
        // If the last index cannot be reached
        if (cr > er || cc > ec) {
            return;
        }
 
        // If the current position is neither
        // on the edges nor on the diagonal,
        // then the pointer moves
        // like a knight
        chessboard1(cr + 2, cc + 1, er, ec);
        chessboard1(cr + 1, cc + 2, er, ec);
 
        // If the pointer is on the
        // edges of the Square Matrix
        // next move can be horizontal or vertical
 
        // This for loop is used to include all the
        // horizontal traversal cases recursively
        for (int i = 1; i <= er; i++) {
            if (cc == 0 || cr == 0
                || cr == er || cc == ec) {
                chessboard1(cr, cc + i, er, ec);
            }
        }
 
        // This for loop is used to include all the
        // vertical traversal cases recursively
        for (int i = 1; i <= er; i++) {
            if (cc == 0 || cr == 0
                || cr == er || cc == ec) {
                chessboard1(cr + i, cc, er, ec);
            }
        }
 
        // If the pointer is on the
        // diagonal of the Square Matrix
        // next move is also diagonal.
        // For loop is used to include
        // all the diagonal traversal cases.
        for (int i = 1; i <= er; i++) {
            if (cr == cc
                || cr + cc == er) {
 
                chessboard1(cr + i,
                            cc + i,
                            er, ec);
            }
        }
    }
 
    // Driver code
    public static void main(String[] args)
    {
 
        int N = 3;
        chessboard1(0, 0, N - 1, N - 1);
        System.out.println(count);
    }
}


C#
// C# program to count the number
// of ways to traverse the given
// N x N Square Matrix recursively
using System;
 
class GFG{
 
// Variable to store the total number
// of ways to traverse the Square Matrix
static int count = 0;
 
// Function to recursively
// count the total number
// of ways to traverse the
// given N x N Square Matrix
public static void chessboard1(int cr, int cc,
                               int er, int ec)
{
     
    // If the last index has been reached, then
    // the count is incremented and returned
    if (cr == er && cc == ec)
    {
        count++;
        return;
    }
 
    // If the last index cannot be reached
    if (cr > er || cc > ec)
    {
        return;
    }
 
    // If the current position is neither
    // on the edges nor on the diagonal,
    // then the pointer moves
    // like a knight
    chessboard1(cr + 2, cc + 1, er, ec);
    chessboard1(cr + 1, cc + 2, er, ec);
 
    // If the pointer is on the edges
    // of the Square Matrix next move
    // can be horizontal or vertical
 
    // This for loop is used to include all the
    // horizontal traversal cases recursively
    for(int i = 1; i <= er; i++)
    {
        if (cc == 0 || cr == 0 ||
           cr == er || cc == ec)
        {
            chessboard1(cr, cc + i, er, ec);
        }
    }
     
    // This for loop is used to include all the
    // vertical traversal cases recursively
    for(int i = 1; i <= er; i++)
    {
        if (cc == 0 || cr == 0 ||
           cr == er || cc == ec)
        {
            chessboard1(cr + i, cc, er, ec);
        }
    }
     
    // If the pointer is on the
    // diagonal of the Square Matrix
    // next move is also diagonal.
    // For loop is used to include
    // all the diagonal traversal cases.
    for(int i = 1; i <= er; i++)
    {
        if (cr == cc || cr + cc == er)
        {
            chessboard1(cr + i, cc + i,
                        er, ec);
        }
    }
}
 
// Driver code
public static void Main(string[] args)
{
    int N = 3;
     
    chessboard1(0, 0, N - 1, N - 1);
     
    Console.Write(count);
}
}
 
// This code is contributed by rutvik_56


Javascript


输出 :

18

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live