CodeForces 438 C.The Child and Polygon(dp+计算几何)
来源:互联网 发布:淘宝网店第三方推广 编辑:程序博客网 时间:2024/05/23 13:08
Description
给出一个简单多边形,问其三角剖分方案
Input
第一行一整数n表示点数,之后顺时针或逆时针给出这n个点的坐标(3<=n<=200,|xi|,|yi|<=1e7)
Output
输出三角剖分方案数,结果模1e9+7
Sample Input
4
0 0
0 1
1 1
1 0
Sample Output
2
Solution
假设点逆时针排好并编号0~n-1,dp[i][j]表示i~j这些点构成的简单多边形的三角剖分数(i < j),每次从i+1~j-1这些点中找出一个k与i和j组成一个三角形,那么这个简单多边形就被分成i,i+1,..,k构成的简单多边形和k,k+1,…,j构成的简单多边形,进而有转移方程dp[i][j]=sum(dp[i][k]*dp[k][j]),其中i+1<=k<=j-1,且k的选择需使得由i,j,k三点构成的三角形与划分出来的两个简单多边形不会相交,判这个太麻烦,不如每次都选择向量ij右边的点k,这样就算相交出现不合法情况在之后的转移中迟早会找不到一个合法的k以供转移进而不会对答案有不合法的贡献
Code
#include<cstdio>#include<iostream>#include<cstring>#include<algorithm>#include<cmath>#include<vector>#include<queue>#include<map>#include<set>#include<ctime>using namespace std;typedef long long ll;#define INF 0x3f3f3f3f#define maxn 222struct node{ ll x,y; node operator-(const node&b)const { node c; c.x=x-b.x,c.y=y-b.y; return c; } ll operator*(const node&b)const { return x*b.y-y*b.x; }}p[maxn];const ll mod=1e9+7ll;ll dp[maxn][maxn];int n;ll dfs(int i,int j){ if(dp[i][j]!=-1)return dp[i][j]; if(i+1==j)return dp[i][j]=1; ll ans=0; for(int k=i+1;k<j;k++) if((p[k]-p[i])*(p[j]-p[i])>0) ans=(ans+dfs(i,k)*dfs(k,j)%mod)%mod; return dp[i][j]=ans;}int main(){ while(~scanf("%d",&n)) { memset(dp,-1,sizeof(dp)); for(int i=0;i<n;i++)scanf("%I64d%I64d",&p[i].x,&p[i].y); ll s=0; for(int i=0;i<n;i++)s+=p[i]*p[(i+1)%n]; if(s<0) for(int i=0,j=n-1;i<j;i++,j--)swap(p[i],p[j]); printf("%I64d\n",dfs(0,n-1)); } return 0;}
0 0
- CodeForces 438 C.The Child and Polygon(dp+计算几何)
- [几何 区间DP] Codeforces 438C & 437E. The Child and Polygon
- Codeforces 437E The Child and Polygon(区间DP)
- Codeforces 437E The Child and Polygon(区间DP) 2017/1/10
- Codeforces 755-D. PolandBall and Polygon(树状数组+计算几何)
- codeforces 437C The Child and Toy
- CodeForces 437C The Child and Toy
- Codeforces 437C The Child and Toy
- codeforces-437C-The Child and Toy
- CodeForces 437C The Child and Toy
- Codeforces-437C-The Child and Toy
- CodeForces 437C The Child and Toy
- Codeforces 703C Chris and Road(计算几何)
- CodeForces 438D The Child and Sequence
- 【Codeforces 438 D】The Child and Sequence
- Codeforces-274C:The Last Hole!(计算几何)
- CodeForces The Child and Set
- Codeforces #250 (Div. 2) C.The Child and Toy
- ashx文件获取$.ajax()方法发送的数据
- 在使用SQLServer时忘记sa账号密码解决办法
- 查看sqlserver2008数据库服务器实例名称
- js中几种实用的跨域方法原理详解
- 关于在Struts2的Action中使用domain模型接收参数的问题
- CodeForces 438 C.The Child and Polygon(dp+计算几何)
- mysql添加新用户,限制用户访问权限
- DNS服务器
- 使用@Controller注解为什么要配置<mvc:annotation-driven />
- Java开发中的一些小技巧
- java.io中的System.in、 System.out和System.err
- vs2008发布项目失败的解决方法
- 20170412STL013_STL_非变动型算法
- 2000年清华大学计算机研究生机试真题(Java自定义容器类的排序)