Ciao a tutti, ho bisogno di aiuto per risolvere il problema The Dutch Farmer.
Guardando i casi d’esempio, ho scoperto che un tulipano è valido quando è all’interno di esattamente due delle semicirconferenze con i lati del rettangolo come diametro, ma mi dà output errato solo per l’ultimo testcase.
Ecco il mio codice:
// NOTE: it is recommended to use this even if you don't understand the following code.
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
using namespace std;
struct point
{
double x, y;
};
int main()
{
// uncomment the two following lines if you want to read/write from files
//ifstream cin("/Users/Riccardo/Desktop/Coding/olinfo/OII4/input.txt");
point topLeft, topRight, bottomRight, bottomLeft;
cin >> topLeft.x >> topLeft.y;
cin >> topRight.x >> topRight.y;
cin >> bottomRight.x >> bottomRight.y;
cin >> bottomLeft.x >> bottomLeft.y;
//punti centrali dei lati
point nord = {(topLeft.x + topRight.x) / 2.0, topLeft.y};
point est = {topRight.x, (topRight.y + bottomRight.y) / 2.0};
point sud = {(bottomLeft.x + bottomRight.x) / 2.0, bottomLeft.y};
point ovest = {bottomLeft.x, (bottomLeft.y + topLeft.y) / 2.0};
int N, K = 0;
double X, Y;
cin >> N;
//raggi delle coppie di semicirconferenze
double radx = abs(topRight.x - bottomLeft.x) / 2.0,
rady = abs(topRight.y - bottomLeft.y) / 2.0;
for (int i = 0; i < N; ++i)
{
cin >> X >> Y;
int count =
(hypot(nord.x - X, nord.y - Y) < radx) +
(hypot(sud.x - X, sud.y - Y) < radx) +
(hypot(ovest.x - X, ovest.y - Y) < rady) +
(hypot(est.x - X, est.y - Y) < rady);
//conto dentro quante circonferenze (da 1 a 3) il tulipano è compreso, per poi aggiornare K se il conteggio è uguale a 2
K += (count == 2);
}
cout << K << endl;
return 0;
}
Grazie mille in anticipo!