Home python directed graph adjacency list

Author

Date

Category

An Olympiad problem in computer science is given: You are developing a navigator for one city. This city is divided by streets into square blocks, and the movement along any of the street sections within each block is strictly one-way. You can leave each intersection only in the directions permitted by the signs. It is required to plot the shortest route from point AA to point BB using the attached city map with the permitted directions of movement indicated on it.

Input data format

A map of the city’s intersections is submitted to the entrance. The first line contains two numbers NN – the number of blocks from north to south and MM – the number of blocks from west to east (1 ≤ n ≤ 50). Point AA is the most northwest, point BB is the southeast. Then 2 * N + 12 * N + 1 lines contain a description of the permitted directions of movement. The streets of the city west-east are described in odd lines. Each such line contains MM characters without a space, indicating the permitted movement in the corresponding section. The even lines contain north-south street descriptions. Each of these lines contains M + 1M + 1 characters each, indicating possible movement along the north-south street sections. Movement to the north, south, west, east is indicated by the letters n, s, w, e, respectively.

Output format

In the first line, print the number of street segments in the shortest route from point AA to point BB. The second line should contain a description of this route in the form of a sequence of characters n, s, w, e without spaces. If there are several shortest routes, display the very first one in alphabetical order. It is guaranteed that from point AA you can get to point BB.

Explanation of the first example Sample Input 1:

``````4 5
weeee
snssss
wewww
snsnns
weeew
snnsns
wwwew
ssssns
eeeew
``````

Sample Output 1:

``````29
sssseeeennnwwseswwnnneeeessss
``````

Sample Input 2:

``````2 2
ee
sss
ee
sss
ee
``````

Sample Output 2:

``````4
eess
``````

The first thing that crossed my mind was to create directed graph adjacency lists:

``````n, m = map (int, input (). split ())
west_east = []
north_south = []
for i in range (1, 2 * n + 2):
if i% 2! = 0:
west_east.append (input ())
else:
north_south.append (input ())
graphs = []
for i in range ((n + 1) * (m + 1)):
graphs.append ([])
if west_east [i% (n + 1)] [(i - 1)% m] == 'w' and i - 1 & gt; = 0:
graphs [i] .append (i - 1)
elif i% (m + 1)! = m:
graphs [i] .append (i + 1)
if north_south [(i - 1)% n] [i% (m + 1)] == 'n' and i - (m + 1) & gt; = 0:
graphs [i] .append (i - (m + 1))
elif i + m + 1 & lt; nine:
graphs [i] .append (i + (m + 1))
print (west_east)
print (north_south)
print (graphs)
``````

However, my code is not working quite right. It outputs, say, 1 and 6 for vertices zero, when it should output 6, since the path from 0 leads only there. Help me figure it out, I’m confused (((

Here’s what I got and it seems to work

``````n, m = [int (i) for i in str (input ()). split ()]
points = []
for i in range (n + 1):
k = []
for j in range (m + 1):
k.append (str (i) + '_' + str (j))
points.append (k)
k = []
for i in points:
for j in i:
k.append (j)
napr = []
for i in range (n):
napr.append ([str (input ()) + '-', str (input ())])
napr.append ([str (input ())])
pointsnap = points.copy ()
for i in range (len (pointsnap)):
for j in range (len (pointsnap [i])):
pointsnap [i] [j] = []
for i in range (len (napr)):
for j in range (len (napr [i] [- 1])):
if i == 0 and j == 0:
if napr [i]  [j] == 'e':
pointsnap [i] [j] .append ('e')
if napr [i] [- 1] [j] == 's':
pointsnap [i] [j] .append ('s')
elif i == 0 and j! = 0 and j & lt; len (napr  [- 1]) - 1:
if j - 1 & gt; = 0:
if napr [i]  [j - 1] == 'w':
pointsnap [i] [j] .append ('w')
if napr [i]  [j] == 'e':
pointsnap [i] [j] .append ('e')
if napr [i] [- 1] [j] == 's':
pointsnap [i] [j] .append ('s')
elif i == 0 and j == len (napr  ) - 1:
if j - 1 & gt; = 0:
if napr [i]  [j - 1] == 'w':
pointsnap [i] [j] .append ('w')
if napr [i] [- 1] [j] == 's':
pointsnap [i] [j] .append ('s')
elif i! = 0 and i & lt; len (napr) - 1 and j == 0:
if napr [i - 1] [- 1] [j] == 'n':
pointsnap [i] [j] .append ('n')
if napr [i] [- 1] [j] == 's':
pointsnap [i] [j] .append ('s')
if napr [i]  [j] == 'e':
pointsnap [i] [j] .append ('e')
elif i == len (napr) - 1 and j == 0:
if (i - 1) & gt; = 0:
if napr [i - 1] [- 1] [j] == 'n':
pointsnap [i] [j] .append ('n')
if napr [i]  [j] == 'e':
pointsnap [i] [j] .append ('e')
elif i! = 0 and i & lt; len (napr) - 1 and j! = 0 and j & lt; len (napr  [- 1]) - 1:
if napr [i]  [j] == 'e':
pointsnap [i] [j] .append ('e')
if (i - 1) & gt; = 0:
if napr [i - 1] [- 1] [j] == 'n':
pointsnap [i] [j] .append ('n')
if napr [i] [- 1] [j] == 's':
pointsnap [i] [j] .append ('s')
if (j - 1) & gt; = 0:
if napr [i]  [j - 1] == 'w':
pointsnap [i] [j] .append ('w')
elif i == (len (napr) - 1) and j! = 0:
if napr [i]  [j] == 'e':
pointsnap [i] [j] .append ('e')
if (j - 1) & gt; = 0:
if napr [i]  [j - 1] == 'w':
pointsnap [i] [j] .append ('w')
if (i - 1) & gt; = 0:
if napr [i - 1] [- 1] [j] == 'n':
pointsnap [i] [j] .append ('n')
elif i! = 0 and j == (len (napr  ) - 1):
if napr [i] [- 1] [j] == 's':
pointsnap [i] [j] .append ('s')
if (i - 1) & gt; = 0:
if napr [i - 1] [- 1] [j] == 'n':
pointsnap [i] [j] .append ('n')
if (j - 1) & gt; = 0:
if napr [i] [- 1] [j - 1] == 'w':
pointsnap [i] [j] .append ('w')
count = 0
for i in range (len (pointsnap)):
for j in range (len (pointsnap [i])):
pointsnap [i] [j] .append (str (count))
count + = 1
con = dict ()
for i in range (len (points)):
for j in range (len (points [i])):
d = []
for h in pointsnap [i] [j]:
if h == 'e':
d.append (pointsnap [i] [j + 1] [- 1])
elif h == 's':
d.append (pointsnap [i + 1] [j] [- 1])
elif h == 'w':
d.append (pointsnap [i] [j - 1] [- 1])
elif h == 'n':
d.append (pointsnap [i - 1] [j] [- 1])
con [pointsnap [i] [j] [- 1]] = d
def bfs (graph_to_search, start, end):
queue = [[start]]
visited = set ()
while queue:
path = queue.pop (0)
vertex = path [-1]
if vertex == end:
return path
elif vertex not in visited:
for current_neighbor in graph_to_search.get (vertex, []):
new_path = list (path)
new_path.append (current_neighbor)
queue.append (new_path)
path = [int (i) for i in bfs (con, '0', str (count - 1))]
print (len (path) - 1)
path2 = []
count = 0
for i in range (len (pointsnap)):
for j in range (len (pointsnap [i])):
pointsnap [i] [j] = count
count + = 1
ROW = 0.
column = 0.
For i in path [1:]:
IF i in Pointsnap [ROW]:
IF I & LT; POINTSNAP [ROW] [column]:
Path2.append ('W')
column - = 1
ELSE:
Path2.append ('E')
column + = 1
ELSE:
if (row - 1) & gt; = 0 and i in pointsnap [row - 1]:
Path2.append ('n')
ROW - = 1
ELSE:
Path2.APPEND ('S')
ROW + = 1
Print (''. Join (Path2))
``````

``````n, m = map (int, input (). Split ())
WEST_EAST = []
north_south = []
For i in Range (1, 2 * n + 2):
IF i% 2! = 0:
WEST_EAST.APPEND (INPUT ())
ELSE:
north_south.append (input ())
Graphs = [[] for i in range ((n + 1) * (M + 1))]
For i in Range ((n + 1) * (M + 1)):
if weest_east [i% (n + 1)] [(i - 1)% m] == 'w' and (i - 1)% m + 1 == i:
IF I - 1 NOT IN GRAPHS [I]:
Graphs [i] .APPEND (I - 1)
ELIF WEST_EAST [I% (N + 1)] [(i - 1)% m] == 'W' and (i - 1)% m - 1 == i:
IF I - 1 NOT IN GRAPHS [I]:
Graphs [i + 1] .APPEND (I)
ELIF WEST_EAST [I% (N + 1)] [(i - 1)% m] == 'E' and (i - 1)% m - 1 == i:
IF i + 1 NOT IN GRAPHS [I]:
Graphs [i] .APPEND (I + 1)
ELIF WEST_EAST [I% (N + 1)] [(i - 1)% m] == 'E' and (i - 1)% m + 1 == i:
IF i + 1 NOT IN GRAPHS [I - 1]:
Graphs [i - 1] .APPEND (I)
If north_south [(i - 1)% n] [i% (m + 1)] == 'n' and i - (m + 1) & gt; = 0:
Graphs [i] .append (I - (M + 1))
ELIF I + M + 1 & LT; nine:
Graphs [i] .APPEND (I + (M + 1))
Print (West_EAST)
Print (north_south)
PRINT (Graphs)
``````

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.