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. For a particular cell, neighbouring cells are those cells that share an edge or a corner with the cell.

Input Format :
The first line of input contains two space separated integers N and M, where N is number of rows and M is the number of columns in the matrix. 
Each of the following N lines contain M characters. Please note that characters are not space separated.
Output Format :
Print 1 if there is a path which makes the sentence “CODINGNINJA” else print 0.
Constraints :
1 <= N <= 1000
1 <= M <= 1000
Time Limit: 1 second
Sample Input 1:
2 11
CXDXNXNXNXA
XOXIXGXIXJX
Sample Output 1:
1




bool hasPath(vector<vector<char>> &board, int n, int m) {
// Write your code here.
}








#include <iostream>
#include <vector>
using namespace std;

#include "solution.h"

int main() {
int n, m;
cin >> n >> m;

vector<vector<char>> board(n, vector<char>(m));

for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
cin >> board[i][j];
}
}

cout << (hasPath(board, n, m) ? 1 : 0);
}

Comments

Popular posts from this blog

Code : Get Path - BFS

Code : All connected components