This preview shows pages 1–3. Sign up to view the full content.
Introduction to Algorithms
November 22, 2005
Massachusetts Institute of Technology
6.046J/18.410J
Professors Erik D. Demaine and Charles E. Leiserson
Handout 27
Problem Set 8 Solutions
Problem 81.
No left turns
You’ve just stolen a brandnew Nexus Nano, but the owner has locked the car with
The Spade
, a
mechanical lock on the steering wheel, which prevents you from making left turns. In addition, to
avoid car accidents and otherwise attracting attention, you aren’t going to make any Uturns either.
You want to reach your chopshop
1
as quickly as possible, and since you can drive down straight
streets very quickly, your primary goal is to minimize the number of (right) turns you make. But
if two paths have the same number of right turns, then you want to minimize the straightline
distance.
Fortunately, you have a map of the city, so you can plan out a route that uses no left turns and no
Uturns. Even better, the map comes in electronic form as an
m
×
n
grid of cells, each marked as
either
empty
(navigable) or
blocked
(unnavigable). At each cell you visit, you can continue in the
direction you were going, or turn right. Two examples of noleftturn maps are shown in Figure 1.
s
t
s
t
Figure 1
: Two noleftturn maps. Find a path from
s
to
t
with as few right turns as possible,
and no leftor Uturns. You can play with these maps using a Java applet on
http://www.
clickmazes.com/noleft/ixnoleft.htm
.
(a)
Give an efﬁcient algorithm to ﬁnd a noleftturn path through an
m
×
n
map using
the minimum number of right turns, or report that no such path exists. Among all
1
A garage that illegally disassembles cars in order to sell the parts.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document2
Handout 27: Problem Set 8 Solutions
pathswiththeminimumnumberofrightturns,youralgorithmshouldreturnthepath
minimizingthestraightlinedistance(i.e.,thenumberofstraightsteps).(
Hint:
For
intuition,solvethelefthandmapinFigure1.)
Solution:
Thebasicideainsolvingthisproblemistocreateagraphrepresentation
oftheproblemandthenuseashortestpathsalgorithmtoﬁndtheoptimalnoleftturn
path.Eachedge
(
u, v
)
inthegraphhastwoweights,
w
1
(
u, v
)
and
w
2
(
u, v
)
.Weset
w
1
(
u, v
) = 1
iftheedgerepresentsarightturnand
w
2
(
u, v
) = 1
iftheedgerepresents
astraightlinepathofdistance1.
..
0
0
0
0
0
0
0
0
0
0
0
0
1
1
1
1
1
1
1
1
1
1
1
1
1
0
1
0
0
1
1
1
Figure2
:GraphRepresentation.
Onewaytoturnthisproblemintoagraph
G
isshowninFigure2.Ourmapofcells
isconvertedintoaweighted,directedgraph.Foreachofthe
mn
cellsofthemap,we
create4nodes.Ifthecarentersaparticularcellfromthecellbelowitonthemap
itwouldenterthe“south”nodeassociatedwiththatparticularcell. Aftercreating
thenodes,weassigndirectededgestothem.Edgesthatcorrespondtoarighthand
turnhaveanedgeweight
w
1
(
u, v
) = 1
and
w
2
(
u, v
) = 0
andedgesthatcorrespond
todrivingstraighthaveaweight
w
1
(
u, v
) = 0
and
w
2
(
u, v
) = 1
.Edgesthatwould
representlefthandturnsoruturnsarenotincludedinthegraph.Anedgewhichgoes
fromoneorientationtoanother(examplesouthtowest)representsarightturn.Itwill
takeus
O
(
mn
)
timetocreatethisgraphrepresentation.
This is the end of the preview. Sign up
to
access the rest of the document.
 Fall '05
 ErikD.DemaineandCharlesE.Leiserson

Click to edit the document details