NYoj 366 D small L [DFS], nyojdfs

Source: Internet
Author: User

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;}        


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.