3 条题解

  • 1
    @ 2025-5-24 19:16:26
    
    #include <iostream>
    #include <vector>
    #include <stack>
    using namespace std;
    
    vector<string> result;
    int count = 0;
    
    void dfs(int n, int index, stack<int>& s, vector<int>& path) {
        if (count >= 20) return;
        if (path.size() == n) {
            string res;
            for (int num : path) res += to_string(num);
            result.push_back(res);
            count++;
            return;
        }
        
        if (!s.empty()) {
            int temp = s.top();
            s.pop();
            path.push_back(temp);
            dfs(n, index, s, path);
            path.pop_back();
            s.push(temp);
        }
        
        if (index <= n) {
            s.push(index);
            dfs(n, index + 1, s, path);
            s.pop();
        }
    }
    
    int main() {
        int n;
        cin >> n;
        stack<int> s;
        vector<int> path;
        dfs(n, 1, s, path);
        
        for (const auto& res : result) {
            cout << res << endl;
        }
        return 0;
    }
    
    
    • @ 2025-5-24 19:19:29

      666现在题解都这样发??

信息

ID
40
时间
1000ms
内存
128MiB
难度
4
标签
递交数
103
已通过
50
上传者