题目地址:http://codeforces.com/problemset/problem/339/C
思路:dfs,当满足条件时放入,不满足则试下一个。
#include<cstdio>
#include<cstdlib>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int m;
char st[15];
vector<int> ans;
void solve(int l,int r,int pre,int tot)
{
if(tot==m)
{
printf("YES\n");
for(int i=0; i<tot-1; i++)
printf("%d ",ans[i]);
printf("%d\n",ans[tot-1]);
exit(0);
}
for(int i=0; i<10; i++)
{
if(!(tot%2)&&(l+i+1>r)&&((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;
}