UVALive 4426 Blast the Enemy! 计算几何求重心
Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu
Description

A new computer game has just arrived and as an active and always-in-the-scene player, you should finish it before the next university term starts. At each stage of this game, you have to shoot an enemy robot on its weakness point. The weakness point of a robot is always the ``center of mass" of its 2D shape in the screen. Fortunately, all robot shapes are simple polygons with uniform density and you can write programs to calculate exactly the center of mass for each polygon.
Let's have a more formal definition for center of mass (COM). The center of mass for a square, (also circle, and other symmetric shapes) is its center point. And, if a simple shape C is partitioned into two simple shapes A and B with areas SA and SB , then COM(C) (as a vector) can be calculated by

As a more formal definition, for a simple shape A with area SA :

Input
The input contains a number of robot definitions. Each robot definition starts with a line containing n , the number of vertices in robot's polygon (n100) . The polygon vertices are specified in the next n lines (in either clockwise or counter-clock-wise order). Each of these lines contains two space-separated integers showing the coordinates of the corresponding vertex. The absolute value of the coordinates does not exceed 100. The case of n = 0 shows the end of input and should not be processed.
Output
The i -th line of the output should be of the form `` Stage #i:x y " (omit the quotes), where ( x, y ) is the center of mass for the i -th robot in the input. The coordinates must be rounded to exactly 6 digits after the decimal point.
Sample Input
4
0 0
0 1
1 1
1 0
3
0 1
1 0
2 2
8
1 1
2 1
2 7
3 7
3 0
0 0
0 7
1 7
0
Sample Output
Stage #1: 0.500000 0.500000
Stage #2: 1.000000 1.000000
Stage #3: 1.500000 3.300000
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 1001
const int inf=0x7fffffff; //无限大
int main()
{
int N;
double x[maxn],y[maxn],a[maxn],ax[maxn],ay[maxn],xg=,yg=,a1=,b1=,c=;
int t=;
while(cin>>N){
xg=,yg=,a1=,b1=,c=;
t++;
int i,n;
if(N==)
break;
for(i=;i<N;i++)
{
scanf("%lf %lf",&x[i],&y[i]);
}
for(i=;i<N-;i++)
{
a[i]=(y[i+]+y[i])*(x[i]-x[i+])/2.0;
ax[i]=(x[i+]*x[i+]+x[i+]*x[i]+x[i]*x[i])*(y[i+]-y[i])/6.0;
ay[i]=(y[i+]*y[i+]+y[i+]*y[i]+y[i]*y[i])*(x[i]-x[i+])/6.0;
}
a[N-]=(y[]+y[N-])*(x[N-]-x[])/2.0;
ax[N-]=(x[]*x[]+x[]*x[N-]+x[N-]*x[N-])*(y[]-y[N-])/6.0;
ay[N-]=(y[]*y[]+y[]*y[N-]+y[N-]*y[N-])*(x[N-]-x[])/6.0;
for(i=;i<N;i++)
{
a1=a1+ax[i];
b1=b1+a[i];
c=c+ay[i];
}
xg=a1/b1;
yg=c/b1;
printf("Stage #%d: %.6lf %.6lf\n",t,xg,yg);
}
return ;
}
UVALive 4426 Blast the Enemy! 计算几何求重心的更多相关文章
- UVALive 4426 Blast the Enemy! --求多边形重心
题意:求一个不规则简单多边形的重心. 解法:多边形的重心就是所有三角形的重心对面积的加权平均数. 关于求多边形重心的文章: 求多边形重心 用叉积搞一搞就行了. 代码: #include <ios ...
- hdu-1115 计算几何 求重心 凸多边形 面积
思想是分割成三角形,然后求三角形的重心.那么多边形重心就是若干个三角形的重心带权求中心,可以用质点质心公式. #include <cstdio> #include <iostream ...
- Lifting the Stone 计算几何 多边形求重心
Problem Description There are many secret openings in the floor which are covered by a big heavy sto ...
- UVALive 4262——Trip Planning——————【Tarjan 求强连通分量个数】
Road Networks Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Stat ...
- hdu1115【多边形求重心模板】
1.质量集中在顶点上.n个顶点坐标为(xi,yi),质量为mi,则重心(∑( xi×mi ) / ∑mi, ∑( yi×mi ) / ∑mi) 2.质量分布均匀.这个题就是这一类型,算法和上面的不同. ...
- POJ 3855 计算几何·多边形重心
思路: 多边形面积->任选一个点,把多边形拆成三角,叉积一下 三角形重心->(x1+x2+x3)/3,(y1+y2+y3)/3 多边形重心公式题目中有,套一下就好了 计算多边形重心方法: ...
- 多边形求重心 HDU1115
http://acm.hdu.edu.cn/showproblem.php?pid=1115 引用博客:https://blog.csdn.net/ysc504/article/details/881 ...
- UVALive 7146 Defeat the Enemy(贪心+STL)(2014 Asia Shanghai Regional Contest)
Long long ago there is a strong tribe living on the earth. They always have wars and eonquer others. ...
- 计算几何--求凸包模板--Graham算法--poj 1113
Wall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 28157 Accepted: 9401 Description ...
随机推荐
- linux 获取时间后--自定义时间格式
自定义时间格式 =================================-===================================== #include <stdio.h ...
- shell监控网站是否自动运行并自动重启【原创】
shell监控网站是否自动运行并自动重启 #!/bin/bash -T www.baidu.com ];then echo "`date` 网站访问正常!" >> /r ...
- Nginx/LVS/HAProxy负载均衡软件的优缺点详解【转】
转自 (总结)Nginx/LVS/HAProxy负载均衡软件的优缺点详解http://www.ha97.com/5646.html PS:Nginx/LVS/HAProxy是目前使用最广泛的三种负载均 ...
- JavaScript新手学习笔记(一)
1.JavaScript 对大小写敏感. JavaScript 对大小写是敏感的. 当编写 JavaScript 语句时,请留意是否关闭大小写切换键. 函数 getElementById 与 getE ...
- PHP获得用户的真实IP地址
<?php /** * 获得用户的真实IP地址 * * @access public * @return string */ function real_ip() { static $reali ...
- android入门问题--R文件丢失
链接 新手刚入门as,发现新创建的项目总是出错 Error:Execution failed for task ':app:mergeDebugResources'. > Error: ja ...
- Spring框架(管理事务)
Spring底层使用Transaction事物模板来进行操作.具体操作: 1.service 需要获得 TransactionTemplate 2.spring 配置模板,并注入给service 3. ...
- 异步消息框架netty
Netty-WebSocket长连接推送服务 http://blog.csdn.net/z69183787/article/details/52505249 http://blog.csdn.net/ ...
- Codeforces 552C Vanya and Scales(进制转换+思维)
题目链接:http://codeforces.com/problemset/problem/552/C 题目大意:有101个砝码重量为w^0,w^1,....,w^100和一个重量为m的物体,问能否在 ...
- 学习python绘图
学会python画图 # 使用清华的pip源进行安装sklearn # pip install -i https://pypi.tuna.tsinghua.edu.cn/simple -U sciki ...