forked from dharmanshu1921/Daa-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFind_Word_in_Grid.cpp
93 lines (91 loc) · 2.37 KB
/
Find_Word_in_Grid.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
#define r 3
#define c 5
int find_letter(char grid[r][c],string word,int x,int y,int row,int col,int level)
{
// cout<<"___________________________"<<endl;
// cout<<"level = "<<level<<endl;
// cout<<"x = "<<x<<endl;
// cout<<"y = "<<y<<endl;
// cout<<"grid["<<x<<"]["<<y<<"] = "<<grid[x][y]<<endl;
// cout<<"___________________________"<<endl;
int l=word.length();
if(level==1)
{
cout<<"level becomes 1"<<endl;
return 1;
}
if(x<0||y<0||x>=row||y>=col)
{
cout<<"invalid conditions"<<endl;
return 0;
}
// cout<<"======================================="<<endl;
// cout<<"word[level] = "<<word[level]<<", level = "<<level<<endl;
// cout<<"grid[x][y] = "<<grid[x][y]<<", x = "<<x<<", y = "<<y<<endl;
// cout<<"======================================="<<endl;
if(grid[x][y]==word[level])
{
char temp=grid[x][y];
grid[x][y]='#';
int ans;
cout<<"ans = "<<ans<<endl;
ans=
find_letter(grid,word,x-1,y,row,col,level+1)|
find_letter(grid,word,x+1,y,row,col,level+1)|
find_letter(grid,word,x,y-1,row,col,level+1)|
find_letter(grid,word,x,y+1,row,col,level+1);
grid[x][y]=temp;
// cout<<"-------------------------------"<<endl;
// cout<<"grid[x][y] = "<<grid[x][y]<<", x = "<<x<<", y = "<<endl;
// cout<<"-------------------------------"<<endl;
return ans;
}
else
{
cout<<"returnes 0"<<endl;
return 0;
}
}
int word_present(char grid[r][c],string word,int row ,int col)
{
int l=word.length();
if(l>row*col)
{
return 0;
}
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
//agar grid me sabse pehla letter of that word mil gaya to uske aaju baju me check karo baki lettters uss word ke using find_letter funtion
if(grid[i][j]==word[0])
{
if(find_letter(grid,word,i,j,row,col,0))
{
return 1;
}
}
}
}
return 0;
}
int main()
{
char grid[r][c]=
{"ABCE",
"SFCS",
"ADEE"
};
if(word_present(grid,"ADBM",r,c))
{
cout<<"YES";
}
else
{
cout<<"NO";
}
return 0;
}