uva10112 Myacm Triangles

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

#define LOCAL

typedef struct Point
{
    char label;
    int x;
    int y;
}point;

point Points[20];
int search[500][3];
int c;
int temp[3];
int visited[30];


void dfs(int pos, int n);
double area(point a, point b, point c);
int inside_triangle(point a, point b, point c, point d);
int cross(point a, point b, point c);

int main()
{
    int tests;
    int i, j;
    point triangle[3];
    int nodeVis[20];
    double s ;
    int ok;
    double temp;
    char ans[3];


    #ifdef LOCAL
        freopen("c://uva_in.txt", "r", stdin);
    #endif

    while (scanf("%d", &tests) && tests != 0)
    {
        c = 0;
        s = -1.0;
        memset(visited, 0, sizeof(visited));
        dfs(0, tests);

        fgetc(stdin);
        for (i = 1; i <= tests; i++)
        {
            scanf("%c%d%d", &(Points[i].label), &(Points[i].x), &(Points[i].y));
            fgetc(stdin);
        }

        for (i = 0; i < c; i++)
        {
            memset(nodeVis, 0, sizeof(nodeVis));
            for (j = 0; j < 3; j++)
            {
                nodeVis[search[i][j]] =1;
                triangle[j].label = Points[search[i][j]].label;
                triangle[j].x = Points[search[i][j]].x;
                triangle[j].y = Points[search[i][j]].y;
            }

            ok = 1;
            for (j = 1; j <= tests; j++)
            {
                if (!nodeVis[j])
                {
                    if (inside_triangle(triangle[0], triangle[1], triangle[2], Points[j]))
                    {
                        ok = 0;
                        break;
                    }
                }
            }


            temp = area(triangle[0], triangle[1], triangle[2]);

            if (ok && (temp > s))
            {
                s = temp;
                ans[0] = triangle[0].label;
                ans[1] = triangle[1].label;
                ans[2] = triangle[2].label;

            }
        }

        for (i = 0; i < 3; i++)
            printf("%c", ans[i]);
        printf("/n");
    }
    return 0;
}

void dfs(int pos, int n)
{
    int i;

    if (pos == 3)
    {
        search[c][0] = temp[0];
        search[c][1] = temp[1];
        search[c][2] = temp[2];
        c++;
    } else
    {
        for (i = 1; i <= n; i++)
        {
            if (!visited[i] && (i > temp[pos - 1]))
            {
                visited[i] = 1;
                temp[pos] = i;
                dfs(pos + 1, n);
                visited[i] = 0;
            }
        }
    }
}

double area(point a, point b, point c)
{
    int x1, y1, x2, y2;
    double s;

    x1 = b.x - a.x;
    y1 = b.y - a.y;

    x2 = c.x - a.x;
    y2 = c.y - a.y;

    s =  fabs(x1 * y2 - y1 * x2) / 2.0;

    return s;
}

int inside_triangle(point a, point b, point c, point d)
{
    if ((cross(a, b, d) >= 0 && cross(b, c, d) >= 0 && cross(c, a, d) >= 0) || (cross(a, b, d) <= 0 && cross(b, c, d) <= 0 && cross(c, a, d) <= 0))
        return 1;
    else
        return 0;
}

int cross(point a, point b, point c)
{
    int x1, y1, x2, y2;
    int temp;

    x1 = b.x - a.x;
    y1 = b.y - a.y;

    x2 = c.x - a.x;
    y2 = c.y - a.y;

    temp = x1 * y2 - x2 * y1;

    return temp;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值