609 - Metal Cutting(几何+暴力)

  Metal Cutting 

In order to build a ship to travel to Eindhoven, The Netherlands, various sheet metal parts have to be cut from rectangular pieces of sheet metal. Each part is a convex polygon with at most 8 vertices. Each rectangular piece of sheet metal has width n and height m, so that the four corners of the sheet can be specified by the Cartesian coordinates (0, 0), (0, m), (nm) and (n, 0) in clockwise order. The cutting machine available can make only straight-line cuts completely through the metal. That is, it cannot cut halfway through the sheet, turn, and then cut some more. You are asked to write a program to determine the minimum total length of cuts this machine has to make in order to cut out the polygon. The cuts must be along the edges of the poligon.


For example, if n = m = 100, and the polygon has vertices (80, 80), (70, 30), (20, 20) and (20, 80), the following diagram shows the optimal cut (the thick lines). The numbers show the order in which the cuts are made.

Input 

The first line of the input is an integer N, then a blank line followed by N datasets. There is a blank line between datasets.

The first line of each dataset contains the two integers n and m where $0 < n, m \le 500$. The next line contains p, the number of vertices in the polygon, where $3 \le p \le 8$. Each of the next p lines contains two integers x and y where 0 < x < n and 0 < y < m, specifying the vertices of the polygon. The vertices are listed in clockwise order. You may assume that the polygon does not intersect itself, and that no three consecutive vertices are colinear.

Output 

For each dataset, print the minimum total length of cuts required to cut out the given polygon, accurate to 3 decimal places. Print a blank line between datasets.

Sample Input 

1

100 100
4
80 80
70 30
20 20
20 80

Sample Output 

Minimum total length = 312.575

题意:一个矩形上面有n个点,组成n条直线,现在求一个切割顺序,使得切割长度最小。

思路:最多8条直线,直接暴力切割顺序,然后去求,求的过程中,每切一条直线就添加进去,然后求长度的时候遍历一遍现有直线,然后就是几何问题了。

利用叉积判断点是否是可取点,然后在这些点找最小距离的点,这样可以求出两个交点,即求出长度。

代码:

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
const double esp = 1e-9;
using namespace std;
struct Point {
	double x, y;
	Point() {}
	Point(double xx, double yy) {
		x = xx; y = yy;
	}
} p[10];

struct Line {
	Point a, b;
	double A, B, C;
	Line() {}
	Line(Point aa, Point bb) {
		a = aa; b = bb;
		A = b.y - a.y;
		B = a.x - b.x;
		C = (A * a.x + B * a.y);
	}
} l[15], save[15];

inline bool px(Line a, Line b) {
	if (fabs((a.b.y - a.a.y) * (b.b.x - b.a.x) - (b.b.y - b.a.y) * (a.b.x - a.a.x)) < esp) return true;
	return false;
}

inline Point jd(Line a, Line b) {
	Point pp((b.B * a.C - a.B * b.C) / (a.A * b.B - b.A * a.B) ,(a.A * b.C - b.A * a.C) / (a.A * b.B - b.A * a.B));
	return pp;
}

int t, P, ln, sn, i, ord[10];
double n, m;

inline double dis(Point a, Point b) {
	return sqrt((b.y - a.y) * (b.y - a.y) + (b.x - a.x) * (b.x - a.x));
}

inline bool outside(Point a, Point b, Point c) {
	return (c.x - a.x) * (b.x - a.x) + (c.y - a.y) * (b.y - a.y) > esp;
}

inline double find(Line li) {
	double mina = 2000000000, minb = 2000000000;
	Point aa, bb;
	for (int i = 0; i < sn; i++) {
		if (px(li, save[i])) continue;
		Point c = jd(li, save[i]);
		double disa = dis(li.a ,c), disb = dis(li.b , c);
		if (outside(li.b, li.a, c) && disa - mina < esp) {
			mina = disa;
			aa = c;
		}
		if (outside(li.a, li.b, c) && disb - minb < esp) {
			minb = disb;
			bb = c;
		}
	}
	return dis(aa, bb);
}

inline double cal() {
	double sum = 0;
	sn = 4;
	for (int i = 0; i < P; i++) {
		sum += find(l[ord[i]]);
		save[sn++] = l[ord[i]];
	}
	return sum;
}

inline double solve() {
	double ans = 2000000000;
	save[sn++] = Line(Point(0, 0), Point(0, m));
	save[sn++] = Line(Point(0, 0), Point(n, 0));
	save[sn++] = Line(Point(n, 0), Point(n, m));
	save[sn++] = Line(Point(0, m), Point(n, m));
	do {
		double t = cal();
		ans = min(ans, t);
	} while (next_permutation(ord, ord + P));
	return ans;
}

int main() {
	scanf("%d", &t);
	while (t--) {
		ln = 0, sn = 0;
		scanf("%lf%lf", &n, &m);
		scanf("%d", &P);
		for (i = 0; i < P; i++) {
			ord[i] = i;
			scanf("%lf%lf", &p[i].x, &p[i].y);
			if (i != 0)
				l[ln++] = Line(Point(p[i - 1].x, p[i - 1].y), Point(p[i].x, p[i].y));
		}
		l[ln++] = Line(Point(p[P - 1].x, p[P - 1].y), Point(p[0].x, p[0].y));
		printf("Minimum total length = %.3lf\n", solve());
		if (t) printf("\n");
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值