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);
}
目录
相关文章
uva 11991 - Easy Problem from Rujia Liu?
这个题目的意思是输入n个数,m组询问,每组询问包含两个整数k,v,意思是询问整数v第k次出现的位置。
39 0
UVa389 - Basically Speaking
UVa389 - Basically Speaking
36 0
uva101 The Blocks Problem
uva101 The Blocks Problem
53 0
|
机器学习/深度学习 自然语言处理
|
人工智能 Java 安全
HDU 1039 Easier Done Than Said?
Easier Done Than Said? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 12751    Accepted Subm...
801 0
|
数据挖掘
poj-1207 THE 3n+1 problem
Description Problems in Computer Science are often classified as belonging to a certain class of problems (e.
792 0