Whats wrong with my 2D map | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Whats wrong with my 2D map

https://code.sololearn.com/cW6KJwV5ZnS1/?ref=app Task description: You're given a representation of a 5x5 2D map, and if you can only move left, right, up, or down, determine how many moves you would have to make to get between two points on the map. Task: Determine the total number of moves that are needed between two points on a map. The points that you move between are marked with a P and the spaces in between are marked with X. Input Format: A string that represents the 2D space starting at the top left. Each level from top to bottom is separated by a comma. Output Format: An integer that represents the total number of moves that you had to make. Sample Input: XPXXX,XXXXX,XXXXX,XXXPX,XXXXX Sample Output: 5

24th Jul 2022, 12:38 PM
Yacine
Yacine - avatar
3 Answers
+ 3
I made the examples, input and output focus on the 2nd test Case XPXXX,XXXXX,XXXXX,XXXPX,XXXXX # Ex. (Output: 5) XXXXX,XXXXP,XXXXX,XXXXX,XXXPX # Test (Output: 4) PPXXX,XXXXX,XXXXX,XXXXX,XXXXX # Task Case 1 (Output: 1) PXXXX,XXXXX,XXXXX,XXXXX,XXXXP # Task Case 2 (Output: 8)
24th Jul 2022, 2:08 PM
SoloProg
SoloProg - avatar
+ 1
Yeah, I had to add abs() function to count the distance only in case the addition leads to a negative number.
24th Jul 2022, 2:18 PM
Yacine
Yacine - avatar
0
here is the solution. and this code has time complexityO(n): t = input() d = len(t.split(',')) nt = "".join(t.split(',')) x = nt.find('p') y = nt.rfind('p') q = y//d r = y%d a,b = x%d, x//d c,d = y%d, y//d p = abs(a-c) + abs(b-d) print(p)
14th Feb 2023, 9:38 AM
Aravelli Prakash Rao
Aravelli Prakash Rao - avatar