
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Total Distance to Visit City Blocks in Python
Suppose we have a matrix of unique strings representing the city blocks, and another list of strings containing blocks to visit. If we are at block matrix[0][0], then find the total Manhattan distance required to visit every block in order.
So, if the input is like
Q | B | C |
D | E | Z |
G | G | i |
Block = [H,B,C]
Then the output will be 6 as "h" is 2 blocks bottom(south) and 1 block right(east), "b" is 2 blocks up(north), "c" is 1 block right(east).
To solve this, we will follow these steps −
- coords := a map with key 'start' and value (0, 0)
- for each row in mat, do
- for each col in mat, do
- insert (row,col) into coords[mat[row, col]]
- for each col in mat, do
- dist := 0
- update blocks by adding 'start' at the beginning
- for i in range 0 to size of blocks -1, do
- c1 := coords[blocks[i]]
- c2 := coords[blocks[i+1]]
- d := |c1[0]-c2[0]| + |c1[1]-c2[1]|
- dist := dist + d
- return dist
Let us see the following implementation to get better understanding −
Example
class Solution: def solve(self, mat, blocks): coords = {'start': (0,0)} for row in range(len(mat)): for col in range(len(mat[row])): coords[mat[row][col]] = (row,col) dist = 0 blocks = ['start']+blocks for i in range(len(blocks)-1): c1 = coords[blocks[i]] c2 = coords[blocks[i+1]] d = abs(c1[0]-c2[0]) + abs(c1[1]-c2[1]) dist += d return dist ob = Solution() inp = [["q", "b", "c"], ["d", "e", "z"], ["g", "h", "i"]] blk = ["h", "b", "c"] print(ob.solve(inp, blk))
Input
[["q", "b", "c"],["d", "e", "z"],["g", "h", "i"]]
Output
6
- Related Articles
- Program to check we can visit any city from any city or not in Python
- Thomas covers a total distance of 1056 km from city A to city C through city B. If the distance from city A to city B is 543.7 km, find the distance between city B and city C.Express the answer as a decimal.
- Find maximum distance between any city and station in Python
- C++ program to count number of cities we can visit from each city with given operations
- Total Hamming Distance in C++
- Program to find minimum total distance between house and nearest mailbox in Python
- Find maximum distance between any city and station in C++
- Max Increase to Keep City Skyline in Python
- Program to find list showing total distance required to move all balls to current position in Python
- A Visit to Cambridge
- What blocks Ruby, Python to get Javascript V8 speed?
- In the given figure, find the total distance travelled."\n
- The following is the distribution of total household expenditure (in Rs.) of manual worker in a city:
- Find the City With the Smallest Number of Neighbors at a Threshold Distance in C++
- What are the places to visit in Hyderabad?

Advertisements