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