-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathPath in a Rectangle with Circles.cpp
149 lines (126 loc) · 3.32 KB
/
Path in a Rectangle with Circles.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// C++ program to find out path in
// a rectangle containing circles.
#include <iostream>
#include <math.h>
#include <vector>
using namespace std;
// Function to find out if there is
// any possible path or not.
bool isPossible(int m, int n, int k, int r, vector<int> X,
vector<int> Y)
{
// Take an array of m*n size and
// initialize each element to 0.
int rect[m][n] = { 0 };
// Now using Pythagorean theorem find if a
// cell touches or within any circle or not.
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
for (int p = 0; p < k; p++) {
if (sqrt((pow((X[p] - 1 - i), 2)
+ pow((Y[p] - 1 - j), 2)))
<= r) {
rect[i][j] = -1;
}
}
}
}
// If the starting cell comes within
// any circle return false.
if (rect[0][0] == -1)
return false;
// Now use BFS to find if there
// is any possible path or not.
// Initialize the queue which holds
// the discovered cells whose neighbors
// are not discovered yet.
vector<vector<int> > qu;
rect[0][0] = 1;
qu.push_back({ 0, 0 });
// Discover cells until queue is not empty
while (!qu.empty()) {
vector<int> arr = qu.front();
qu.erase(qu.begin());
int elex = arr[0];
int eley = arr[1];
// Discover the eight adjacent nodes.
// check top-left cell
if ((elex > 0) && (eley > 0)
&& (rect[elex - 1][eley - 1] == 0)) {
rect[elex - 1][eley - 1] = 1;
vector<int> v = { elex - 1, eley - 1 };
qu.push_back(v);
}
// check top cell
if ((elex > 0) && (rect[elex - 1][eley] == 0)) {
rect[elex - 1][eley] = 1;
vector<int> v = { elex - 1, eley };
qu.push_back(v);
}
// check top-right cell
if ((elex > 0) && (eley < n - 1)
&& (rect[elex - 1][eley + 1] == 0)) {
rect[elex - 1][eley + 1] = 1;
vector<int> v = { elex - 1, eley + 1 };
qu.push_back(v);
}
// check left cell
if ((eley > 0) && (rect[elex][eley - 1] == 0)) {
rect[elex][eley - 1] = 1;
vector<int> v = { elex, eley - 1 };
qu.push_back(v);
}
// check right cell
if ((eley < n - 1) && (rect[elex][eley + 1] == 0)) {
rect[elex][eley + 1] = 1;
vector<int> v = { elex, eley + 1 };
qu.push_back(v);
}
// check bottom-left cell
if ((elex < m - 1) && (eley > 0)
&& (rect[elex + 1][eley - 1] == 0)) {
rect[elex + 1][eley - 1] = 1;
vector<int> v = { elex + 1, eley - 1 };
qu.push_back(v);
}
// check bottom cell
if ((elex < m - 1) && (rect[elex + 1][eley] == 0)) {
rect[elex + 1][eley] = 1;
vector<int> v = { elex + 1, eley };
qu.push_back(v);
}
// check bottom-right cell
if ((elex < m - 1) && (eley < n - 1)
&& (rect[elex + 1][eley + 1] == 0)) {
rect[elex + 1][eley + 1] = 1;
vector<int> v = { elex + 1, eley + 1 };
qu.push_back(v);
}
}
// Now if the end cell (i.e. bottom right cell)
// is 1(reachable) then we will send true.
return (rect[m - 1][n - 1] == 1);
}
// Driver Program
int main()
{
// Test case 1
int m1 = 5, n1 = 5, k1 = 2, r1 = 1;
vector<int> X1 = { 1, 3 };
vector<int> Y1 = { 3, 3 };
// Function call
if (isPossible(m1, n1, k1, r1, X1, Y1))
cout << "Possible" << endl;
else
cout << "Not Possible" << endl;
// Test case 2
int m2 = 5, n2 = 5, k2 = 2, r2 = 1;
vector<int> X2 = { 1, 1 };
vector<int> Y2 = { 2, 3 };
// Function call
if (isPossible(m2, n2, k2, r2, X2, Y2))
cout << "Possible" << endl;
else
cout << "Not Possible" << endl;
return 0;
}