C++ || DFS solution


#1
bool dfs(vector<string>A,int i,int j,string word,int idx){
    if(i<0 || i>=A.size() || j<0 || j>=A[0].length() || word[idx]!=A[i][j])
    return false;
    if(idx==word.length()-1)
    return true;
    bool res=dfs(A,i+1,j,word,idx+1) ||
    dfs(A,i-1,j,word,idx+1) ||
    dfs(A,i,j+1,word,idx+1) ||
    dfs(A,i,j-1,word,idx+1);
    return res;
}

int Solution::exist(vector<string> &A, string B) {
    for(int i=0;i<A.size();i++){
        for(int j=0;j<A[0].length();j++){
            if(A[i][j]==B[0] && dfs(A,i,j,B,0))
            return 1;
        }
    }
return 0;
}