Posted: Sun Sep 16, 2007 7:57 pm
right ... that approach is not quite correct... changed to different one,
but still compute minimum diameter.
but still compute minimum diameter.
The Online Judge board
https://onlinejudge.org/board/
Code: Select all
Removed
Code: Select all
theta = acos( ( a*a + b*b - c*c )/(2*a*b) ) ;
alpha = acos( a/D[j] ); // since D[j] = 2*r ;
Code: Select all
theta = acos( ( a*a + b*b - c*c )/(2*a*b) ) ;
alpha = asin(c/D[j] );
if (alpha-theta>-eps) ...
thanks for your reply. But I thought a lot about my logic but yet not found any wrong. Can u give me some sample i/o so that i can justify my logic.I believe that you have used the wrong angle.
Code: Select all
bool valid(double a, double b, double c, double d){
double theta, alpha, eta, xx;
if( a - d > eps ) return false ;
theta = acos( ( a*a + b*b - c*c )/(2*a*b) ) ;
alpha = acos( a/d ); // since D[j] = 2*r ;
eta = theta - alpha ;
xx = d*cos(eta) ;
return (xx-b>-eps);
}
Code: Select all
if (valid(a,b,c,D[j]) || valid(b,c,a,D[j]) || valid(c,a,b,D[j])) ...
Code: Select all
#include <iostream>
#include <cmath>
using namespace std;
#define PI 3.141592653589793
int main()
{
double holes[100];
int num_holes = 0;
int i;
cin >> num_holes;
for(i = 0; i < num_holes; i++) {
cin >> holes[i];
}
int num_pegs = 0;
cin >> num_pegs;
char letters[27] = "ABCDEFGHIJKLMNOPQRSTUVXYZ";
for(i = 0; i < num_pegs; i++)
{
double d1;
double d2;
double d3;
bool fit_in_holes[100];
bool does_fit = false;
cin >> d1 >> d2 >> d3;
for(int j = 0; j < num_holes; j++)
{
double r = holes[j] / 2.0;
if(2.0*r < d3) continue;
double theta = acos(((2.0*r*r - d3*d3) / (2.0*r*r)));
double delta = acos(((d2*d2 + d3*d3 - d1*d1) / (2.0 * d2 * d3)));
double beta = delta - 0.5 * (PI - theta);
if(2.0 * r * cos(beta) >= d2) {
fit_in_holes[j] = true;
does_fit = true;
}
}
if(does_fit == true) {
cout << "Peg " << letters[i] << " will fit into hole(s):";
for(int k = 0; k < num_holes; k++)
{
if(fit_in_holes[k] == true) {
cout << " " << (k+1);
fit_in_holes[k] = false;
}
}
cout << endl;
} else {
cout << "Peg " << letters[i] << " will not fit into any holes" << endl;
}
}
return 0;
}
Code: Select all
#include <iostream>
#include <cmath>
using namespace std;
#define fo(i,j,k) for (i= j;i<k;i++)
#define rep(i,j,k) for (i=j;i<=k;i++)
#define fod(i,j,k) for(i=j;i>=k;i--)
#define MAXN 101
#define eps 1e-10
//FILE *fin = fopen ("pegs.in","r");
//FILE *fout = fopen ("pegs.out","w");
#define fin stdin
#define fout stdout
int N,M;
double r[MAXN];
bool ok (double a, double b, double c, double d){
double t1,t2,t3;
if(a - d > eps) return false;
t1 = acos((b*b + c*c - a*a )/(2*c*b)) ;
t2 = asin(a/d);
return (t1 - t2>=-eps);
}
int main () {
fscanf (fin,"%d",&M);
int i;
fo(i,0,M) fscanf (fin,"%lf",&r[i]);
fscanf (fin,"%d",&N);
fo(i,0,N) {
double a,b,c,c1,c2,c3;
fscanf (fin, "%lf %lf %lf",&a,&b,&c);
bool mark[M+1];
memset (mark, false , sizeof (mark));
bool found = false;
int j;
fo(j,0,M) {
double t = r[j];
if (ok (a,b,c,t)||ok(b,c,a,t)||ok(c,a,b,t)) {
mark[j] = true;
found = true;
}
}
if (found) {
fprintf (fout,"Peg %c will fit into hole(s): ",(char) 65+i);
fo(j,0,M-1)
if (mark[j]) fprintf (fout,"%d ",j+1);
if(mark[j]) fprintf(fout,"%d",j+1);
}
else fprintf (fout,"Peg %c will not fit into any holes",(char) 65+i);
if (i<N-1)fprintf (fout,"\n");
}
return 0;
}