首页 > 其他 > 详细

609 - Metal Cutting(几何+暴力)

时间:2014-03-13 09:26:46      阅读:445      评论:0      收藏:0      [点我收藏+]

  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.

bubuko.com,布布扣

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 bubuko.com,布布扣. The next line contains p, the number of vertices in the polygon, where bubuko.com,布布扣. 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;
}


609 - Metal Cutting(几何+暴力),布布扣,bubuko.com

609 - Metal Cutting(几何+暴力)

原文:http://blog.csdn.net/accelerator_/article/details/21135435

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!