Codeforces Round #197 (Div. 2) C. Xenia and Weights(DFS)

题目地址:http://codeforces.com/problemset/problem/339/C

思路:dfs,当满足条件时放入,不满足则试下一个。
#include
#include
#include
#include
#include
#include
using namespace std;
int m;
char st[15];
vector ans;
void solve(int l,int r,int pre,int tot)
{
    if(tot==m)
    {
        printf("YES\n");
        for(int i=0; ir)&&((i+1)!=pre)&&(st[i]=='1'))
        {
            ans.push_back(i+1);
            solve(l+i+1,r,i+1,tot+1);
            ans.pop_back();
        }
        if((tot%2)&&(r+i+1>l)&&((i+1)!=pre)&&(st[i]=='1'))
        {
            ans.push_back(i+1);
            solve(l,r+i+1,i+1,tot+1);
            ans.pop_back();
        }
    }
}
int main()
{
    scanf("%s%d",st,&m);
    solve(0,0,0,0);
    printf("NO\n");
    return 0;
}



你可能感兴趣的:(OJ_Codeforces,搜索_DFS,ACM)