# Python Program for on a cube problem

## On a cube problem

On a cube, the problem is one of the questions asked in TCS CodeVita where we need to calculate the total distance traveled by the beetle. A beetle can travel in al the 3 directions as it is on the surface of the cube. Here is the Python program for On a Cube Problem, which can give the total travel distance covered by a beetle.

### Problem description:

A solid cube of 10 cm x 10cm x 10 cm rests on the ground. It has a beetle on it, and some sweet honey spots at various locations on the surface of the cube. The beetle starts at a point on the surface of the cube and goes to the honey spots in order along the surface of the cube.

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

The beetle is a student of cartesian geometry and knows the coordinates (x, y, z) of all the points it needs to go to. The origin of coordinates it uses is one corner of the cube on the ground, and the z-axis points up.Hence, the bottom surface (on which it does not crawl) is z=0, and the top surface is z=10.The beetle keeps track of all the distances traveled, and rounds the distance traveled to two decimal places once it reaches the next spot so that the final distance is a sum of the rounded distances from spot to spot.

Input Format:

• The first line gives an integer N, the total number of points (including the starting point) the beetle visits

• The second line is a set of 3N comma separated non-negative numbers, with up to two decimal places each. These are to be interpreted in groups of three as the x, y, z coordinates of the points the beetle needs to visit in the given order.

Output Format:

•  One line with a number giving the total distance traveled by the beetle accurate to two decimal places. Even if the distance traveled is an integer, the output should have two decimal places

Constraints:

• None of the points the beetle visits is on the bottom face (z=0) or on any of the edges of the cube (the lines where two faces meet)

• 2<=N<=10

Sample Input 1:
3
1, 1, 10, 2, 1, 10, 0, 5, 9
Sample Output 1:
6.05
Sample Input 2:
3
1, 1, 10, 2, 1, 10, 0, 1, 9
Sample Output 2:
4.05

### Implementation:

• Store the point in a list and traverse the list from index 3 to 3*n-1 with increment of 3.
• For calculating distance if z axis cordinate is same as previous point and any one of x or y cordinate  is same as pervious point , then bee travel in arc.
• Else bee travels through shortest path.

#### Python Code:

import math
PI=3.14

def short_distx,y,z,sx,sy,sz):
dis = 0.0
#print(sx,sy,sz)
if(z == sz and (x == sx or y == sy) and sz != 0): # if the Z-axis and any one of the X-axis or Y-axis is same.

if(x != sx):    # if the next co-ordinate’s x axis is same
dis = (2 * PI * (abs(x – sx)))/6.0

else# if the next co-ordinate’s Y axis is same
dis = (2 * PI * (abs(y – sy)))/6.0

else#if bee is moving to another face
dis = int((math.sqrt(pow(x-sx,2) + pow(y-sy,2)) + abs(z – sz)))  #find eculidean distance between x and y and the abs distance of Z axis

#assigning new starting cordinate as bee moved to that point
sx = x
sy = y
sz = z

return dis,sx,sy,sz
n=int(input())
points=list(map(int,input().split(‘,’)))
#starting co-ordinate
sx = points[0]
sy = points[1]
sz = points[2]
sum=0
#traverse the list
for i in range(3,3*n,3):
x,sx,sy,sz= short_dist(points[i],points[i+1],points[i+2],sx,sy,sz)
sum = sum + x

print(round(sum,2))

`Input: 31, 1, 10, 2, 1, 10, 0, 5, 9 Output :6.05`
`Input: 31, 1, 10, 2, 1, 10, 0, 1, 9 Output :4.05`