📌  相关文章
📜  检查单词是否存在于网格中

📅  最后修改于: 2021-05-04 20:19:38             🧑  作者: Mango

给定字符和单词的2D网格,任务是检查该单词在网格中是否存在。一个单词可以在任意四个方向上匹配。

这4个方向分别是:水平左右方向,垂直上下方向。
例子:

Input:  grid[][] = {"axmy",
                    "bgdf",
                    "xeet",
                    "raks"};
Output: Yes

a x m y
b g d f
x e e t
r a k s

Input: grid[][] = {"axmy",
                   "brdf",
                   "xeet",
                   "rass"};
Output : No

资料来源:微软访谈

方法:下面的步骤描述了此处使用的想法:

  • 检查每个单元格(如果该单元格具有第一个字符),然后一个一个地重复出现,并尝试从该单元格的所有4个方向进行匹配。
  • 将网格中的位置标记为已访问,然后在4个可能的方向上重复出现。
  • 重复执行后,再次将职位标记为未访问。
  • 单词中的所有字母匹配后,返回true。

下面是上述方法的实现:

C++
// C++ program to check if the word
// exists in the grid or not
#include 
using namespace std;
#define r 4
#define c 5
  
// Function to check if a word exists in a grid
// starting from the first match in the grid
// level: index till which pattern is matched
// x, y: current position in 2D array
bool findmatch(char mat[r], string pat, int x, int y,
               int nrow, int ncol, int level)
{
    int l = pat.length();
  
    // Pattern matched
    if (level == l)
        return true;
  
    // Out of Boundary
    if (x < 0 || y < 0 || x >= nrow || y >= ncol)
        return false;
  
    // If grid matches with a letter while
    // recursion
    if (mat[x][y] == pat[level]) {
  
        // Marking this cell as visited
        char temp = mat[x][y];
        mat[x][y] = '#';
  
        // finding subpattern in 4 directions
        bool res = findmatch(mat, pat, x - 1, y, nrow, ncol, level + 1) | 
                   findmatch(mat, pat, x + 1, y, nrow, ncol, level + 1) | 
                   findmatch(mat, pat, x, y - 1, nrow, ncol, level + 1) | 
                   findmatch(mat, pat, x, y + 1, nrow, ncol, level + 1);
  
        // marking this cell
        // as unvisited again
        mat[x][y] = temp;
        return res;
    }
    else // Not matching then false
        return false;
}
  
// Function to check if the word exists in the grid or not
bool checkMatch(char mat[r], string pat, int nrow, int ncol)
{
  
    int l = pat.length();
  
    // if total characters in matrix is
    // less then pattern lenghth
    if (l > nrow * ncol)
        return false;
  
    // Traverse in the grid
    for (int i = 0; i < nrow; i++) {
        for (int j = 0; j < ncol; j++) {
  
            // If first letter matches, then recur and check
            if (mat[i][j] == pat[0])
                if (findmatch(mat, pat, i, j, nrow, ncol, 0))
                    return true;
        }
    }
    return false;
}
  
// Driver Code
int main()
{
    char grid[r] = { "axmy",
                        "bgdf",
                        "xeet",
                        "raks" };
  
    // Function to check if word exists or not
    if (checkMatch(grid, "geeks", r, c))
        cout << "Yes";
    else
        cout << "No";
  
 return 0;
  
}


Java
// Java program to check if the word
// exists in the grid or not
class GFG
{
      
static final int r = 4;
static final int c = 4;
  
// Function to check if a word exists in a grid
// starting from the first match in the grid
// level: index till which pattern is matched
// x, y: current position in 2D array
static boolean findmatch(char mat[][], String pat, int x, int y,
                        int nrow, int ncol, int level)
{
    int l = pat.length();
  
    // Pattern matched
    if (level == l)
        return true;
  
    // Out of Boundary
    if (x < 0 || y < 0 || x >= nrow || y >= ncol)
        return false;
  
    // If grid matches with a letter while
    // recursion
    if (mat[x][y] == pat.charAt(level))
    {
  
        // Marking this cell as visited
        char temp = mat[x][y];
        mat[x][y] = '#';
  
        // finding subpattern in 4 directions
        boolean res = findmatch(mat, pat, x - 1, y, nrow, ncol, level + 1) | 
                    findmatch(mat, pat, x + 1, y, nrow, ncol, level + 1) | 
                    findmatch(mat, pat, x, y - 1, nrow, ncol, level + 1) | 
                    findmatch(mat, pat, x, y + 1, nrow, ncol, level + 1);
  
        // marking this cell
        // as unvisited again
        mat[x][y] = temp;
        return res;
    }
    else // Not matching then false
        return false;
}
  
// Function to check if the word exists in the grid or not
static boolean checkMatch(char mat[][], String pat, int nrow, int ncol)
{
  
    int l = pat.length();
  
    // if total characters in matrix is
    // less then pattern lenghth
    if (l > nrow * ncol)
        return false;
  
    // Traverse in the grid
    for (int i = 0; i < nrow; i++)
    {
        for (int j = 0; j < ncol; j++) 
        {
  
            // If first letter matches, then recur and check
            if (mat[i][j] == pat.charAt(0))
                if (findmatch(mat, pat, i, j, nrow, ncol, 0))
                    return true;
        }
    }
    return false;
}
  
// Driver Code
public static void main(String[] args)
{
    char grid[][] = { "axmy".toCharArray(),
                        "bgdf".toCharArray(),
                        "xeet".toCharArray(),
                        "raks".toCharArray() };
  
    // Function to check if word exists or not
    if (checkMatch(grid, "geeks", r, c))
        System.out.print("Yes");
    else
        System.out.print("No");
}
}
  
// This code is contributed by 29AjayKumar


Python3
# Python3 program to check if the word 
# exists in the grid or not 
  
r = 4
c = 4
  
# Function to check if a word exists 
# in a grid starting from the first 
# match in the grid level: index till  
# which pattern is matched x, y: current 
# position in 2D array 
def findmatch(mat, pat, x, y, 
              nrow, ncol, level) :
  
    l = len(pat) 
  
    # Pattern matched 
    if (level == l) :
        return True
  
    # Out of Boundary 
    if (x < 0 or y < 0 or 
        x >= nrow or y >= ncol) :
        return False
  
    # If grid matches with a letter 
    # while recursion 
    if (mat[x][y] == pat[level]) :
  
        # Marking this cell as visited 
        temp = mat[x][y]
        mat[x].replace(mat[x][y], "#")
  
        # finding subpattern in 4 directions 
        res = (findmatch(mat, pat, x - 1, y, nrow, ncol, level + 1) | 
               findmatch(mat, pat, x + 1, y, nrow, ncol, level + 1) | 
               findmatch(mat, pat, x, y - 1, nrow, ncol, level + 1) |
               findmatch(mat, pat, x, y + 1, nrow, ncol, level + 1)) 
  
        # marking this cell as unvisited again 
        mat[x].replace(mat[x][y], temp)
        return res
      
    else : # Not matching then false 
        return False
  
# Function to check if the word
# exists in the grid or not 
def checkMatch(mat, pat, nrow, ncol) :
  
    l = len(pat)
  
    # if total characters in matrix is 
    # less then pattern lenghth 
    if (l > nrow * ncol) :
        return False
  
    # Traverse in the grid 
    for i in range(nrow) :
        for j in range(ncol) :
  
            # If first letter matches, then 
            # recur and check 
            if (mat[i][j] == pat[0]) :
                if (findmatch(mat, pat, i, j, 
                              nrow, ncol, 0)) :
                    return True
    return False
  
# Driver Code 
if __name__ == "__main__" :
  
    grid = ["axmy", "bgdf", 
            "xeet", "raks"]
  
    # Function to check if word 
    # exists or not 
    if (checkMatch(grid, "geeks", r, c)) :
        print("Yes")
    else :
        print("No") 
  
# This code is contributed by Ryuga


C#
// C# program to check if the word
// exists in the grid or not
using System;
  
class GFG
{
      
static readonly int r = 4;
static readonly int c = 4;
  
// Function to check if a word exists in a grid
// starting from the first match in the grid
// level: index till which pattern is matched
// x, y: current position in 2D array
static bool findmatch(char [,]mat, String pat, int x, int y,
                        int nrow, int ncol, int level)
{
    int l = pat.Length;
  
    // Pattern matched
    if (level == l)
        return true;
  
    // Out of Boundary
    if (x < 0 || y < 0 || x >= nrow || y >= ncol)
        return false;
  
    // If grid matches with a letter while
    // recursion
    if (mat[x, y] == pat[level])
    {
  
        // Marking this cell as visited
        char temp = mat[x, y];
        mat[x, y] = '#';
  
        // finding subpattern in 4 directions
        bool res = findmatch(mat, pat, x - 1, y, nrow, ncol, level + 1) | 
                    findmatch(mat, pat, x + 1, y, nrow, ncol, level + 1) | 
                    findmatch(mat, pat, x, y - 1, nrow, ncol, level + 1) | 
                    findmatch(mat, pat, x, y + 1, nrow, ncol, level + 1);
  
        // marking this cell
        // as unvisited again
        mat[x, y] = temp;
        return res;
    }
    else // Not matching then false
        return false;
}
  
// Function to check if the word exists in the grid or not
static bool checkMatch(char [,]mat, String pat, int nrow, int ncol)
{
  
    int l = pat.Length;
  
    // if total characters in matrix is
    // less then pattern lenghth
    if (l > nrow * ncol)
        return false;
  
    // Traverse in the grid
    for (int i = 0; i < nrow; i++)
    {
        for (int j = 0; j < ncol; j++) 
        {
  
            // If first letter matches, then recur and check
            if (mat[i, j] == pat[0])
                if (findmatch(mat, pat, i, j, nrow, ncol, 0))
                    return true;
        }
    }
    return false;
}
  
// Driver Code
public static void Main(String[] args)
{
    char [,]grid = { {'a','x','m','y'},
                    {'b','g','d','f'},
                    {'x','e','e','t'},
                    {'r','a','k','s'} };
  
    // Function to check if word exists or not
    if (checkMatch(grid, "geeks", r, c))
        Console.Write("Yes");
    else
        Console.Write("No");
}
}
  
// This code is contributed by 29AjayKumar


输出:
Yes