Algorithm
Problem Name: beecrowd | 3162
Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/3162
Space Communication
By Daniel Lago, CEFET-MG Brazil
Timelimit: 1
The year is 2337. Thousands of human crews travel in space in a crazy way to and fro. And the best: the ships can communicate via radio, it is even possible for crews between different ships to play games.
However, unfortunately the signal quality fades with distance. While nearby ships are able to communicate well, ships that are distant have poor signal strength to communicate. For this reason, the ships preferentially communicate with the nearest ship.
Considering a stretch of space where the ships can be considered points in space, therefore with three-dimensional coordinates, with each axis being able to have a value between 0 and 100 m.u. It is known that the intensity of the communication signal is given by the distance between the ships; so that ships that are spaced up to 20 m.u. have a high intensity; above 20 m.u. and up to 50 m.u. have a medium intensity; while the signal strength above 50 m.u. it is so low that it does not allow communication between ships.
Given the information passed on, help the crew of these ships to be able to know the signal strength between each of them and the nearest ship, to inform them if they will be able to have good communication with each other.
Input
The first line of the entry has an integer N (2 <= N <= 10), which represents the number of ships in the space to be analyzed. The next N lines will receive 3 integer values, separated by space, indicating the discrete x, y and z coordinates of each ship.
Output
One line for each ship, indicating a letter for the signal strength between it and the nearest ship. "A" stands for high intensity; "M" represents medium intensity and "B" represents low intensity.
Input Sample | Output Sample |
4 50 55 55 15 28 79 45 48 37 25 50 32 |
A B A M |
Code Examples
#1 Code Example with C Programming
Code -
C Programming
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int m[11][3];
int __always_inline min(int a, int b)
{
return a < b ? a : b;
}
int __always_inline dist(int a, int b)
{
return sqrt(pow((m[a][0] - m[b][0]), 2) + pow((m[a][1] - m[b][1]), 2) + pow((m[a][2] - m[b][2]), 2));
}
int main(int argc, char **argv)
{
int n;
scanf("%d", &n);
memset(m, 0, sizeof(int) * n);
for (int i = 0; i < n; ++i)
scanf("%d %d %d", &m[i][0], &m[i][1], &m[i][2]);
for (int i = 0; i < n; ++i)
{
int minimun = 0xFFFF;
for (int j = 0; j < n; ++j)
if (i != j)
minimun = min(minimun, dist(i, j));
if (minimun < 20)
putchar_unlocked('A'), putchar_unlocked('\n');
else if (minimun < 50)
putchar_unlocked('M'), putchar_unlocked('\n');
else
putchar_unlocked('B'), putchar_unlocked('\n');
}
return 0;
}
Copy The Code &
Try With Live Editor
Input
Output
#2 Code Example with Javascript Programming
Code -
Javascript Programming
const { readFileSync } = require("node:fs")
const [[N], ...input] = readFileSync("/dev/stdin", "utf8")
.split("\n")
.map((line) => line.split(" ", 3).map(value => Number.parseInt(value, 10)))
class Point3D {
/**
* @typedef {number | bigint | string} axisType
* @param {axisType} x
* @param {axisType} y
* @param {axisType} z
*/
constructor(x, y, z) {
this.x = Number.parseFloat(x.toString(10))
this.y = Number.parseFloat(y.toString(10))
this.z = Number.parseFloat(z.toString(10))
}
}
class Coordinates3D {
/**
* @param {Point3D} pointA
* @param {Point3D} pointB
*/
static distance(pointA, pointB) {
const dx = pointA.x - pointB.x
const dy = pointA.y - pointB.y
const dz = pointA.z - pointB.z
return Math.hypot(dx, dy, dz)
}
}
/**
* find the closest distance between a point and another multiple points
* @param {Point3D} point
* @param {number} index
* @param {Point3D[]} points
*/
const calculateClosestDistance3D = (point, index, points) => {
let distance = Number.POSITIVE_INFINITY
for (let j = 0; j < N; j++) if (index != j) distance = Math.min(distance, Coordinates3D.distance(point, points[j]))
return distance
}
const SignalIntensityEnum = Object.freeze({
HIGH: "A",
MEDIUM: "M",
LOW: "B"
})
const getSignalIntensityFromDistance = (distance) => {
if (distance <= 20) return SignalIntensityEnum.HIGH
if (20 <= distance && distance <= 50) return SignalIntensityEnum.MEDIUM
if (50 <= distance) return SignalIntensityEnum.LOW
}
const output = input
.splice(0, N)
.map(([X, Y, Z]) => new Point3D(X, Y, Z))
.map(calculateClosestDistance3D)
.map(getSignalIntensityFromDistance)
console.log(output.join("\n"))
Copy The Code &
Try With Live Editor