Mobile Numeric Keypad Problem | Set 2 | C ++ implementation of the approach ; Function to return the count of numbers possible ; Array of list storing possible direction for each number from 0 to 9 mylist [ i ] stores possible moves from index i ; Initializing list ; Storing values for n = 1 ; To store the values for n = i ; Loop to iterate through each index ; For each move possible from j Increment the value of possible move positions by Arr [ j ] ; Update Arr [ ] for next iteration ; Find the count of numbers possible ; Driver code
#include <iostream> NEW_LINE #include <list> NEW_LINE using namespace std ; #define MAX  10 NEW_LINE int getCount ( int n ) { list < int > mylist [ MAX ] ; mylist [ 0 ] . assign ( { 0 , 8 } ) ; mylist [ 1 ] . assign ( { 1 , 2 , 4 } ) ; mylist [ 2 ] . assign ( { 2 , 1 , 3 , 5 } ) ; mylist [ 3 ] . assign ( { 3 , 6 , 2 } ) ; mylist [ 4 ] . assign ( { 4 , 1 , 7 , 5 } ) ; mylist [ 5 ] . assign ( { 5 , 4 , 6 , 2 , 8 } ) ; mylist [ 6 ] . assign ( { 6 , 3 , 5 , 9 } ) ; mylist [ 7 ] . assign ( { 7 , 4 , 8 } ) ; mylist [ 8 ] . assign ( { 8 , 5 , 0 , 7 , 9 } ) ; mylist [ 9 ] . assign ( { 9 , 6 , 8 } ) ; int Arr [ MAX ] = { 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 } ; for ( int i = 2 ; i <= n ; i ++ ) { int Arr2 [ MAX ] = { 0 } ; for ( int j = 0 ; j < MAX ; j ++ ) { for ( int x : mylist [ j ] ) { Arr2 [ x ] += Arr [ j ] ; } } for ( int j = 0 ; j < MAX ; j ++ ) Arr [ j ] = Arr2 [ j ] ; } int sum = 0 ; for ( int i = 0 ; i < MAX ; i ++ ) sum += Arr [ i ] ; return sum ; } int main ( ) { int n = 2 ; cout << getCount ( n ) ; return 0 ; }