-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcodingNinjas.cpp
More file actions
103 lines (82 loc) · 2.5 KB
/
codingNinjas.cpp
File metadata and controls
103 lines (82 loc) · 2.5 KB
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
94
95
96
97
98
99
100
101
102
103
// Coding Ninjas
// Given a NxM matrix containing Uppercase English Alphabets only. Your task is to tell if there is a path in the given matrix which makes the sentence “CODINGNINJA” .
// There is a path from any cell to all its neighbouring cells. A neighbour may share an edge or a corner.
// Input Format :
// Line 1 : Two space separated integers N and M, where N is number of rows and M is number of columns in the matrix.
// Next N lines : N rows of the matrix. First line of these N line will contain 0th row of matrix, second line will contain 1st row and so on
// Assume input to be 0-indexed based
// Output Format :
// Return 1 if there is a path which makes the sentence “CODINGNINJA” else return 0.
// Constraints :
// 1 <= N <= 100
// 1 <= M <= 100
// Sample Input :
// 2 11
// CXDXNXNXNXA
// XOXIXGXIXJX
// Sample Output :
// 1
#include<bits/stdc++.h>
using namespace std;
#define MAXN 102
void deleteVisited(bool **visited, int N) {
for(int i = 0; i < N; i++) {
delete [] visited[i];
}
delete [] visited;
}
bool findPath(char graph[][MAXN], int n, int m, int i, int j, bool **visited, string path) {
if(path == ""){
return true;
}
if(i >= n || j >= m || i < 0 || j < 0) {
return false;
}
if(visited[i][j] || graph[i][j] != path[0]) {
return false;
}
visited[i][j] = true;
if(findPath(graph, n, m, i, j+1, visited, path.substr(1)) || findPath(graph, n, m, i, j-1, visited, path.substr(1)) || findPath(graph, n, m, i+1, j, visited, path.substr(1)) || findPath(graph, n, m, i-1, j, visited, path.substr(1))){
visited[i][j] = false;
return true;
}
else if(findPath(graph, n, m, i+1, j+1, visited, path.substr(1)) || findPath(graph, n, m, i+1, j-1, visited, path.substr(1)) || findPath(graph, n, m, i-1, j+1, visited, path.substr(1)) || findPath(graph, n, m, i-1, j-1, visited, path.substr(1))) {
visited[i][j] = false;
return true;
}
visited[i][j] = false;
return false;
}
int solve(char Graph[][MAXN],int N, int M)
{
bool **visited = new bool*[N];
for(int i = 0; i < N; i++) {
visited[i] = new bool[M];
for(int j = 0; j < M; j++) {
visited[i][j] = false;
}
}
string path = "CODINGNINJA";
for(int i = 0; i < N; i++) {
for(int j = 0; j < M; j++) {
if(Graph[i][j] == 'C') {
if(findPath(Graph, N, M, i, j, visited, path)) {
deleteVisited(visited, N);
return 1;
}
}
}
}
deleteVisited(visited, N);
return 0;
}
int main()
{
int N,M,i;
char Graph[MAXN][MAXN];
cin>>N>>M;
for(i = 0;i < N; i++){
cin>>Graph[i];
}
cout<<solve(Graph,N,M)<<endl;
}