I am have a hard time coming up with a solution for an algorithm on hackerrank,
The problem
The following diagram is of a standard telephone keypad. It consists of a 4x3 grid of buttons. Using the valid moves of a piece from the game of chess, varying combinations of 7-digit phone numbers can be derived. For example, starting in the upper-right corner (the "3" key) using a rook (which moves any number of spaces horizontally or vertically), one valid number, after pressing the initial "3" key, is: 314-5289
1 2 3
4 5 6
7 8 9
* 0 #
Write a program that will count the number of valid 7-digit phone numbers that can be traced out on the keypad for every given chess piece. The following rules define a valid phone number: Seven digits in length Cannot start with a 0 or 1 Cannot contain a * or #
What i have tried so far
function find_positions_to_move(board, partial, row, col) {
rows = [-1, -2, 1, 2, 0, 0, 0, 0];
cols = [0, 0, 0, 0,-1,-2, 1, 2];
}
positions = [];
for (i in range(len(rows))) {
if (3 > (row + rows[i]) >= 0 && 3 > (col + cols[i]) >= 0) {
positions.push(((row + rows[i]), (col + cols[i])));
}
return positions;
}
function recursively_generate(board, i, j, partial_result, max_length, result) {
// if size if partial result is equal to expected length, then add partial result to final list of results and return
if (max_length == len(partial_result)) {
result.push(partial_result);
return;
}
positions = find_positions_to_move(board, partial_result, i, j);
// for each possible position add it to partial result
for (x, y in positions) {
recursively_generate(board, x, y, partial_result + String(board[x][y]), length, result);
}
function generate_phone_number(board, start_i, start_j) {
result = [];
max_length = 7;
}
// start with position start_i and start_j
recursively_generate(board, start_i, start_j, String(board[start_i][start_j]), max_length, result);
Any tips on solving this would be highly appreciated. Thanks in advance