#DCEPCA02. Ant Colony Optimization
Ant Colony Optimization
Ants are known to move in groups and in perfect order. However, in our problem the ants can move only in directions Left (L), Right (R), Up (U), Down (D). The place is a rectangular grid. There are 2 types of ants: Type A and Type B. Each ant type has its own queen ant which defines the 2 directions at which their type of ants can reproduce ants.
A starting cell is given where one ant of each type is standing (A and B). When the process begins, each type of ant produces 4 ants, 2 ants of their type in the 2 directions (1 ant per direction) specified by their queen ant and 2 ants of the other type in the remaining 2 directions. So if queen ant of type A says LR and queen ant of type B says UD then A ant produces new type A ants at left cell and right cell and new type B ants at up cell and down cell of the original cell. Similarly, type B ant produces 2 new type A ants in left and right cell and 2 new type B ants in up cell and down cell. The ants produced again reproduce more ants at their adjacent cells according to the same directions given by the their queen ant.
Reproducing ants at adjacent cells take 1 time unit. You need to find the sum of the minimum time in which type A ant reaches the end point and the minimum time in which type B ant reaches the end point. If any type of ant cannot reach end point, the answer is -1.
Constraints
T ≤ 10
0 < M ≤ 500
0 < N ≤ 500
0 ≤ SX, EX < M
0 ≤ SY, EY < N
Input
T denotes the number of test cases. Each test case consists of 4 lines. The first line gives M and N which is the size of the grid. The second line gives starting cell cordinates SX and SY. The third line gives end point cell cordinates EX and EY. The fourth line gives a permutation of LRUD denoting the directions provided by queen ant to move. The first two characters of the string gives the directions of queen ant type A and last two characters gives direction of queen ant type B.
Output
Print T lines each giving the minimum time in which ant of both types can reach the ending cell for each testcase ie. Find sum of (minimum time at which A type ant reaches end point + minimum time at which B type ant reaches end point.)
Example
Input: 3 5 5 1 1 3 4 LRUD 2 4 0 2 0 3 RULD 2 4 0 2 1 1 URDL</p>Output: 10 -1 6
Explanation
Test Case 1: Minimum time at which a type A ant reaches end point = 5, and minimum time at which a type B ant reaches end point = 5
Test Case 2: B cannot reach end point. Hence answer is -1.
Test Case 3: Minimum time at which a type A ant reaches end point = 4, and minimum time at which a type B ant reaches end point = 2