# TCS CodeVita Question – Total distance traveled by the Beetle

## Total distance traveled by the Beetle

Here, in this page we will discuss the program for finding the total distance traveled by the beetle in different programming languages, this question is asked in TCS CodeVita.
TCS CodeVita Season 10 is the tenth-edition of the prestigious coding competition which is organized by TCS every year, in the search of world’s  best coder. ### Problem Description

A 10cm x 10cm x 10cm solid cube rests on the ground. It has a beetle on it, as well as some sweet honey spots on the cube’s surface. The beetle begins at a point on the cube’s surface and moves in a clockwise direction along the cube’s surface to the honey spots.

• If it goes from one point on the same face to another (say, X to Y), it goes in an arc of a circle that subtends an angle of 60 degrees at the circle’s center.
• If it travels from one point to another on a different face, it takes the shortest path on the cube’s surface, except that it never travels along its bottom.

The beetle is a Cartesian geometry student who knows the coordinates (x, y, z) of all the points it needs to visit. Its coordinate origin is one of the cube’s corners on the ground, and the z-axis points up. As a result, z=0 is the bottom surface (on which it does not crawl), and z=10 is the top. The beetle keeps track of all distances traveled and rounded the distance to two decimal places when it arrives at the following location so that the final distance is a sum of the rounded distances from spot to spot.

Input

The first line returns an integer N, the total number of points visited by the beetle (including the starting point).

The second line contains 3N non-negative numbers, each with two decimal places. These are to be interpreted as the x, y, and z coordinates of the points the beetle must visit in the given order.

Output

One line containing a number representing the total distance traveled by the beetle to two decimal places. Even if the travel distance is an integer, the output should have two decimal places.

Constraints

None of the points visited by the beetle are on the bottom face (z=0) or on any of the cube’s edges (the lines where two faces meet)

2<=N<=10

```#include<stdio.h> #include<math.h>
#include<stdlib.h>
#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;
scanf("%d", &n);
n = 3 * n;
int arr[n];
for(int i = 0;i < n;i++)
scanf("%d", &arr[i]);

float sum = 0.0;

s_x = arr;
s_y = arr;
s_z = arr;
for(int i=3;i<n;i+=3)
{
sum += shortDist(arr[i],arr[i+1],arr[i+2]);
}
printf("%.2f",sum);
} ```
```#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;
s_y = arr;
s_z = arr;
for(int i=3;i<n;i+=3)
{
sum += shortDist(arr[i],arr[i+1],arr[i+2]);
}
printf("%.2f",sum);
} ```
```import java.util.*;
import java.lang.*;
import java.io.*;
class Main{

final static float PIE = 3.14f;
static int s_x, s_y, s_z;

public static void main(String[] args) {

int i, N, x, y, z;
int arr[] = new int;
float sum = 0.0f;
Scanner sc = new Scanner(System.in);

N = sc.nextInt();
N = 3 * N;

for(i = 0; i < N; i++){
arr[i] = sc.nextInt();
}

s_x = arr;
s_y = arr;
s_z = arr;

for(i = 3; i < N ; i += 3){
sum += shortDist(arr[i], arr[i+1], arr[i+2]);
}
System.out.printf("%.2f", sum);
}

private static 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 * (Math.abs(x - s_x))) / 6.0f;
}

//check if the y axis of next co-ordinate is same
else{
dis = (2 * PIE * (Math.abs(y - s_y))) / 6.0f;
}
}
else{
dis = (int)(Math.sqrt(Math.pow(x - s_x, 2) + Math.pow(y - s_y, 2)) + Math.abs(z - s_z));
}
s_x = x;
s_y = y;
s_z = z;
return dis;
}
}```
```import math
PIE=3.14
def shortDist( x,y,z,sx,sy,sz):
dis = 0.0

# if the x or y axis same as z axis
if(z == s_z and (y == s_y or x==s_x) and s_z != 0):
# if the x axis of next co-ordinate is same
if(x != s_x):
dis = (2 * PIE * (abs(x - s_x)))/6.0

# if the y axis of next co-ordinate is same
else:
dis = (2 * PIE * (abs(y -s_y)))/6.0

# else means bee is going to another face
#finding eculidean distance
else:
dis = int((math.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 dist,s_x,s_y,s_z

n=int(input())
arr = [int(x) for x in input().split()]

s_x = arr
s_y = arr
s_z = arr
ans=0
for j in range(3,3*n,3):
x,s_x,s_y,s_z= shortDist(arr[j],arr[j+1],arr[j+2],s_x,s_y,s_z)
ans += x
print(round(ans,2))```

### Input

`31 1 10 2 1 10 0 1 9`

`4.05`