Problem: ROBOTS Problem - CodeChef
This problem can be solved by calculating the position in three different directions (along the three lines passing through each point). The angle between each line is 60 rather than the standard 90 degrees.
Assuming these directions to be x, y, z:
Then find the new position, direction after each substring in three axes
calculating new direction relative to previous and accordingly moving the robot:
d = (d + c) % 6;
New position moving 1 unit in this direction:
if (d == 0)
x++;
else if (d == 1)
y++;
else if (d == 2)
z++;
else if (d == 3)
x--;
else if (d == 4)
y--;
else if (d == 5)
z--;
Which can be translated to 2 axes
finalX = x + (y / 2) - (z / 2);
finalY = (y + z) * sqrt(3) / 2.0;
This will guarantee the required precision but will exceed the time limit.
Time Complexity: O(NQ)
Here is my submission: https://www.codechef.com/viewsolution/27934250
FOR RANGED QUERIES:
Use prefix-calculation storing the position(in three dimensions) and direction after 1 to i queries.
Then find the relative change in position from before the first operation to the last operation in the query.
Detailed Description:
Assuming xs, ys, zs, ds store the position(x[i], y[i], z[i]) and direction(d[i])
after 1 to i operations
FOR l, r QUERY:
Initial Position will be:
d1 = ds[l - 1]
x1 = xs[l - 1]
y1 = ys[l - 1]
z1 = zs[l - 1]
Final Position:
d2 = ds[r]
x2 = xs[r]
y2 = ys[r]
z2 = zs[r]
Relative change in position:
xq = x2 - x1
yq = y2 - y1
zq = z2 - z1
if d1 == 0:
x = xq
y = yq
z = zq
elif d1 == 1:
x = yq
y = zq
z = - xq
elif d1 == 2:
x = zq
y = -xq
z = -yq
elif d1 == 3:
x = -xq
y = -yq
z = - zq
elif d1 == 4:
x = -yq
y = -zq
z = xq
elif d1 == 5:
x = -zq
y = xq
z = yq
Use these formula to find position in x and y:
finalX = x + (y / 2) - (z / 2);
finalY = (y + z) * sqrt(3) / 2.0;
Time Complexity: O(N+Q)
Here is my AC submission: https://www.codechef.com/viewsolution/27935391
Expecting some likes