标签:dfs
2 2 3
12 21 123 132 213 231 312 321
简单深搜;
代码:
#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; }
标签:dfs
原文地址:http://blog.csdn.net/shengweisong/article/details/44164659