NYoj 366 D small L [DFS], nyojdfs
D small L time limit: 4000 MS | memory limit: 65535 KB difficulty: 2
-
Description
-
One day, TC went to the ACM's Xiao L to play with the Three Kingdoms, but this would be a little busy. I don't want to play with Kuang, but I'm afraid of being angry, at this time, Xiao L gave me a question to solve (Xiao L is very D), there is a number of n (0 <n <10), write a full arrangement of 1 to n, at this moment, I am a little confused. Can you help me solve the problem intelligently?
-
Input
-
Enter N (0 <N <10) in the first row, indicating that N groups of test data exist. The next N rows Input Multiple groups of input data, each group of input data is an integer x (0 <x <10)
-
Output
-
Output all combinations in a specific order.
Specific order: Values in each combination are arranged in ascending order, and the combinations are arranged in lexicographically.
-
Sample Input
-
223
-
Sample output
-
1221123132213231312321
Simple deep search;
Code:
#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <cmath>const int M = 20;using namespace std;int n;char ss[M];bool vis[M];const string s = "123456789";void dfs(int step){if(step == n){ss[n] = '\0';cout << ss <<endl; return ;}for(int i = 0; i < n; ++ i){if(!vis[i]){vis[i] = 1;ss[step] = s[i];dfs(step+1);vis[i] = 0;}}}int main(){int t;cin >> t;while(t --){memset(vis, 0, sizeof(vis));cin >> n;int i;for(i = 0; i < n; ++ i){ss[0] = s[i];vis[i] = 1;dfs(1);vis[i] = 0;}}return 0;}