Java program for On a Cube Problem

Java 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 Java program for On a Cube Problem, which can give the total travel distance covered by a beetle.

Problem Statement

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

On a Cube Problem in few other Coding Languages

C

We don’t have the solution for this problem, you can contribute the answer of this code in C programming language, we post that answer on our page

C++

We don’t have the solution for this problem, you can contribute the answer of this code in C++ programming language, we post that answer on our page

Python

We don’t have the solution for this problem, you can contribute the answer of this code in C programming language, we post that answer on our page

 

6 comments on “Java program for On a Cube Problem”


  • LOKANI

    #include
    #include
    #include
    #define PI 3.14

    int sx,sy,sz;
    float short_dist(int x,int y,int z);

    int main()
    {
    int i,N,a[50],x,y,z;
    float sum = 0.0;
    scanf(“%d”,&N);
    N = 3 * N;
    for(i = 0;i < N;i++)
    {
    scanf("%d",&a[i]);
    }
    sx = a[0];
    sy = a[1];
    sz = a[2];
    for(i = 3;i<N;i=i+3)
    {
    sum = sum + short_dist(a[i],a[i+1],a[i+2]);
    }
    printf("%.2f",sum);
    return 0;
    }

    float short_dist(int x,int y,int z)
    {
    float dis = 0.0;
    if(z == sz && (x == sx || y == sy) && sz != 0)
    {
    if(x != sx){
    dis = (2 * PI * (abs(x – sx)))/6.0;
    }
    else{
    dis = (2 * PI * (abs(y – sy)))/6.0;
    }
    }
    else{
    dis = (int)(sqrt(pow(x-sx,2) + pow(y-sy,2)) + abs(z – sz));
    }
    sx = x;sy = y;sz = z;
    return dis;
    }


  • Saptarshi

    Please check the solution.
    from math import pi

    def calculate_distance(point_1, point_2):
    for i in range(3):
    if point_1[i] in [0,10]:
    surface_1 = i
    if point_2[i] in [0,10]:
    surface_2 = i

    if surface_1 == surface_2:
    radius = 0
    for i in range(3):
    if i != surface_1:
    radius += (point_1[i] – point_2[i])**2
    radius = radius**(0.5)
    return round(2*pi*radius/6,2)

    else:
    height = abs(point_1[surface_1] – point_2[surface_1]) + abs(point_1[surface_2] – point_2[surface_2])

    index = 3 – surface_1 – surface_2
    base = abs(point_1[index] – point_2[index])

    return round(((height)**2+(base)**2)**(0.5), 2)

    N = int(input())
    coordinates = list(map(int, input().split()))
    point_1 = coordinates[:3]
    total_distance = 0
    for i in range(1,N):
    point_2 = coordinates[i*3 : (i+1)*3]
    total_distance += (calculate_distance(point_1, point_2))
    point_1 = point_2.copy()

    print(round(total_distance, 2))