fork(1) download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int *sel;
  5. int *chk;
  6. const int NUM = 4;
  7.  
  8. void dfs(int n)
  9. {
  10. if( n == NUM )
  11. {
  12. for( int i = 0 ; i < NUM ; ++i )
  13. cout << sel[i] << " ";
  14. cout << endl;
  15. }
  16. else
  17. {
  18. for( int i = 0 ; i < NUM ; ++i )
  19. {
  20. if( chk[i] == 0 )
  21. {
  22. chk[i] = 1;
  23. sel[n] = i;
  24. dfs(n+1);
  25. chk[i] = 0;
  26. }
  27. }
  28. }
  29. }
  30.  
  31. int main() {
  32. sel = new int[NUM];
  33. chk = new int[NUM];
  34. for( int i = 0 ; i < NUM ; ++i )
  35. sel[i] = chk[i] = 0;
  36.  
  37. dfs(0);
  38.  
  39. delete[] sel;
  40. delete[] chk;
  41. return 0;
  42. }
Success #stdin #stdout 0s 3428KB
stdin
Standard input is empty
stdout
0 1 2 3 
0 1 3 2 
0 2 1 3 
0 2 3 1 
0 3 1 2 
0 3 2 1 
1 0 2 3 
1 0 3 2 
1 2 0 3 
1 2 3 0 
1 3 0 2 
1 3 2 0 
2 0 1 3 
2 0 3 1 
2 1 0 3 
2 1 3 0 
2 3 0 1 
2 3 1 0 
3 0 1 2 
3 0 2 1 
3 1 0 2 
3 1 2 0 
3 2 0 1 
3 2 1 0