a229. ๆ‹ฌ่™ŸๅŒน้…ๅ•้กŒ 5/16/2025

่งฃๆณ•ไธ€ใ€DFS

๐Ÿ”น ๅ‚ณ้žๅƒ่€ƒ

ๅ‚ณ้žๅƒ่€ƒ string& s
่€Œไธๆ˜ฏๅ‚ณ้žๆ•ดๅ€‹ๅญ—ไธฒ string s
้ฟๅ…ไธๅฟ…่ฆ็š„ๅญ—ไธฒ่ค‡่ฃฝ

โœ… ๅฎŒๆ•ดไปฃ็ขผ

่ฉ•ๅˆ†็ตๆžœ(ๅƒ่€ƒ) ๏ผš AC (0.2s, 324KB)

#include <bits/stdc++.h>
using namespace std;

int n;

void dfs(int left, int right, string& s){
    if(left+right == 2*n){
        cout << s << "\n";
        return;
    }
    
    if(left < n){
        s[left+right] = '(';
        dfs(left+1, right, s);
    }
    if(right < left){
        s[left+right] = ')';
        dfs(left, right+1, s);
    }
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    while(cin >> n){
        string str(2*n, '_');
        dfs(0, 0, str);
        cout << "\n";
    }

    return 0;
}