fork download
  1. #include <iostream>
  2. #include <math.h>
  3. #include <stdlib.h>
  4. #include <sstream>
  5. #include <bits/stdc++.h>
  6. #include <string>
  7. #include <string.h>
  8. #include <vector>
  9. #include <sstream>
  10. #include <stdio.h>
  11. #include <time.h>
  12. #include <algorithm>
  13. #include <iterator>
  14. using namespace std;
  15.  
  16.  
  17. void printvector(vector<vector<int>> v){
  18. for(int i=0;i<v.size();i++)
  19. {
  20. for(int j=0;j<v[i].size();j++)
  21. {
  22. cout<<v[i][j]<<" ";
  23. }
  24. cout << endl;
  25. }
  26. }
  27.  
  28. void dfs(int layer,int time,vector<int> &arrangement,bool* visited,vector<int> candidate,vector<vector<int>> &path){
  29. if (layer == time){
  30. /*for (int i = 0; i < time; i++){
  31.   cout << arrangement[i] << "\t";
  32.   }
  33.   cout << endl;
  34.   */
  35. path.push_back(arrangement);
  36. //for(int i=0; i<path.size(); i++) cout << path.at(i) << " ";
  37. return;
  38. }
  39. for (int i = 0; i < time; i++){
  40. if (visited[i]){
  41. continue;
  42. }
  43. visited[i] = true;
  44. arrangement.resize(layer+1);
  45. arrangement.at(layer) = candidate[i];
  46. dfs(layer + 1,time,arrangement,visited,candidate,path);
  47. visited[i] = false;
  48. }
  49. }
  50.  
  51. //窮舉
  52. vector<vector<int>> generate_path(vector<int> candidate,int time){
  53.  
  54. vector<vector<int>>path;
  55. bool visited[time] = {false};
  56. vector<int> arrangement;
  57. dfs(0,time,arrangement,visited,candidate,path);
  58. return path;
  59. //path.push_back(dfs(0,time,arrangement,visited,candidate));
  60. //for(int i=0; i<path.size(); i++) cout << path.at(i) << " ";
  61.  
  62. }
  63.  
  64. int main()
  65. {
  66. vector<int> candidate;
  67. candidate.push_back(10105);
  68. candidate.push_back(10732);
  69. candidate.push_back(10865);
  70. //vector<vector<int>>path;
  71. vector<vector<int>>v;
  72. v=generate_path(candidate,3);
  73. printvector(v);
  74. }
Success #stdin #stdout 0s 4160KB
stdin
Standard input is empty
stdout
10105 10732 10865 
10105 10865 10732 
10732 10105 10865 
10732 10865 10105 
10865 10105 10732 
10865 10732 10105