fork download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. static const int N = 6;
  5. static const int M = 6;
  6.  
  7. typedef std::vector<int> vec;
  8. typedef std::vector<std::vector<int>> vecvec;
  9.  
  10. static const int U[M][N] = {{1, 12, 10, 8, 9, 3},
  11. {14, 16, 17, 6, 2, 15},
  12. {12, 1, 13, 2, 4, 18},
  13. {13, 9, 9, 10, 13, 12},
  14. {0, 2, 17, 14, 7, 11},
  15. {19, 3, 2, 19, 0, 1}};
  16.  
  17. vecvec assign(int n,int m, vec ne, vec po, vecvec ot, int j) {
  18. for (int i{}; i < m; i++) {
  19. if(po[i] == 0) {
  20. po[i] = 1;
  21. ne[j] = i;
  22. ne[n] += U[ne[j]][j];
  23. if (j < n-1) ot = assign(n, m, ne, po, ot, j+1);
  24. if(ne[n] >= ot[0][n]) {
  25. if(ne[n] > ot[0][n]) ot.clear();
  26. ot.push_back(ne);
  27. }
  28. else {
  29. po[i] = 0;
  30. ne[n] -= U[ne[j]][j];
  31. }
  32. }
  33. }
  34.  
  35.  
  36. return ot;
  37. }
  38.  
  39. vecvec assign() {
  40. return assign( N, M, vec(N+1, 0), vec(N+1, 0), vecvec(1,vec(N+1, 0)), 0);
  41. }
  42.  
  43.  
  44. int main() {
  45. // your code goes here
  46.  
  47. vecvec ot = assign();
  48.  
  49. for (auto v : ot) {
  50. for(int i : v) std::cout << i << ' ';
  51. std::cout << std::endl;
  52. }
  53.  
  54. return 0;
  55. }
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
1 0 4 5 3 2 93 
5 0 1 4 3 2 93