算法设计与分析

算法设计与分析

第1关:实验一:凸包问题
/*
8
1 2
-1 -9
3 -4
-2 5
3 8
7 -4
2 -5
-3 -6
*/

#include <stdio.h>
#include <stdlib.h>

#define MAX 100

struct point
{
	double x;
	double y;
};
struct point p[MAX];   // The given points
int pnum;              // The number of the given points
int idex[MAX];         // The indexes of points of convex-hull
int inum = 0;          // The number of the indexes

void input()
{
	int i, j;
	//fp = fopen(".\\closest_pair.txt", "r");
	scanf("%d", &pnum); // First line is the number of points
	// The following lines are the points
	for (i = 0; i < pnum; i++)
		scanf("%lf %lf", &p[i].x, &p[i].y);
	printf("There are %d points:\n", pnum);
	for (j = 0; j < pnum; j++)
		printf("%.0lf,%.0lf\n", p[j].x, p[j].y);
}

void output()
{
	int i;
	printf("The convex-hull is\n");
	for (i = 0; i < inum; i++)
		printf("(%.0lf,%.0lf)\n", p[idex[i]].x, p[idex[i]].y);
}

void addIndex(int idex1, int idex2)  //添加为凸包的索引 
{
	int i;
	int find = 0;  // To indicate whether the two points are collected
	for (i = 0; i < inum; i++)
	{
		if (idex[i] == idex1)
			find += 1;
		if (idex[i] == idex2)
			find += 2;
	}
	printf("find:%d",find); 
	switch (find)
	{
        case 0:      // Both of the points are not collected
            idex[inum] = idex1;
            idex[inum + 1] = idex2;
            inum += 2;
            break;
        case 1:   // Point 1 is collected
            idex[inum] = idex2;
            inum++;
            break;
        case 2:   // Point 2 is collected
            idex[inum] = idex1;
            inum++;
	}
}

void process()
{
	/**********  Begin  **********/
//	在此处填写算法代码
int a,b,c,sign1,sign2;
for(int i=0;i<pnum;i++){
	for(int j=i+1;j<pnum;j++){
		 a=p[j].y-p[i].y;
		 b=p[i].x-p[j].x;
		 c=((p[i].x)*(p[j].y))-((p[i].y)*(p[j].x));
		 sign1=0;
		 sign2=0;
		for(int k=0;k<pnum;k++){
			if((k==j)||(k==i)){
				continue;
			}
			if((a*p[k].x)+(b*p[k].y)==c){
				sign1++;
				sign2++;
			}
			if((a*p[k].x)+(b*p[k].y)>c){
				sign1++;
			}
			if((a*p[k].x)+(b*p[k].y)<c){
				sign2++;
			}
		}
		if(sign1==pnum-2||sign2==pnum-2){
			addIndex(i,j);
		}
	}
}
  
    /**********  End  **********/
}

int main()
{
	input();
	process();
	output();
	return 0;
}
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值