poj 1664 put apples(dfs)

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

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

 

代码如下:

#include <iostream>
using namespace std;

int M, N, Count = 0;

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+1, i, put+i );
        }
    }
}

int main()
{
    int t;

    cin >> t;
    while ( t-- )
    {
        Count = 0;

        cin >> M >> N;
        dfs( 0, 0, 0 );

        cout << Count << endl;
    }

    return 0;
}

 

你可能感兴趣的:(apple)