您的位置:首页 > 其它

poj 1923 dp(Fourier's Lines)

2015-03-31 23:42 155 查看
题意:给定两个正整数n和m,问n条直线交于m个点最多能把平面分成多少部分。要求不会多于两条直线交于一点。

思路:dp。知道是dp可是自己还是在一个地方卡住了,忍不住搜了题解发现也比较好理解。

#include <cstdio>
#include <cstring>
int dp[105][10005];
int n,m,c=1;
int out(int y,int x){
return y<0 || (y>x*(x-1)/2) || (y>0&&y<x-1);
}
int test(int x,int y){
int i;
if(out(y,x))
return 0;
if(!y)
return 1;
if(dp[x][y] != -1)
return dp[x][y];
for(i = 1;i<=x;i++){
dp[x][y] = test(x-i, y-i*(x-i));
if(dp[x][y])
return 1;
}
return 0;
}
int main(){
memset(dp, -1, sizeof(dp));
while(scanf("%d %d",&n,&m) && (n+m)){
printf("Case %d: %d ",c++,n);
if(test(n,m))
printf("lines with exactly %d crossings can cut the plane into %d pieces at most.\n",m,n+m+1);
else
printf("lines cannot make exactly %d crossings.\n",m);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: