forked from black-shadows/InterviewBit-Topicwise-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathValidPath.cpp
More file actions
104 lines (77 loc) · 2 KB
/
Copy pathValidPath.cpp
File metadata and controls
104 lines (77 loc) · 2 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
104
string Solution::solve(int x, int y, int n, int r, vector<int> &E, vector<int> &F)
{
int ans[x+1][y+1];
for(int i=0;i<=x ;i++)
{
for(int j=0;j<=y ;j++)
ans[i][j]=0;
}
for(int i=0;i<=x ; i++)
{
for(int j=0;j<=y ; j++)
{
for(int k=0;k<n ; k++)
{
if( sqrt( pow((E[k]-i),2) + pow((F[k]-j),2) ) <= r )
ans[i][j]=-1;
}
}
}
queue< pair<int,int> > q;
pair<int,int> p;
if(ans[0][0]==-1)
return "NO";
q.push({0,0});
ans[0][0]=1;
while(!q.empty())
{
p=q.front();
q.pop();
int a=p.first;
int b=p.second;
if( a>0 && b>0 && ans[a-1][b-1]==0)
{
ans[a-1][b-1]=1;
q.push({a-1,b-1});
}
if(a+1 <= x && b+1 <= y && ans[a+1][b+1]==0)
{
ans[a+1][b+1]=1;
q.push({a+1,b+1});
}
if( a>0 && ans[a-1][b]==0)
{
ans[a-1][b]=1;
q.push({a-1,b});
}
if(b>0 && ans[a][b-1]==0)
{
ans[a][b-1]=1;
q.push({a,b-1});
}
if(a>0 && b+1<=y && ans[a-1][b+1]==0)
{
ans[a-1][b+1]=1;
q.push({a-1,b+1});
}
if(b+1 <= y && ans[a][b+1]==0)
{
ans[a][b+1]=1;
q.push({a,b+1});
}
if(a+1 <= x && b>0 && ans[a+1][b-1]==0)
{
ans[a+1][b-1]=1;
q.push({a+1,b-1});
}
if(a+1<=x && ans[a+1][b]==0)
{
ans[a+1][b]=1;
q.push({a+1,b});
}
}
if(ans[x][y]==1)
return "YES";
else
return "NO";
}