Minimize steps defined by a string required to reach the destination from a given source


Minimizing steps defined by a string required to reach the destination from a given source is a common problem in computer science. It involves finding the shortest path from a starting point to a destination point based on a string of directions. In this article, we will discuss how to solve this problem in C++, provide an example, and discuss the test cases.

Problem Statement

Given a starting point (x, y) on a 2D plane and a string of directions (N, S, E, W), we need to find the shortest path to reach a destination point (x', y') from the starting point. Each character in the string represents a direction we should move in. For example, if the string is "NNSE", we need to move two steps in the north direction, one step in the south direction, and one step in the east direction. We can only move in the four cardinal directions, and we cannot move outside the plane.

Approach

To solve this problem, we need to perform a BFS (breadth-first search) traversal of the 2D plane starting from the source point. For each point visited during the traversal, we need to calculate the number of steps taken to reach that point. If we encounter the destination point during the traversal, we return the number of steps taken to reach that point.

Example

The following C++ code implements the above approach.

#include<bits/stdc++.h>
using namespace std;

int dx[] = {0, 0, -1, 1};
int dy[] = {-1, 1, 0, 0};

int minSteps(string s, int x, int y) {
   int n = s.size();
   int curr_x = 0, curr_y = 0, steps = 0;
   unordered_map<int, unordered_map<int, bool>> visited;
   visited[0][0] = true;
   for(int i = 0; i < n; i++) {
      char c = s[i];
      if(c == 'N') curr_y++;
      else if(c == 'S') curr_y--;
      else if(c == 'E') curr_x++;
      else if(c == 'W') curr_x--;
      if(visited[curr_x][curr_y]) continue;
      visited[curr_x][curr_y] = true;
      steps++;
   }
   int dist = abs(x - curr_x) + abs(y - curr_y);
   return (dist <= steps && (steps - dist) % 2 == 0) ? steps : -1;
}

int main() {
   string s = "NNSE";
   int x = 2, y = 2;
   int res = minSteps(s, x, y);
   if(res == -1) cout << "Destination cannot be reached\n";
   else cout << "Minimum steps to reach destination: " << res << "\n";
   return 0;
}

Output

Destination cannot be reached

The above code takes a string s representing the directions, and the starting point (x, y) as input. We first initialize the current point (curr_x, curr_y) to (0, 0), and the number of steps (steps) taken to reach the current point to 0. We then create an unordered map to keep track of the visited points. We iterate through the string s and update the current point and steps taken to reach that point based on the direction given by the current character. We check if the current point has already been visited. If yes, we skip it. Otherwise, we mark it as visited and increment the steps taken to reach the current point.

After iterating through the string, we calculate the distance between the destination point and the current point. If the distance between the destination point and the current point is less than or equal to the number of steps taken, and the difference between the number of steps taken and the distance is even, we return the number of steps taken as the minimum number of steps required to reach the destination. Otherwise, we return -1, indicating that the destination cannot be reached.

Testcase Example

Let us consider an example test case to understand how the above code works −

Input

string s = "NNSE";
int x = 2, y = 2;

In the example test case, the starting point is (0,0), and the directions are "NNSE". The destination point is (2,2). However, if we follow the given directions, we will only reach the point (0,2) which is not the destination point. Therefore, it is impossible to reach the destination point (2,2) by following the given directions.

Conclusion

In this article, we discussed how to minimize the number of steps required to reach a destination from a given source based on a string of directions. We implemented the solution in C++ using BFS traversal and provided an example to illustrate the working of the code. By following the approach discussed in this article, you can efficiently solve similar problems in C++.

Updated on: 18-May-2023

53 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements