We have a rectangular grid of points where one corner is (0,0)
and the other corner is (π,π»), where
π,π» represent the width and height of the grid,
respectively. From each point (π₯,π¦), we can move
along one of the cardinal directions to (π₯β²,π¦β²) β
{(π₯+1,π¦) ,(π₯β1,π¦)
,(π₯,π¦+1), (π₯,π¦β1)}, as long as
0β€π₯β²β€π and 0β€π¦β²β€π» (i.e, we are
not allowed to move out of the grid).
Furthermore, we specify a set of π circles πΆ =
{(π₯1,π¦1,π1),β¦,(π₯π,π¦π,ππ)}
where each circle has center
(π₯π,π¦π) and radius
ππ.
The goal is to find a path from (0,0) to (π,π»)
while avoiding any point on the surface of or inside the circles in
πΆ. If such a path is found, your algorithm should return
the path as a list of grid points. If not, your algorithm should
return the empty list.
Example 1
Consider π =π» = 3 and two circles πΆ =
{(1,2,1),(2,2,0.5)}.
The red lines show a path from (0,0) to (3,3). Your algorithm
may return a list [(0,0), (1,0), (2,0), (3, 0), (3,1), (3,2), (3,3)
] (there is another path in this case and any of them may be
returned.
Example 2
Consider π=π»=3and two circles
πΆ={(1,2,1),(2,2,1)}.
There are no paths in this case (in particular (3,2) lies on the
orange circle though this is not 100% clear from the picture). Your
algorithm should return the empty list. Note, coding language used
is python
from math import sqrt
# You may use this function to test if a point lies inside given
circle.
def ptInCircle(x,y, circles_list):
for (xc,yc,rc) in circles_list:
d = sqrt ( (x-xc)**2 +
(y-yc)**2)
if d <= rc:
return True
return False
def findPath(width, height, forbidden_circles_list):
# width is a positive number
# height is a positive number
# forbidden_circles_list is a list of triples
[(x1, y1, r1),..., (xk, yk, rk)]
assert width >= 1
assert height >= 1
assert all(x <= width and x >=0 and y
<= height and y >= 0 and r > 0 for (x,y,r) in
forbidden_circles_list)
# your code here
To check for correctness
def checkPath(width, height, circles, path):
assert path[0] == (0,0), 'Path must begin at
(0,0)'
assert path[-1] == (width, height), f'Path must
end at {(width, height)}'
(cur_x, cur_y) = path[0]
for (new_x, new_y) in path[1:]:
dx = new_x - cur_x
dy = new_y - cur_y
assert (dx,dy) in
[(1,0),(-1,0), (0,1),(0,-1)]
assert 0 <= new_x and
new_x <= width
assert 0 <= new_y and
new_y <= height
assert not
ptInCircle(new_x, new_y, circles)
cur_x, cur_y = new_x,
new_y
return
print('-- Test 1 -- ')
circles = [(2,2,0.5), (1,2,1)]
p = findPath(3, 3, circles)
print(p)
checkPath(3, 3, circles, p)
print('-- Test 2 -- ')
circles1 = [(2,2,1), (1,2,1)]
p1 = findPath(3, 3, circles1)
print(p1)
assert p1 == [], 'Answer does not match with ours'
print('-- Test 3 -- ')
p2 = findPath(5,5, circles1)
print(p2)
checkPath(5, 5, circles1, p2)
print('-- Test 4 --')
circles3 = [(1,2,0.5), (2,2,1), (3,3,1),(4,3,1)]
p3 = findPath(5, 5, circles3)
print(p3)
checkPath(5, 5, circles3, p3)
print('-- Test 5 --')
circles5 = [ (4,1, 1), (4,4,1),(2,6,1)]
p5 = findPath(6,6,circles5)
print(p5)
assert p5 == []
print('All tests passed: 15 points!')
3 2 1 0 0 1 1 0.5 2 3
3 2 1 Π Π 1 1 2 3
We have a rectangular grid of points where one corner is (0,0) and the other corner is (𝑊,𝐻), where 
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am