Count of possible hexagonal walks | Java implementation of counting number of possible hexagonal walks ; We initialize our origin with 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 . ; This array stores the number of ways possible for a given step ; Driver program to test above function ; Preprocessing all possible ways
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 [ ] ) { ways [ 0 ] [ 8 ] [ 8 ] = 1 ; 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 ] ; } } list [ N ] = ways [ N ] [ 8 ] [ 8 ] ; } } public static void main ( String [ ] args ) { int list [ ] = new int [ 15 ] ; preprocess ( list ) ; int steps = 4 ; System . out . println ( " Number ▁ of ▁ walks " + " ▁ possible ▁ is / are ▁ " + list [ steps ] ) ; } }