Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- int ans=0;
- map<int,map<int,bool>> visited;
- void func(char** board, char* word, int i,int x,int y,int m,int n){
- if(x<0 || y<0 || x>=m|| y>=n )
- return;
- if(!visited[x][y] && word[i]==board[x][y]){
- if(i==strlen(word)-1){
- ans++;
- return;
- }
- visited[x][y]=true;
- func(board,word,i+1,x+1,y,m,n);
- func(board,word,i+1,x+1,y+1,m,n);
- func(board,word,i+1,x+1,y-1,m,n);
- func(board,word,i+1,x-1,y+1,m,n);
- func(board,word,i+1,x-1,y-1,m,n);
- func(board,word,i+1,x-1,y,m,n);
- func(board,word,i+1,x,y+1,m,n);
- func(board,word,i+1,x,y-1,m,n);
- }
- return;
- }
- int helper(char** board,int m,int n,char* word)
- {
- ans=0;
- for(int i=0;i<m;i++){
- for(int j=0;j<n;j++){
- if(board[i][j]==word[0])
- {
- //memset(visited,false,sizeof(visited));
- visited.clear();
- func(board,word,0,i,j,m,n);
- }
- }
- }
- return ans;
- }
- int main(){
- char** board;
- board=new char* [2];
- cout<<visited[1][2]<<endl;
- for(int i=0;i<2;i++)
- board[i] = new char[4];
- board[0][0]='h';
- board[0][1]='e';
- board[0][2]='l';
- board[0][3]='l';
- board[1][0]='a';
- board[1][1]='b';
- board[1][2]='h';
- board[1][3]='o';
- char* word;
- word=new char[5];
- word[0]='h';
- word[1]='e';
- word[2]='l';
- word[3]='l';
- word[4]='o';
- cout<<helper(board,2,4,word);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement