Advertisement
yash123321

Untitled

Oct 7th, 2021
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.36 KB | None | 0 0
  1. int dx[] = {1,0,-1,0};
  2. int dy[] = {0,1,0,-1};
  3. class Solution {
  4. public:
  5.     bool solve(vector<vector<char>>& arr,string s,vector<vector<int>>& vis,int i,int x,int y,int n,int m)
  6.     {
  7.         if(i==s.size())
  8.             return true;
  9.         bool flag = false;
  10.         for(int j=0;j<4;j++)
  11.         {
  12.             int x1 = x+dx[j];
  13.             int y1 = y+dy[j];
  14.             if(x1>=0&&x1<n&&y1>=0&&y1<m&&!vis[x1][y1]&&arr[x1][y1]==s[i])
  15.             {
  16.                 vis[x1][y1]=1;
  17.                 flag|=solve(arr,s,vis,i+1,x1,y1,n,m);
  18.                 vis[x1][y1]=0;
  19.                 if(flag)
  20.                     return true;
  21.             }
  22.         }
  23.         return flag;
  24.     }
  25.     bool exist(vector<vector<char>>& board, string word) {
  26.         int n = board.size();
  27.         int m = board[0].size();
  28.         vector<vector<int>> vis(n,vector<int>(m,0));
  29.         // memset(vis,0,sizeof(vis));
  30.         int f=0;
  31.         for(int i=0;i<n;i++)
  32.         {
  33.             for(int j=0;j<m;j++)
  34.             {
  35.                 if(board[i][j]==word[0])
  36.                 {
  37.                    
  38.                     vis[i][j]=1;
  39.                     if(solve(board,word,vis,1,i,j,n,m))
  40.                     {
  41.                         return true;
  42.                     }
  43.                     vis[i][j]=0;
  44.                 }
  45.             }
  46.         }
  47.         return false;
  48.     }
  49. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement