A solid cube of 10 cm x 10cm x 10 cm rests on the ground. It has a beetle on it, and…
- User
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 centre 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 travelled, and rounds the distance travelled 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 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 One line with a number giving the total distance travelled by the beetle accurate to two decimal places. Even if the distance travelled 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. solve in c++
-
- Assistant
- User
remove comments
- Assistant
Here's the C++ solution without comments:
#include <iostream> #include <vector> #include <cmath> #include <iomanip> using namespace std; double euclideanDistance(double x1, double y1, double z1, double x2, double y2, double z2) { return sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1) + (z2 - z1) * (z2 - z1)); } double arcDistance(double x1, double y1, double x2, double y2) { double radius = euclideanDistance(x1, y1, 0, x2, y2, 0) / sqrt(3); return M_PI * radius / 3.0; } double shortestPath(double x1, double y1, double z1, double x2, double y2, double z2) { double distance = euclideanDistance(x1, y1, z1, x2, y2, z2); return distance; } bool isSameFace(double x1, double y1, double z1, double x2, double y2, double z2) { return (fabs(x1 - x2) < 1e-9 && y1 == y2) || (fabs(y1 - y2) < 1e-9 && x1 == x2) || (fabs(z1 - z2) < 1e-9 && x1 == x2); } int main() { int N; cin >> N; vector<tuple<double, double, double>> points(N); for (int i = 0; i < N; ++i) { double x, y, z; char comma; cin >> x >> comma >> y >> comma >> z; points[i] = make_tuple(x, y, z); } double totalDistance = 0.0; for (int i = 0; i < N - 1; ++i) { double x1 = get<0>(points[i]); double y1 = get<1>(points[i]); double z1 = get<2>(points[i]); double x2 = get<0>(points[i + 1]); double y2 = get<1>(points[i + 1]); double z2 = get<2>(points[i + 1]); double segmentDistance = 0.0; if (isSameFace(x1, y1, z1, x2, y2, z2)) { if (z1 == z2) { segmentDistance = arcDistance(x1, y1, x2, y2); } else { segmentDistance = euclideanDistance(x1, y1, z1, x2, y2, z2); } } else { segmentDistance = shortestPath(x1, y1, z1, x2, y2, z2); } totalDistance += round(segmentDistance * 100) / 100.0; } cout << fixed << setprecision(2) << totalDistance << endl; return 0; } - User
make variable name small and plag free
- Assistant
Certainly! I'll shorten the variable names to make them concise and ensure they're distinct:
#include <iostream> #include <vector> #include <cmath> #include <iomanip> using namespace std; double dist(double x1, double y1, double z1, double x2, double y2, double z2) { return sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1) + (z2 - z1) * (z2 - z1)); } double arcDist(double x1, double y1, double x2, double y2) { double r = dist(x1, y1, 0, x2, y2, 0) / sqrt(3); return M_PI * r / 3.0; } double shortPath(double x1, double y1, double z1, double x2, double y2, double z2) { return dist(x1, y1, z1, x2, y2, z2); } bool sameFace(double x1, double y1, double z1, double x2, double y2, double z2) { return (fabs(x1 - x2) < 1e-9 && y1 == y2) || (fabs(y1 - y2) < 1e-9 && x1 == x2) || (fabs(z1 - z2) < 1e-9 && x1 == x2); } int main() { int n; cin >> n; vector<tuple<double, double, double>> pts(n); for (int i = 0; i < n; ++i) { double x, y, z; char c; cin >> x >> c >> y >> c >> z; pts[i] = make_tuple(x, y, z); } double totalDist = 0.0; for (int i = 0; i < n - 1; ++i) { double x1 = get<0>(pts[i]); double y1 = get<1>(pts[i]); double z1 = get<2>(pts[i]); double x2 = get<0>(pts[i + 1]); double y2 = get<1>(pts[i + 1]); double z2 = get<2>(pts[i + 1]); double segDist = 0.0; if (sameFace(x1, y1, z1, x2, y2, z2)) { if (z1 == z2) { segDist = arcDist(x1, y1, x2, y2); } else { segDist = dist(x1, y1, z1, x2, y2, z2); } } else { segDist = shortPath(x1, y1, z1, x2, y2, z2); } totalDist += round(segDist * 100) / 100.0; } cout << fixed << setprecision(2) << totalDist << endl; return 0; }