HDU 4643 GSM(计算几何)
2016-07-28 17:03
309 查看
题目链接
题目描述:
GSM
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 1260 Accepted Submission(s): 482
Problem Description
Xiao Ming is traveling around several cities by train. And the time on the train is very boring, so Xiao Ming will use the mobile Internet. We all know that mobile phone receives the signal from base station and it will change the base station when moving on the train. Xiao Ming would like to know how many times the base station will change from city A to city B.
Now, the problem is simplified. We assume the route of train is straight, and the mobile phone will receive the signal from the nearest base station.
Input
Multiple cases. For each case, The first line: N(3<=N<=50) - the number of cities, M(2<=M<=50) - the number of base stations. Then there are N cities with coordinates of (x, y) and M base stations with coordinates of (x, y) - (0<=x<=1000, 0<=y<=1000, both x and y is integer).Then there is a number : K, the next, there are K queries, for each query, each line, there are two numbers: a, b.
Output
For each query, tell Xiao Ming how many times the base station will change from city a to city b.
Sample Input
4 4
0 2
1 3
1 0
2 0
1 2
1 1
2 2
2 1
4
1 2
1 3
1 4
3 4
Sample Output
0
1
2
1
Hint
The train way from a to b will not cross the point with the same distance from more than 2 base stations.
(For the distance d1 and d2, if fabs(d1-d2)<1e-7, we think d1 == d2).
And every city exactly receive signal from just one base station.
题解:
在从u->v的路径上,不断分成两段去做。
题目描述:
GSM
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 1260 Accepted Submission(s): 482
Problem Description
Xiao Ming is traveling around several cities by train. And the time on the train is very boring, so Xiao Ming will use the mobile Internet. We all know that mobile phone receives the signal from base station and it will change the base station when moving on the train. Xiao Ming would like to know how many times the base station will change from city A to city B.
Now, the problem is simplified. We assume the route of train is straight, and the mobile phone will receive the signal from the nearest base station.
Input
Multiple cases. For each case, The first line: N(3<=N<=50) - the number of cities, M(2<=M<=50) - the number of base stations. Then there are N cities with coordinates of (x, y) and M base stations with coordinates of (x, y) - (0<=x<=1000, 0<=y<=1000, both x and y is integer).Then there is a number : K, the next, there are K queries, for each query, each line, there are two numbers: a, b.
Output
For each query, tell Xiao Ming how many times the base station will change from city a to city b.
Sample Input
4 4
0 2
1 3
1 0
2 0
1 2
1 1
2 2
2 1
4
1 2
1 3
1 4
3 4
Sample Output
0
1
2
1
Hint
The train way from a to b will not cross the point with the same distance from more than 2 base stations.
(For the distance d1 and d2, if fabs(d1-d2)<1e-7, we think d1 == d2).
And every city exactly receive signal from just one base station.
题解:
在从u->v的路径上,不断分成两段去做。
#include<iostream> #include<cstdio> #include<algorithm> #include<cmath> #include<cstring> using namespace std; const int maxn=50+5; const double eps=1e-8; const double inf=0x3f3f3f3f; int n,m; struct point { double x,y; point(double a=0,double b=0): x(a),y(b) {} }; double len(point a,point b) { return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); } point p1[maxn],p2[maxn]; int find(point p) { double _min=len(p,p2[1]),k=1; for(int i=2;i<=m;i++) { double dis=len(p,p2[i]); if(dis<_min) { k=i; _min=dis; } } return k; } int solve(point a,point b) { int p=find(a); int q=find(b); if(p==q) return 0; else if(len(a,b)<eps) return 1; else { point mid=point((a.x+b.x)/2,(a.y+b.y)/2); return solve(a,mid)+solve(mid,b); } } int main() { while(~scanf("%d%d",&n,&m)) { memset(p1,0,sizeof(p1)); memset(p2,0,sizeof(p2)); for(int i=1;i<=n;i++) scanf("%lf%lf",&p1[i].x,&p1[i].y); for(int i=1;i<=m;i++) scanf("%lf%lf",&p2[i].x,&p2[i].y); int k; scanf("%d",&k); while(k--) { int p,q; scanf("%d%d",&p,&q); printf("%d\n",solve(p1[p],p1[q])); } } }
相关文章推荐
- HTML标签及其属性
- 制作Linux(Fedora、Ubuntu、CentOS)优盘启动 制作Linux(Fedora、Ubuntu、CentOS)优盘启动
- B - Frequent values
- IDF实验室:WEB_一种编码而已
- 新建WEB前端开发技术交流群---期待你的加入
- Welcome to LED Control Wiki
- Ajax 请求 asp.net mvc 的View
- mac nginx 整合tomcat 配置
- OpenCV imgproc模块(二)
- 经典乱码“烫烫烫”和“屯屯屯”
- [置顶] Spring cache +redis
- ROS by Example 第2卷 -- 概括
- nginx+php-fpm出现502 bad gateway错误解决方法__转发
- Leetcode题集——maximum-subarray
- js基础
- 1.安卓开发环境搭建
- UITableView-编辑模式(多选移动删除)
- 156 - Ananagrams
- 告别外卖“黑作坊”,零成本打造千万并发后厨直播
- StringBuilder&& StringBuffer