第三题: #include <iostream> #include <string> #include <vector> using namespace std; void dfs(vector<int> & flags,int start,vector<string> & ans,string tmp) {     if (start == flags.size()) {         ans.push_back(tmp);         return;     }     dfs(flags,start+1,ans,tmp+to_string(start));     if(flags[start] == 0)         dfs(flags, start + 1, ans, tmp);          } int main(void) {     int tmp;     while(cin>>tmp) {         vector<int> flags;         vector<string> ans;         flags.push_back(tmp);         for (int i = 1; i < 10;++i) {             cin >> tmp;             flags.push_back(tmp);         }         dfs(flags, 0, ans, ""); sort(ans.begin(),ans.end());       for (auto x : ans)             cout << x << endl;     } } 使用了backtrace,注意解空间是类似于二叉树,当然也可以使用数组的解空间,但是比较繁琐。
点赞 评论

相关推荐

牛客网
牛客企业服务