

"""simple docstring"""


from __future__ import annotations






def     a__    (    __SCREAMING_SNAKE_CASE )	->	int:

		# preprocessing the first row
		for i in range(1					, len(matrix[0] ) ):
				matrix[0][i] += matrix[0][i - 1]

		# preprocessing the first column
		for i in range(1					, len(__SCREAMING_SNAKE_CASE ) ):
				matrix[i][0] += matrix[i - 1][0]

		# updating the path cost for current position
		for i in range(1					, len(__SCREAMING_SNAKE_CASE ) ):
				for j in range(1					, len(matrix[0] ) ):
						matrix[i][j] += min(matrix[i - 1][j]					, matrix[i][j - 1] )

		return matrix[-1][-1]


if __name__ == "__main__":
			import doctest

			doctest.testmod()





"""simple docstring"""


__A						   =			{
    "A": ["B", "C", "E"],
    "B": ["A", "D", "E"],
    "C": ["A", "F", "G"],
    "D": ["B"],
    "E": ["A", "B", "D"],
    "F": ["C"],
    "G": ["C"],
}






def     a__    (    __SCREAMING_SNAKE_CASE					, __SCREAMING_SNAKE_CASE					, __SCREAMING_SNAKE_CASE )	->	list[str]:
		__lowerCAmelCase:       Tuple          =			set()
		# keep track of all the paths to be checked
		__lowerCAmelCase:       Optional[Any]          =			[[start]]

		# return path if start is goal
		if start == goal:
				return [start]

		# keeps looping until all possible paths have been checked
		while queue:
				# pop the first path from the queue
				__lowerCAmelCase:       str          =			queue.pop(0 )
				# get the last node from the path
				__lowerCAmelCase:       List[Any]          =			path[-1]
				if node not in explored:
						__lowerCAmelCase:       str          =			graph[node]
						# go through all neighbour nodes, construct a new path and
						# push it into the queue
						for neighbour in neighbours:
								__lowerCAmelCase:       Optional[int]          =			list(__SCREAMING_SNAKE_CASE )
								new_path.append(__SCREAMING_SNAKE_CASE )
								queue.append(__SCREAMING_SNAKE_CASE )
								# return path if neighbour is goal
								if neighbour == goal:
										return new_path

            # mark node as explored
						explored.add(__SCREAMING_SNAKE_CASE )

    # in case there's no path between the 2 nodes
		return []






def     a__    (    __SCREAMING_SNAKE_CASE					, __SCREAMING_SNAKE_CASE					, __SCREAMING_SNAKE_CASE )	->	int:
		if not graph or start not in graph or target not in graph:
				return -1
		if start == target:
				return 0
		__lowerCAmelCase:       Dict          =			[start]
		__lowerCAmelCase:       Any          =			set(__SCREAMING_SNAKE_CASE )
		# Keep tab on distances from `start` node.
		__lowerCAmelCase:       int          =			{start: 0, target: -1}
		while queue:
				__lowerCAmelCase:       Optional[Any]          =			queue.pop(0 )
				if node == target:
						__lowerCAmelCase:       Dict          =			(
						    dist[node] if dist[target] == -1 else min(dist[target]					, dist[node] )
						)
				for adjacent in graph[node]:
						if adjacent not in visited:
								visited.add(__SCREAMING_SNAKE_CASE )
								queue.append(__SCREAMING_SNAKE_CASE )
								__lowerCAmelCase:       List[str]          =			dist[node] + 1
		return dist[target]


if __name__ == "__main__":
			print(bfs_shortest_path(demo_graph, "G", "D"))  # returns ['G', 'C', 'A', 'B', 'D']
			print(bfs_shortest_path_distance(demo_graph, "G", "D"))  # returns 4


