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
65 0
UVa12478 - Hardest Problem Ever (枚举)
UVa12478 - Hardest Problem Ever (枚举)
54 0
UVa389 - Basically Speaking
UVa389 - Basically Speaking
43 0
|
存储 算法
【转】UVa Problem 100 The 3n+1 problem (3n+1 问题)——(离线计算)
1 // The 3n+1 problem (3n+1 问题) 2 // PC/UVa IDs: 110101/100, Popularity: A, Success rate: low Level: 1 3 // Verdict: Accepted 4 // Submission Date: 2011-05-22 5 // UVa Run Time: 0.032s 6 // 7 // 版权所有(C)2011,邱秋。
1112 0
|
C++
uva 12096 The SetStack Computer
点击打开链接uva 12096 思路: STL模拟 分析: 1 题目给定5种操作,每次输出栈顶集合的元素的个数 2 利用stack和set来模拟,set保存集合的元素。
878 0