给我们一个无限的二维平面,由连接在一起的六边形组成。我们可以将这个平面想象成一个蜂窝。元素 X 存在于单元格/六边形之一上。
我们有 N 步,任务是计算可能的这种六边形路径的数量,其中元素 X 必须执行 N 步行走并返回到原始六边形,其中
例子:
Input : 1
Output : Number of walks possible is/are 0
Explanation :
0 because using just one step we can move to
any of the adjacent cells but we cannot trace
back to the original hexagon.
Input : 2
Output : Number of walks possible is/are 6
Input : 4
Output : Number of walks possible is/are 90
方法 :
- 六边形行走可以定义为穿过相邻的六边形并返回到原始单元格。我们知道一个六边形包含六个边的事实,即一个六边形被六个六边形包围。现在,我们必须计算我们采取 N 步并返回到原始六边形的方式的数量。
- 现在,让我们假设原始六边形(元素 X 最初存在的地方)是原点。我们需要所有可能的方法来采取(Nk)步,这样我们就有一些可以追溯到我们原始六边形的步骤。我们可以从下图中可视化这个六边形及其相关坐标系。
- 现在,让我们假设,我们的元素 X 出现在给定图片的 0:0 处。因此,我们可以从六边形沿六个可能的方向行进。现在,使用上面的方向,我们记住所有可能的运动,以便我们追溯到原始 0:0 索引。为了记忆,我们使用 3D 数组,我们对给定数量的步骤的答案进行预处理,然后进行相应的查询。
以下是上述方法的实现:
C++
// C++ implementation of counting
// number of possible hexagonal walks
#include
using namespace std;
int depth = 16;
int ways[16][16][16];
int stepNum;
void preprocess(int list[])
{
// We initialize our origin with 1
ways[0][8][8] = 1;
// For each N = 1 to 14, we traverse in all possible
// direction. Using this 3D array we calculate the
// number of ways at each step and the total ways
// for a given step shall be found at
// ways[step number][8][8] because all the steps
// after that will be used to trace back to the
// original point index 0:0 according to the image.
for (int N = 1; N <= 14; N++)
{
for (int i = 1; i <= depth; i++)
{
for (int j = 1; j <= depth; j++)
{
ways[N][i][j] = ways[N - 1][i][j + 1]
+ ways[N - 1][i][j - 1]
+ ways[N - 1][i + 1][j]
+ ways[N - 1][i - 1][j]
+ ways[N - 1][i + 1][j - 1]
+ ways[N - 1][i - 1][j + 1];
}
}
// This array stores the number of ways
// possible for a given step
list[N] = ways[N][8][8];
}
}
// Driver function
int main()
{
int list[15];
// Preprocessing all possible ways
preprocess(list);
int steps = 4;
cout << "Number of walks possible is/are "
<< list[steps] << endl;
return 0;
}
Java
// Java implementation of counting
// number of possible hexagonal walks
import java.util.*;
class GFG {
static int depth = 14;
static int ways[][][] = new int[16][16][16];
static int stepNum;
static void preprocess(int list[])
{
// We initialize our origin with 1
ways[0][8][8] = 1;
// For each N = 1 to 14, we traverse in
// all possible direction. Using this 3D
// array we calculate the number of ways
// at each step and the total ways for a
// given step shall be found at ways[step
// number][8][8] because all the steps
// after that will be used to trace back
// to the original point index 0:0
// according to the image.
for (int N = 1; N <= 14; N++)
{
for (int i = 1; i < depth; i++)
{
for (int j = 1; j < depth; j++)
{
ways[N][i][j] =
ways[N - 1][i][j + 1]
+ ways[N - 1][i][j - 1]
+ ways[N - 1][i + 1][j]
+ ways[N - 1][i - 1][j]
+ ways[N - 1][i + 1][j - 1]
+ ways[N - 1][i - 1][j + 1];
}
}
// This array stores the number of
// ways possible for a given step
list[N] = ways[N][8][8];
}
}
/* Driver program to test above function */
public static void main(String[] args)
{
int list[] = new int[15];
// Preprocessing all possible ways
preprocess(list);
int steps = 4;
System.out.println( "Number of walks"
+ " possible is/are "+
list[steps] );
}
}
Python3
# Python 3 implementation of counting
# number of possible hexagonal walks
depth = 16
ways = [[[0 for i in range(17)]
for i in range(17)]
for i in range(17)]
def preprocess(list, steps):
# We initialize our origin with 1
ways[0][8][8] = 1
# For each N = 1 to 14, we traverse in
# all possible direction. Using this 3D
# array we calculate the number of ways
# at each step and the total ways for a
# given step shall be found at ways[step
# number][8][8] because all the steps after
# that will be used to trace back to the
# original point index 0:0 according to the image.
for N in range(1, 16, 1):
for i in range(1, depth, 1):
for j in range(1, depth, 1):
ways[N][i][j] = (ways[N - 1][i][j + 1] +
ways[N - 1][i][j - 1] +
ways[N - 1][i + 1][j] +
ways[N - 1][i - 1][j] +
ways[N - 1][i + 1][j - 1] +
ways[N - 1][i - 1][j + 1])
# This array stores the number of ways
# possible for a given step
list[N] = ways[N][8][8]
print("Number of walks possible is/are",
list[steps])
# Driver Code
if __name__ == '__main__':
list = [0 for i in range(16)]
steps = 4
# Preprocessing all possible ways
preprocess(list, steps)
# This code is contributed by
# Surendra_Gangwar
C#
// C# implementation of counting
// number of possible hexagonal walks
using System;
class GFG {
static int depth = 14;
static int [, ,]ways = new int[16,16,16];
// static int stepNum;
static void preprocess(int []list)
{
// We initialize our origin with 1
ways[0,8,8] = 1;
// For each N = 1 to 14, we traverse in
// all possible direction. Using this 3D
// array we calculate the number of ways
// at each step and the total ways for a
// given step shall be found at ways[step
// number][8][8] because all the steps
// after that will be used to trace back
// to the original point index 0:0
// according to the image.
for (int N = 1; N <= 14; N++)
{
for (int i = 1; i < depth; i++)
{
for (int j = 1; j < depth; j++)
{
ways[N,i,j] =
ways[N - 1,i,j + 1]
+ ways[N - 1,i,j - 1]
+ ways[N - 1,i + 1,j]
+ ways[N - 1,i - 1,j]
+ ways[N - 1,i + 1,j - 1]
+ ways[N - 1,i - 1,j + 1];
}
}
// This array stores the number of
// ways possible for a given step
list[N] = ways[N,8,8];
}
}
/* Driver program to test above function */
public static void Main()
{
int []list = new int[15];
// Preprocessing all possible ways
preprocess(list);
int steps = 4;
Console.WriteLine( "Number of walks"
+ " possible is/are "+
list[steps] );
}
}
// This code is contributed by anuj_67.
Javascript
输出 :
Number of walks possible is/are 90
上述代码的时间复杂度为由于使用了 3D 阵列,空间复杂度也相似。
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。