首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

PKU 1113 Wall

2012-11-12 
PKU 1113 Wall .Your task is to help poor Architect to save his head, by writing a program that will

PKU 1113 Wall .

Your task is to help poor Architect to save his head, by writing a program that will find the minimum possible length of the wall that he could build around the castle to satisfy King's requirements.

The task is somewhat simplified by the fact, that the King's castle has a polygonal shape and is situated on a flat ground. The Architect has already established a Cartesian coordinate system and has precisely measured the coordinates of all castle's vertices in feet.

#include <iostream>#include <vector>#include <cmath>#include <algorithm>using namespace std;#define MAXI 1011#define sq(x) ((x) * (x)) //Squarestruct pt { double x, y; } ps[MAXI];vector<pt> st; //Pseudo-Stack {= =}int n, len;double l;double dlt(pt &a, pt &b, pt &o) //Cross-Product{return (o.x - a.x) * (b.y - a.y) - (b.x - a.x) * (o.y - a.y);}double dis(pt &a, pt &b) //Distance{return sqrt(sq(a.x - b.x) + sq(a.y - b.y));}bool pac(pt &a, pt &b) //Polar-Angle-Compare{double delta = dlt(a, b, ps[0]);if ( delta == 0 && sq(a.x - ps[0].x) + sq(a.y - ps[0].y) > sq(b.x - ps[0].x) + sq(b.y - ps[0].y))return 1;else if (delta < 0)return 1;return 0;}void pas() //Polar-Angle-Sort{int i, id;for (id = 0, i = 1; i < n; i++)if ((ps[i].y == ps[id].y && ps[i].x < ps[id].x) || ps[i].y < ps[id].y)id = i;swap(ps[0], ps[id]);sort(ps + 1, ps + n, pac);#if 0for (i = 0; i < n; i++)printf("p%d = [%lf, %lf]\n", i, ps[i].x, ps[i].y);#endif}void ghs() //Graham's-Scan{int i;pas();len = 2;st.clear();st.push_back(ps[0]);st.push_back(ps[1]);for (i = 2; i < n; i++)if (dlt(st[1], ps[i], st[0]) != 0)break;st.push_back(ps[2]);for (i++; i < n; i++){while (dlt(st[len], ps[i], st[len - 1]) >= 0){st.pop_back();len--;}st.push_back(ps[i]);len++;}len++;#if 0for (i = 0; i < len; i++)printf("p%d = [%lf, %lf]\n", i, st[i].x, st[i].y);#endif}double sta() //Statistic{int i;double tsu = 0.0;for (i = 0; i < len; i++)tsu += dis(st[i % len], st[(i + 1) % len]);#if 0printf("%lf\n", dlt(st[1], st[2], st[0]) / (dis(st[0], st[1]) * dis(st[1], st[2])));#endifreturn tsu + 4.0 * asin(1.0) * l;}int main(){int i;#if 0printf("%lf\n", 4.0 * asin(1.0) * 100);#endifwhile (scanf("%d%lf", &n, &l) != EOF){for (i = 0; i < n; i++)scanf("%lf%lf", &ps[i].x, &ps[i].y);ghs();printf("%d\n", (int)(sta() + 0.5));}return 0;}

?

第一次用代码控件…………

今日学到好多几何野,想写好多野,以后再写写吧,关于几何既野{=__.=+}大家都可以去101majia既关于PKU1113题目文章果度参考下噶!地址呢度http://gzhu-101majia.iteye.com/blog/1128755

热点排行