标签:style blog http color os 2014
题意:给定一个金属板,上面有一些点,现在有一台切割机,要切割出单调四边形,由所有点组成,问有多少种情况。
思路:递推,设dp[i][j],i为上面点,j为下面点,现在多添加一个点k进来,那么原来的dp[i][j]必然要有一维为k - 1,枚举另外一维就是所有情况。然后再添加点进来的过程中还要考虑能不能加进来,写一个判断函数,把连接线之间所有点枚举一边利用向量叉积去判断即可,如果是上面的线,就不能有点在上面,如果是下面的线,就不能有点再下面。
代码:
#include <stdio.h> #include <string.h> #include <algorithm> using namespace std; const int N = 55; int t, n; struct Point { double x, y; void read() { scanf("%lf%lf", &x, &y); } } p[N]; long long dp[N][N]; bool cmp(Point a, Point b) { return a.x < b.x; } double xmul(Point a1, Point a2, Point b1, Point b2) { double ax = a2.x - a1.x, ay = a2.y - a1.y; double bx = b2.x - b1.x, by = b2.y - b1.y; return ax * by - bx * ay; } bool judge(int s, int e, double flag) { for (int i = s + 1; i < e; i++) { double ans = xmul(p[s], p[i], p[s], p[e]) * flag; if (ans <= 0) return false; } return true; } long long solve() { memset(dp, 0, sizeof(dp)); dp[1][0] = dp[0][1] = 1; for (int i = 2; i < n; i++) { for (int j = 0; j < i - 1; j++) { int k = i - 1; dp[i][j] += dp[k][j]; dp[j][i] += dp[j][k]; } for (int j = 0; j < i - 1; j++) { if (judge(j, i, 1)) dp[i][i - 1] += dp[j][i - 1]; if (judge(j, i, -1)) dp[i - 1][i] += dp[i - 1][j]; } } long long ans = 0; for (int i = 0; i < n - 1; i++) { if (judge(i, n - 1, 1)) ans += dp[i][n - 1]; if (judge(i, n - 1, -1)) ans += dp[n - 1][i]; } return ans; } int main() { scanf("%d", &t); while (t--) { scanf("%d", &n); for (int i = 0; i < n; i++) p[i].read(); sort(p, p + n, cmp); printf("%lld\n", solve() / 2); } return 0; }
UVA 1425 - Metal(递推),布布扣,bubuko.com
标签:style blog http color os 2014
原文地址:http://blog.csdn.net/accelerator_/article/details/36429437