C++ 在二维矩阵中搜索图案

C++ 在二维矩阵中搜索图案,c++,C++,这是一个面试问题 你必须在二维数组中找到一个字符串。输入包含二维字符数组和给定字符串。你可以朝八个方向中的一个移动。如果字符串完全找到,则输出包含字符串第一个字母的位置,否则返回-1。如果可能,多个答案中的任何一个都可以接受 例如,输入: b t g p a d r k j String: rat Output: (2,0) 我尝试了这个,但是得到了错误的输出 5 5 A C P R C X S O P C V O V N I W G F M N Q

这是一个面试问题

你必须在二维数组中找到一个字符串。输入包含二维字符数组和给定字符串。你可以朝八个方向中的一个移动。如果字符串完全找到,则输出包含字符串第一个字母的位置,否则返回-1。如果可能,多个答案中的任何一个都可以接受

例如,输入:

b t g
p a d
r k j

String: rat
Output: (2,0)
我尝试了这个,但是得到了错误的输出

 5 5
    A C P R C
    X S O P C
    V O V N I
    W G F M N
    Q A T I T

    MICROSOFT
请帮忙

#include<iostream>
#include<string>
using namespace std;
bool isInside(int x,int y,int m,int n)
{
    if(x>=0&&x<m&&y>=0&&y<n)return true;
    return false;
}
void findString(char mat[10][10],int m,int n,string str)
{
    int i,j;

    for(i=0;i<m;i++)
        for(j=0;j<n;j++)
    {
        int k=0,x=i,y=j;
        bool flag=true;
        if(mat[i][j]==str[k])
        {
            while(flag&&k<str.size())
            {
                int x1=x-1,x2=x+1,y1=y-1,y2=y+1;
                if(isInside(x1,y1,m,n)&&mat[x1][y1]==str[k])
                    {
                        x=x1; y=y1; k++;
                    }
                else if(isInside(x1,y,m,n)&&mat[x1][y]==str[k])
                    {
                        x=x1; y=y; k++;
                    }
                else if(isInside(x1,y2,m,n)&&mat[x1][y2]==str[k])
                    {
                        x=x1; y=y2; k++;
                    }
                else if(isInside(x,y1,m,n)&&mat[x][y1]==str[k])
                    {
                        x=x; y=y1; k++;
                    }
                else if(isInside(x,y2,m,n)&&mat[x][y2]==str[k])
                    {
                        x=x; y=y2; k++;
                    }
                else if(isInside(x2,y1,m,n)&&mat[x2][y1]==str[k])
                    {
                        x=x2; y=y1; k++;
                    }
                else if(isInside(x2,y,m,n)&&mat[x2][y]==str[k])
                    {
                        x=x2; y=y; k++;
                    }
                else if(isInside(x2,y2,m,n)&&mat[x2][y2]==str[k])
                    {
                        x=x2; y=y2; k++;
                    }
                else flag=false;
            }
            if(flag==true)
            {
                cout<<endl<<"\("<<i<<","<<j<<")"<<endl;
                return;
            }
        }
    }
    cout<<endl<<"-1"<<endl;
    return;
}
int main()
{
    int i,j,n,m;
    char mat[10][10];
    string str;
    cout<<"enter the dimention of the matrix: ";
    cin>>m>>n;
    cout<<endl<<"enter the char matrix:"<<endl;
    for(i=0;i<m;i++)
        for(j=0;j<n;j++)
        cin>>mat[i][j];
    cout<<endl<<"enter the test string: ";
    cin>>str;
    findString(mat,m,n,str);
    return 0;
}
#包括
#包括
使用名称空间std;
布尔伊森赛德(整数x,整数y,整数m,整数n)
{

如果(x>=0&&x=0&&y您的搜索未完成,因为从给定位置您只尝试一个有效邻居

例如,从
M
开始,如果只遵循底部的
I
,搜索将停止;您还需要尝试右上角的
I
,这需要递归解决方案

你的解决方案也不禁止两次通过同一个字符,我不确定这是否合法

伪代码:

Try(i, j, Suffix):
  if i < 0 or i >= m or j < 0 or j >= n:
    # No such position
    return

  if Mat[i][j] == Suffix[0]:
    if len(Suffix) == 1:
      # The whole string was matched
      print "Found !"
      return

    # Mark the position (optional)
    Mat[i][j]= uppercase(Mat[i][j])

    # Try all 8 neighbors
    Try(i+1, j, Suffix[1:])
    Try(i+1, j+1, Suffix[1:])
    Try(i, j+1, Suffix[1:])
    ...

    # Unmark the position (optional)
    Mat[i][j]= lowercase(Mat[i][j])

谢谢,我明白了。但是你能建议如何递归调用这个函数吗?如果你能建议任何代码编辑的话
for i in range(m):
  for j in range(n):
    Try(i, j, String)