Hasan and Walk
Practice
4.4 (22 votes)
Ready
Basic programming
Hard
Problem
24% Success 474 Attempts 50 Points 1s Time Limit 256MB Memory 1024 KB Max Code

Hasan likes walking very much! Specially when he's going to his college, that's why he doesn't use public transport.

And what is weird about Hasan is that he doesn't always follow shortest path to his college (so that he can enjoy more walking), his house is located in (Xh, Yh) and college is located in (Xc, Yc), Hasan starts walking from his house and move N steps, in each step walking either to left, right, up or down, and in last step he arrive to college, but Hasan doesn't like to go very far so he will not go outside rectangle defined by two opposite sides (1, 1) and (H, W), and it's guaranteed that house and college are inside that rectangle.

Hasan has a book that contains details about all different ways to go from his house to college in exactly N steps, each way in a separate line, a way is written in the book by a string of length N consisting of characters 'L', 'R', 'U', 'D' for left, right, up, down respectively, if he follow all characters correctly from left to right he will arrive to college, Hasan's book consists of multiple pages, each page has M lines, numbered from 0 to M-1 in each page, all ways are written in the book in lexicographical order, first M ways in first page, second M ways in second page and so on...

Today, Hasan chose an arbitrarily way from his book and used it to go to his college, but unfortunately he forgot where it was written in his book.

Given the way that Hasan used today help him by telling him in which line is this way written, you don't have to tell him in which page because he still remember it.

Input format:

First line consist of integers N, M, H, W, Xh, Yh, Xc, Yc.
Second line contain a string of length N consisting of only characters 'L', 'R', 'U', 'D' representing the way that Hasan followed today.

Output format:

One integer is line number in which the way that Hasan used is written.

Constraints:

  • 2<= N, W, H <= 200
  • 1<= Xh, Xc <= W
  • 1<= Yh, Yc <= H
  • 1<= M <=1,000,000

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:50
2 votes
Tags:
AlgorithmsDynamic ProgrammingOpenApprovedHard
Points:50
4 votes
Tags:
Dynamic ProgrammingAlgorithms3 DimensionalHard
Points:50
4 votes
Tags:
String3D dynamic programmingDynamic ProgrammingAlgorithms