-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathDay-087.cpp
88 lines (86 loc) · 2.58 KB
/
Day-087.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
#include <bits/stdc++.h>
using namespace std;
class Node{
public:
char value;
map<char , vector<Node>> neighbour;
Node(char name) {
value = name;
}
};
class Solution{
map<char,char> opposites = {
{'N' , 'S'} , {'S' , 'N'} , {'E' , 'W'} , {'W' , 'E'}
};
public:
// a is present "direction" of b
bool add(Node&a , Node&b , const string&dir){
for(auto&ch : dir){
if(!add(a,b,ch)) {
return false;
}
}
return true;
}
bool add( Node &a, Node&b, const char& ch){
for(auto &itr : b.neighbour[opposites[ch]]){
if (itr.value == a.value){
return false;
}
}
for(auto & itr : a.neighbour[ch]){
if (itr.value == b.value){
return false;
}
}
/*
* if current node is in north(assume) of second node then all the neighbours of current node
* which are in north side , they must be also in north side of second node;
* there shouldn't be a ambiguous case like
* A N B
* C N B
* C S A
* though it is a correct configuration as coordinates can be : A C B
* but it is kind of ambiguous statement for over program because until we saw "C S A" we can't
* be sure what is the relationship between Node A and Node C
*/
for(auto itr : a.neighbour[ch]){
// handling the above case
bool added = add(itr,b,ch); // neighbour of a should also present in "ch" of b
if(!added){
return false;
}
}
b.neighbour[ch].push_back(a);
a.neighbour[opposites[ch]].push_back(b);
return true;
}
};
int main(void){
Solution s1;
{ // Invalid Case Testing
Node a('a');
Node b('b');
Node c('c');
if(!s1.add(a,b,"N")){
cout << "Invalid case" << '\n';
}
if(!s1.add(b,c,"NE")){
cout << "Invalid case" << '\n' ;
}
if(!s1.add(c,a,"N")){
cout << "Invalid case" << '\n' ;
}
}
{ // Valid Case Testing
Node a('a');
Node b('b');
if(!s1.add(a,b,"NW")){
cout << "Invalid case" << '\n';
}
if(!s1.add(a,b,"N")){
cout << "Invalid case" << '\n' ;
}
}
return 0;
}