UVa1531 - Problem Bee

简介: UVa1531 - Problem Bee
#include <cstdio>#include <cmath>#include <algorithm>usingnamespacestd;
constdoubleEPS=1e-6;
constdoubleSQRT3=sqrt((double)3);
constintdir[7][2] = {{0, 0}, {-1, 1}, {0, 2}, {1, 1}, {1, -1}, {0, -2}, {-1, -1}};
structPoint{
doublex, y;
};
doubler, xa, ya, xb, yb;
doublestepx, stepy;
boolinput();
PointfindFirstSection(doublex, doubley);
Pointfind(doublex, doubley);
voidsolve();
intmain()
{
#ifndef ONLINE_JUDGEfreopen("d:\\OJ\\uva_in.txt", "r", stdin);
#endifwhile (input()) {
solve();
    }
return0;
}
boolinput()
{
if (scanf("%lf%lf%lf%lf%lf", &r, &xa, &ya, &xb, &yb) !=5||!(r||xa||ya||xb||yb)) returnfalse;
stepx=1.5*r;
stepy=SQRT3/2*r;
returntrue;
}
PointfindFirstSection(doublex, doubley)
{
intdx= (int)(x/stepx);
intdy= (int)(y/stepy);
if (dx&1) {
if (!(dy&1)) dy++;
    } else {
if (dy&1) dx++;
    }
doubleMin=0x3f3f3f3f;
Pointans;
for (inti=0; i<7; i++) {
doubletmpx=stepx* (dx+dir[i][0]);
doubletmpy=stepy* (dy+dir[i][1]);
doubletmp=sqrt(pow(tmpx-x, 2) +pow(tmpy-y, 2));
if (tmp<Min) {
ans.x=tmpx;
ans.y=tmpy;
Min=tmp;
        }
    }
returnans;
}
Pointfind(doublex, doubley)
{
Pointans;
if (x>0&&y>0) {
ans=findFirstSection(x, y);
    } elseif (x<0&&y>0) {
ans=findFirstSection(-x, y);
ans.x=-ans.x;
    } elseif (x<0&&y<0) {
ans=findFirstSection(-x, -y);
ans.x=-ans.x;
ans.y=-ans.y;
    } else {
ans=findFirstSection(x, -y);
ans.y=-ans.y;
    }
returnans;
}
voidsolve()
{
Pointa=find(xa, ya);
Pointb=find(xb, yb);
doubledis;
if (fabs(a.x-b.x) <EPS&&fabs(a.y-b.y) <EPS) {
dis=sqrt(pow(xa-xb, 2) +pow(ya-yb, 2));
printf("%.3lf\n", dis);
return;
    }
intx1= (int)(fabs(a.x-b.x) /stepx+0.5);
intx2= (int)(fabs(a.y-b.y) /stepy+0.5);
intd;
if (x1>=x2) {
d=x1;
     } else {
d=x1+ (x2-x1) /2;
     }
dis=sqrt(pow(a.x-xa, 2) +pow(a.y-ya, 2)) +sqrt(pow(b.x-xb, 2) +pow(b.y-yb, 2)) +d*SQRT3*r;
printf("%.3lf\n", dis);
}
目录
相关文章
uva101 The Blocks Problem
uva101 The Blocks Problem
58 0
UVa12478 - Hardest Problem Ever (枚举)
UVa12478 - Hardest Problem Ever (枚举)
50 0
|
数据挖掘
HDOJ 1032(POJ 1207) The 3n + 1 problem
HDOJ 1032(POJ 1207) The 3n + 1 problem
132 0
HDOJ 2101 A + B Problem Too
HDOJ 2101 A + B Problem Too
105 0
|
数据挖掘
poj-1207 THE 3n+1 problem
Description Problems in Computer Science are often classified as belonging to a certain class of problems (e.
795 0
|
机器学习/深度学习 数据挖掘
HDOJ 1032(POJ 1207) The 3n + 1 problem
Description Problems in Computer Science are often classified as belonging to a certain class of problems (e.
1093 0