poj1923 Fourier's Lines

Posted 王宜鸣

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了poj1923 Fourier's Lines相关的知识,希望对你有一定的参考价值。

思路:

记忆化搜索。

n条直线的交点方案数 
=(n-r)条平行线与r条直线交叉的交点数+r条直线本身的交点方案 
=(n-r)*r+r条直线之间本身的交点方案数(0<r<=n)

于是可以枚举r,递归来计算。

实现:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 
 6 int dp[105][5005];
 7 int dfs(int n, int m)
 8 {
 9     if (m < 0 || m > n * (n - 1) >> 1) return 0;
10     if (!m) return 1;
11     if (dp[n][m] != -1) return dp[n][m];
12     for (int i = 1; i <= n; i++)
13     {
14         if (dfs(n - i, m - i * (n - i)))
15             return dp[n][m] = 1;
16     }
17     return dp[n][m] = 0;
18 }
19 
20 int main()
21 {
22     int kase = 1, n, m;
23     memset(dp, -1, sizeof(dp));
24     while (cin >> n >> m, n + m)
25     {
26         if (dfs(n, m))
27             cout << "Case " << kase++ << ": " << n << " lines with exactly " << m << " crossings can cut the plane into " << n + m + 1 << " pieces at most." << endl;
28         else
29             cout << "Case " << kase++ << ": " << n << " lines cannot make exactly " << m << " crossings." << endl;
30     }
31     return 0;
32 }

 

以上是关于poj1923 Fourier's Lines的主要内容,如果未能解决你的问题,请参考以下文章

POJ 3069 Saruman's Army

[POJ2411]Mondriaan's Dream

poj3207 Ikki's Story IV - Panda's Trick

POJ 3286 How many 0's?(多少0?)

POJ 2411 Mondriaan's Dream

POJ3207 Ikki's Story IV - Panda's Trick 2-sat