原文:

给定一个数字 n,我们需要打印所有由手机键盘形成的 n 个数字模式。 注意:我们可以从手机小键盘的任意一个按键上、下、左、右移动,每个图案都包含唯一的按键。

示例:

input :   n = 3   
output :  all 3 digit pattern are : 
           123, 125, 145, 147 
           236, 214, 258, 256, 254
           321, 325, 369, 365 
           412, 456, 452, 458, 478
           and so on ..

这个pg电子试玩链接的解决方案的想法是基于 dfs 的。我们一个接一个地选择所有键盘按键作为 n 位数的起始数字,之后,我们试图生成由该按键形成的所有 n 位数模式(使用 dfs,因为我们只能从该按键向上、向左、向右或向下移动)。

 printpattern function (dfs function) 
  add current key to pattern
  pattern  = keypad[x][y]
 .... make current key as visited 
  visited[x][y] = true;
 ... print pattern if size of pattern == n 
 call dfs for all 4 adjacent keypad key 
 __dfs_function

下面是上述想法的实现:

c

// c   program to print all n digit patterns
// formed by mobile keypad.
#include 
using namespace std;
// a function to check if a given cell (row, col)
// can be included in dfs
bool issafe(int x, int y, bool visited[][3])
{
    // row number is in range, column number
    // is in range and not yet visited
    return (x >= 0 && x < 4 && y >= 0 &&
            y < 3 && !visited[x][y]);
}
// a utility function to do dfs for a mobile keypad
// matrix. it only considers the 4 neighbors as
// adjacent vertice and print pattern of size n
void dfs(bool visited[][3], int keypad[][3], int n,
         string pattern, int x, int y)
{
    // add current number to string
    pattern.push_back((keypad[x][y]   '0'));
    // print pattern
    if (pattern.size() == n) {
        cout << pattern << " ";
        return;
    }
    // these arrays are used to get row and
    // column
    // numbers of 4 neighbours of a given cell
    static int row[] = { 0, 1, 0, -1 };
    static int col[] = { 1, 0, -1, 0 };
    // mark this cell as visited
    visited[x][y] = true;
    // recur for all connected neighbours
    for (int k = 0; k < 4; k  )
        if (issafe(x   row[k], y   col[k], visited)
            && keypad[x   row[k]][y   col[k]] != -1)
            dfs(visited, keypad, n, pattern,
                       x   row[k], y   col[k]);
    // unvisited
    visited[x][y] = false;
    pattern.pop_back();
}
void patternofsizek(int keypad[][3], int n)
{
    // make a bool array to mark visited cells.
    // initially all cells are unvisited
    bool visited[4][3];
    memset(visited, false, sizeof(visited));
    // try to generate all pattern of size n
    // by making every key of keypad as
    // starting char of pattern
    for (int i = 0; i < 4; i  )
        for (int j = 0; j < 3; j  )
            if (keypad[i][j] != -1)
                dfs(visited, keypad, n, "", i, j);
}
// drive program to test above function.
int main()
{
    int keypad[4][3] = { { 1, 2, 3 },
                         { 4, 5, 6 },
                         { 7, 8, 9 },
                         { -1, 0, -1 } };
    int n = 3;
    patternofsizek(keypad, n);
    return 0;
}

java 语言(一种计算机语言,尤用于创建网站)

// java program to print all n digit patterns
// formed by mobile keypad.
public class main
{
    // a function to check if a given cell (row, col)
    // can be included in dfs
    static boolean issafe(int x, int y, boolean[][] visited)
    {
        // row number is in range, column number
        // is in range and not yet visited
        return (x >= 0 && x < 4 && y >= 0 &&
                y < 3 && !visited[x][y]);
    }
    // a utility function to do dfs for a mobile keypad
    // matrix. it only considers the 4 neighbors as
    // adjacent vertice and print pattern of size n
    static void dfs(boolean[][] visited, int[][] keypad, int n,
             string pattern, int x, int y)
    {
        // add current number to string
        pattern = pattern   integer.tostring(keypad[x][y]);
        // print pattern
        if (pattern.length() == n) {
            system.out.print(pattern   " ");
            return;
        }
        // these arrays are used to get row and
        // column
        // numbers of 4 neighbours of a given cell
        int[] row = { 0, 1, 0, -1 };
        int[] col = { 1, 0, -1, 0 };
        // mark this cell as visited
        visited[x][y] = true;
        // recur for all connected neighbours
        for (int k = 0; k < 4; k  )
            if (issafe(x   row[k], y   col[k], visited)
                && keypad[x   row[k]][y   col[k]] != -1)
                dfs(visited, keypad, n, pattern,
                           x   row[k], y   col[k]);
        // unvisited
        visited[x][y] = false;
        pattern = pattern.substring(0, pattern.length() - 1);
    }
    static void patternofsizek(int[][] keypad, int n)
    {
        // make a bool array to mark visited cells.
        // initially all cells are unvisited
        boolean[][] visited = new boolean[4][3];
        for (int i = 0; i < 4; i  )
        {
            for (int j = 0; j < 3; j  )
            {
                visited[i][j] = false;
            }
        }
        // try to generate all pattern of size n
        // by making every key of keypad as
        // starting char of pattern
        for (int i = 0; i < 4; i  )
        {
            for (int j = 0; j < 3; j  )
            {
                if (keypad[i][j] != -1)
                    dfs(visited, keypad, n, "", i, j);
            }
        }
    }
  // driver code
    public static void main(string[] args) {
        int[][] keypad = { { 1, 2, 3 },
                         { 4, 5, 6 },
                         { 7, 8, 9 },
                         { -1, 0, -1 } };
        int n = 3;
        patternofsizek(keypad, n);
    }
}
// this code is contributed by divyesh072019.

python 3

# python3 program to print all n digit patterns
# formed by mobile keypad.
# a function to check if a given cell
# (row, col) can be included in dfs
def issafe(x, y, visited):
    # row number is in range, column number
    # is in range and not yet visited
    return (x >= 0 and x < 4 and
            y >= 0 and y < 3 and not visited[x][y])
# a utility function to do dfs for a mobile keypad
# matrix. it only considers the 4 neighbors as
# adjacent vertice and print pattern of size n
def dfs(visited, keypad, n, pattern, x, y):
    # add current number to string
    pattern = pattern   str(keypad[x][y])
    # print pattern
    if (len(pattern) == n):
        print(pattern, end = ' ')
        return
    # these arrays are used to get row and
    # column
    # numbers of 4 neighbours of a given cell
    row = [ 0, 1, 0, -1 ]
    col = [ 1, 0, -1, 0 ]
    # mark this cell as visited
    visited[x][y] = true
    # recur for all connected neighbours
    for k in range(0, 4):
        if (issafe(x   row[k],
                   y   col[k], visited) and
            keypad[x   row[k]][y   col[k]] != -1):
            dfs(visited, keypad, n, pattern,
                x   row[k], y   col[k])
    # unvisited
    visited[x][y] = false;
    pattern = pattern[:-1]
def patternofsizek(keypad, n):
    # make a bool array to mark visited cells.
    # initially all cells are unvisited
    visited = [[false for i in range(3)]
                      for j in range(4)]
    # try to generate all pattern of size n
    # by making every key of keypad as
    # starting char of pattern
    for i in range(4):
        for j in range(3):
            if (keypad[i][j] != -1):
                dfs(visited, keypad, n,
                    "", i, j)
# drive code
if __name__=='__main__':
    keypad = [ [ 1, 2, 3 ],
               [ 4, 5, 6 ],
               [ 7, 8, 9 ],
               [ -1, 0, -1 ] ]
    n = 3
    patternofsizek(keypad, n)
# this code is contributed by rutvik_56

c

// c# program to print all n digit patterns
// formed by mobile keypad.
using system;
class gfg {
    // a function to check if a given cell (row, col)
    // can be included in dfs
    static bool issafe(int x, int y, bool[,] visited)
    {
        // row number is in range, column number
        // is in range and not yet visited
        return (x >= 0 && x < 4 && y >= 0 &&
                y < 3 && !visited[x,y]);
    }
    // a utility function to do dfs for a mobile keypad
    // matrix. it only considers the 4 neighbors as
    // adjacent vertice and print pattern of size n
    static void dfs(bool[,] visited, int[,] keypad, int n,
             string pattern, int x, int y)
    {
        // add current number to string
        pattern = pattern   (keypad[x,y]).tostring();
        // print pattern
        if (pattern.length == n) {
            console.write(pattern   " ");
            return;
        }
        // these arrays are used to get row and
        // column
        // numbers of 4 neighbours of a given cell
        int[] row = { 0, 1, 0, -1 };
        int[] col = { 1, 0, -1, 0 };
        // mark this cell as visited
        visited[x,y] = true;
        // recur for all connected neighbours
        for (int k = 0; k < 4; k  )
            if (issafe(x   row[k], y   col[k], visited)
                && keypad[x   row[k],y   col[k]] != -1)
                dfs(visited, keypad, n, pattern,
                           x   row[k], y   col[k]);
        // unvisited
        visited[x,y] = false;
        pattern = pattern.substring(0, pattern.length - 1);
    }
    static void patternofsizek(int[,] keypad, int n)
    {
        // make a bool array to mark visited cells.
        // initially all cells are unvisited
        bool[,] visited = new bool[4,3];
        for (int i = 0; i < 4; i  )
        {
            for (int j = 0; j < 3; j  )
            {
                visited[i,j] = false;
            }
        }
        // try to generate all pattern of size n
        // by making every key of keypad as
        // starting char of pattern
        for (int i = 0; i < 4; i  )
        {
            for (int j = 0; j < 3; j  )
            {
                if (keypad[i,j] != -1)
                    dfs(visited, keypad, n, "", i, j);
            }
        }
    }
  static void main() {
    int[,] keypad = { { 1, 2, 3 },
                         { 4, 5, 6 },
                         { 7, 8, 9 },
                         { -1, 0, -1 } };
    int n = 3;
    patternofsizek(keypad, n);
  }
}
// this code is contributed by suresh07.

java 描述语言


输出:

123 125 145 147 236 256 258 254 214 369 365 325 321 
456 458 452 478 412 569 563 589 580 587 547 541 523 521 
698 658 654 652 632 789 780 785 745 741 896 874 856 854 852 
980 987 985 965 963 089 087 085