BEETLE Editorial

Problem Explanation

There is a 10 \times 10 \times 10 cube and we are given 3 dimensional coordinates for a beetle and the points it visits. It can’t visit the bottom face of the cube. We have to output the distance travelled by the beetle. If the next point is on the next face it travels in the shortest path, Otherwise if the point is on the same face, it it goes in an arc that subtends an angle of 60 degrees at the circle’s center.

Approach

To solve this problem, we will first traverse the list and store the points in a list with an increment of 3 from 3 to 3 \cdot (n-1). We will check if the x or y coordinate is the same as the previous point and z coordinate is the same as the previous point if so the bee will travel in an arc else the bee will always take the shortest route.

Algorithm

  • We are storing the point in a list and traversing the list with an increment of 3 from index 3 to 3 \cdot (n-1).
  • To calculate distance, if the z-axis coordinate is the same as the previous point and any of the x or y coordinates is the same as the previous point, the bee will travel in an arc.
  • Otherwise, the bee will take the shortest route.

Code

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

int s_x,s_y,s_z;

float shortDist(int x, int y, int z)
{
    float dis;
    // check if the Z-axis and any other one axis are the same.
    if(z == s_z && ( y == s_y || x == s_x) && s_z != 0) {
        //check if the x axis of next co-ordinate is same
        if(x != s_x)
        dis = (2 * PIE * (abs(x-s_x)))/6.0;
        
        //check if the y axis of next co-ordinate is same 
        else
        dis = (2 * PIE * (abs(y-s_y)))/6.0;
    }
    
    // finddistance between x and y and the abs distance of Z axis
    else
        dis = (int)(sqrt(pow(x-s_x,2) + pow(y-s_y,2)) + abs(z-s_z)); 
    
    s_x = x;
    s_y = y;
    s_z = z;
    return dis;
}

int main()
{
    int n;
    cin>>n;
    
    n = 3 * n;
    int arr[n];
    for(int i = 0;i < n;i++) cin>>arr[i];
    
    float sum = 0.0;
  
    s_x = arr[0];
    s_y = arr[1];
    s_z = arr[2];
    for(int i=3;i<n;i+=3)
    {
        sum += shortDist(arr[i],arr[i+1],arr[i+2]);
    }
    printf("%.2f",sum);
}