poj 1664 put apples(dfs)

时间:2022-09-06 09:17:50

题目链接:http://poj.org/problem?id=1664

思路分析:数据较小,考虑深度优先搜索搜索解空间。

代码如下:

#include <iostream>
using namespace std; int M, N, Count = ; void dfs( int deep, int x, int put )
{
if ( deep == N )
{
if ( put == M )
Count++;
else
return;
}
else
{
for ( int i = x; i <= M; ++i )
{
if ( put + i > M )
break;
dfs( deep+, i, put+i );
}
}
} int main()
{
int t; cin >> t;
while ( t-- )
{
Count = ; cin >> M >> N;
dfs( , , ); cout << Count << endl;
} return ;
}