fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define PR (void)printf(
  5.  
  6. main(int argc, char *argv[])
  7. {
  8. int n, x, fr, to;
  9. unsigned int i, j, p[3];
  10.  
  11. n = atoi(argv[1]);
  12.  
  13. p[0]=1; p[0]<<=n; p[0]--;
  14. p[1]=p[2]=0;
  15.  
  16. /* Print out the initial configuration: */
  17. PR"\n|");
  18. for(i=n;i>0;i--) PR" %d",i);
  19. PR"\n|\n|\n");
  20.  
  21. for(x=1; x < (1 << n); x++) {
  22.  
  23. i=x&x-1; fr=(i+i/3)&3;
  24. i=(x|x-1)+1; to=(i+i/3)&3;
  25.  
  26. for(i=x, j=1; ; i>>=1, j<<=1) { if(i&1) break; }
  27. /* Move the bit: */
  28. p[fr] &= ~j;
  29. p[to] |= j;
  30.  
  31. /* Print out the configuration after move x: */
  32. PR"\n|"); j=1; j<<=n;
  33. for(i=n;i>0;i--) { j>>=1; if(j&p[0]) PR" %d",i); }
  34. PR"\n|"); j=1; j<<=n;
  35. for(i=n;i>0;i--) { j>>=1; if(j&p[1]) PR" %d",i); }
  36. PR"\n|"); j=1; j<<=n;
  37. for(i=n;i>0;i--) { j>>=1; if(j&p[2]) PR" %d",i); }
  38. PR"\n");
  39.  
  40. }
  41. }
  42.  
Runtime error #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
Standard output is empty