e-maze-in

Ankit is in the maze. The command center sent him a string that decodes to come out from the maze. He is initially at (0, 0). The string contains L, R, U, D denoting left, right, up, and down. In each command, he will traverse 1 unit distance in the respective direction.

For example, if he is at (2, 0) and the command is L he will go to (1, 0).

Example:

Input:  s = "LLRDDR"
Output: 0 -2

Approach

C++

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

void eMazeIn(string s)
{
    int L = 0R = 0U = 0D = 0;
    int n = s.size();
    for (int i = 0i < ni++)
    {
        if (s[i] == 'L')
            L++;
        else if (s[i] == 'R')
            R++;
        else if (s[i] == 'U')
            U++;
        else
            D++;
    }
    cout << R - L << " " << U - D << "\n";
}
int main()
{
    string s = "LLRDDR";

    eMazeIn(s);

    return 0;
}


No comments:

Post a Comment