fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. void create(int** x, int k)
  5. {
  6. if (k == 1)
  7. {
  8. x[0][0] = 1;
  9. x[0][1] = 2;
  10. x[1][0] = 3;
  11. x[1][1] = 1;
  12. }
  13. else
  14. {
  15. create(x, k - 1);
  16. int from = 1 << (k - 1), n = 1 << k;
  17. for (int i = from; i < n; i++)
  18. for (int j = from; j < n; j++)
  19. x[i][j] = x[i - from][j - from];
  20. for (int i = 0; i < from; i++)
  21. for (int j = 0; j < from; j++)
  22. {
  23. x[i + from][j] = x[i][j] + n;
  24. x[i][j + from] = x[i][j] + n;
  25. }
  26. for (int i = 0; i < from; i++)
  27. x[i + from][i]--;
  28. }
  29. }
  30.  
  31. int main()
  32. {
  33. int k, n = 1;
  34. scanf("%d", &k);
  35. n <<= k;
  36. int** x = new int*[n];
  37. for (int i = 0; i < n; i++)
  38. x[i] = new int[n];
  39. create(x, k);
  40. for (int i = 0; i < n; i++)
  41. {
  42. printf("%d", x[i][0]);
  43. for (int j = 1; j < n; j++)
  44. printf(" %d", x[i][j]);
  45. printf("\n");
  46. }
  47. return 0;
  48. }
Success #stdin #stdout 0s 4460KB
stdin
3
stdout
1 2 5 6 9 10 13 14
3 1 7 5 11 9 15 13
4 6 1 2 12 14 9 10
7 4 3 1 15 12 11 9
8 10 13 14 1 2 5 6
11 8 15 13 3 1 7 5
12 14 8 10 4 6 1 2
15 12 11 8 7 4 3 1