http://chino.taipei/note-2016-0311C-%E7%9A%84%E8%BC%B8%E5%87%BA%E5%85%A5cin-cout%E5%92%8Cscanf-printf%E8%AA%B0%E6%AF%94%E8%BC%83%E5%BF%AB%EF%BC%9F/
順便分享一下程式碼:
#include <iostream>
#include <string>
#include <cstring>
#include <stdio.h>
using namespace std;
void dfs(int il, int ir, string s, int n){
//括號順序不對
if (il < ir){
return;
}
//存在解答,跳出DFS
if (il==ir && il==n){
cout << s << "\n"; <=0.5s, 280KB
//cout << s << endl; <=逾時
return;
}
//左括號先做完
if (il < n){
dfs(il+1 , ir , s+"(" , n);
}
//換右括號
if (ir < n){
dfs(il , ir+1 , s+")" , n);
}
}
int main()
{
int n;
while(cin >> n){
dfs(0, 0, "", n);
}
return 0;
}