-
Notifications
You must be signed in to change notification settings - Fork 144
/
Copy pathcodingninjas.cpp
71 lines (68 loc) · 1.79 KB
/
codingninjas.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
bool check(char graph[][MAXN],int n,int m,bool visited[][MAXN],string s,int i,int j)
{
if(s[0]=='\0')
return true;
if(visited[i][j]==true)
return false;
visited[i][j]=true;
bool ans=false;
if(graph[i-1][j-1]==s[0]&&(i-1>=0&&j-1>=0))
{
ans=check(graph,n,m,visited,s.substr(1),i-1,j-1);
if(ans)
return true;
}
if(graph[i-1][j]==s[0]&&i-1>=0)
{ ans=check(graph,n,m,visited,s.substr(1),i-1,j);
if(ans)
return true;
}
if(graph[i-1][j+1]==s[0]&&(i-1>=0&&j+1<m))
{ ans = check(graph,n,m,visited,s.substr(1),i-1,j+1);
if(ans)
return true;
}
if(graph[i][j+1]==s[0] && j+1<m)
{ ans = check(graph,n,m,visited,s.substr(1),i,j+1);
if(ans)
return true;
}
if(graph[i+1][j+1]==s[0] && (j+1<m&&i+1<n))
{
ans = check(graph,n,m,visited,s.substr(1),i+1,j+1);
if(ans)
return true;
}
if(graph[i+1][j]==s[0] && i+1<n)
{ans= check(graph,n,m,visited,s.substr(1),i+1,j);
if(ans)
return true;
}
if(graph[i+1][j-1]==s[0] && (j-1>=0&&i+1<n))
{ans = check(graph,n,m,visited,s.substr(1),i+1,j-1);
if(ans)
return true;
}
if(graph[i][j-1]==s[0] && j-1>=0)
{ans = check(graph,n,m,visited,s.substr(1),i,j-1);
if(ans)
return true;
}
visited[i][j]=false;
if(ans==false)
return false;
}
int solve(char graph[][MAXN],int N, int M)
{
// Write your code here.
bool visited[N][MAXN]={false};
string s="CODINGNINJA";
for(int k=0;k<N;k++)
{
for(int l=0;l<M;l++)
{ if(graph[k][l]=='C')
{
if(check(graph,N,M,visited,s.substr(1),k,l))
return true;
}
}