Minimum Time in Wormhole Network

Determine the minimum time required to travel from a starting point to a destination point in a two-dimensional coordinate system, considering both direct movement and the use of wormholes.

Explanation:

You are given a starting point (sx, sy) and a destination point (dx, dy). The spacecraft can move only in the X (left or right) or Y (up or down) direction. The time taken to move from one point (x1, y1) to another point (x2, y2) is calculated as |x2 - x1| + |y2 - y1| seconds.

Additionally, there are N wormholes in the system. Using a wormhole allows you to travel from its entry point to its exit point in a specified amount of time. Your task is to find the minimum time required to reach the destination, using direct movement and/or wormholes.

Input:

The first line contains 4 space-separated integers sx, sy, dx, and dy representing the source and destination points.
The second line contains an integer N, the number of wormholes.
Each of the next N lines contains five space-separated integers: the starting point (x1, y1), the exit point (x2, y2) of the wormhole, and the time taken to use the wormhole.

Output:

Print the minimum time required to travel from the source to the destination point.

Example:

Input:
sx = 0, sy = 0, dx = 10, dy = 10
N = 1
Wormhole: (2, 2) to (8, 8) with time 1
Output:
9

Explanation:

The optimal path may include using the wormhole if it reduces the total travel time.

Constraints:

  • 1 <= Coordinates <= 105
  • 1 <= N <= 200
  • 1 <= Wormhole Time <= 105
  • Time Limit: 1 sec
AnswerBot
4mo

Find the minimum time to travel from a starting point to a destination point using direct movement and wormholes.

  • Calculate the time taken for direct movement from source to destination.

  • Consider using ...read more

Help your peers!
Select
Add answer anonymously...

Samsung Software Developer interview questions & answers

A Software Developer was asked 3w agoQ. All 1's and 2's of array
A Software Developer was asked Q. Write a function to insert a node into an N-ary tree.
A Software Developer was asked Q. What is the basic role of JAVA in software development?

Popular interview questions of Software Developer

A Software Developer was asked 4w agoQ1. All 1's and 2's of array
A Software Developer was asked Q2. What is the basic role of JAVA in software development?
A Software Developer was asked Q3. Implement the Hungarian algorithm.

Top HR questions asked in Samsung Software Developer

A Software Developer was asked 8mo agoQ1. Where do you see yourself in 5 years?
A Software Developer was asked Q2. Will you stay at Samsung?
A Software Developer was asked Q3. Tell me about yourself.
Samsung Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits