1178 Bumpy Objects问题(虽然是presentation error,没办法了)
其实求解思路倒也不复杂,就是先找到所有顶点的凸包(convex hull),然后找到这些凸包点中每条相邻点连成的直线,同时找到直线在多边形中的端点,如Object1中1,2,5,6这条直线,端点就是1,6。如果重心和这两个点的连线和直线都是锐角的话,那么这条直线就是稳定的,就可以找到题目要求的所谓最小点了。我的代码自己测试是没问题的,但怎么都Presentation Error,无语。找凸包用的是Graham's Scan方法。
[cpp]
#include <iostream>
#include <stack>
#include <vector>
#include <math.h>
#include <iomanip>
using namespace std;
typedef struct point
{
double x;
double y;
int index;//记录点的index
}point;
int cal_max(int index, vector<int>* myv, vector<point>* vertices, point mass);
int find_stable(stack<int>* mys, vector<point>* vertices, point mass);
void find_first(vector<point>* vertices);
void sort(vector<point>* vertices);
bool is_left(int index, stack<int>* mys, vector<point>* vertices);
void find_convex_hull(vector<point>* vertices, stack<int>* mys);
int main()
{
char name[20];
vector<point> vertices;
stack<int> mys;
point mass;
while(cin>>name)
{
vertices.clear();
while(mys.size() > 0)
mys.pop();
if(strcmp(name,"#") == 0)
{
return 0;
}
cin>>mass.x>>mass.y;
point tmp;int index = 0;
while(cin>>tmp.x>>tmp.y)
{
if(tmp.x == 0 && tmp.y == 0)
{
break;
}
tmp.index = ++index;
vertices.push_back(tmp);
}
find_convex_hull(&vertices, &mys);//寻找凸包
int min = find_stable(&mys,&vertices,mass);
cout<<setw(20)<<setiosflags(ios::left)<<name<<setw(1)<<min<<endl;
//cout << name << ' ' << min << endl;
}
}
//寻找凸包的Graham's Scan法
//vertices:点集, mys:凸包的stack
void find_convex_hull(vector<point>* vertices, stack<int>* mys)
{
find_first(vertices);//找到y坐标最小的点,放在第一位
sort(vertices);//根据和y0角度坐标排序
mys->push(0);mys->push(1);mys->push(2);
for(int i = 3; i < vertices->size(); i++)
{
int pos = mys->top();
while(!is_left(i,mys,vertices))
{
mys->pop();
}
mys->push(i);
}
}
//计算myv中index和index+1组成的直线的最大baseline
//myv:凸包中的点集; vertices:点集; mass:重心坐标
//返回最大的baseline
int cal_max(int index, vector<int>* myv, vector<point>* vertices, point mass)
{
int start = index, end = index+1;
if(end == myv->size())
end = 0;
start = myv->at(start);end = myv->at(end);
point left, right;
int max_index = 0, min_x = 65535, max_x = -65535;
for(int i = 0; i < myv->size(); i++)
{
int ano = myv->at(i);
double rate1 = (vertices->at(end).y - vertices->at(start).y)/(vertices->at(end).x - vertices->at(start).x);
double rate2 = (vertices->at(ano).y - vertices->at(start).y)/(vertices->at(ano).x - vertices->at(start).x);
if(vertices->at(ano).x == vertices->at(start).x)
rate2 = (vertices->at(ano).y - vertices->at(end).y)/(vertices->at(ano).x - vertices->at(end).x);
if(vertices->at(ano).x == vertices->at(start).x && vertices->at(end).x == vertices->at(start).x)
rate2 = rate1 = 1;
if(rate1 == rate2)
{
if(vertices->at(ano).index > max_index)
max_index = vertices->at(ano).index;
if(vertices->at(ano).x > max_x)
{
max_x = vertices->at(ano).x;
right = vertices->at(ano);
}
if(vertices->at(ano).x < min_x)
&
补充:软件开发 , C++ ,