首页 > 代码库 > poj1923 Fourier's Lines

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