fork download
  1. #include <stdio.h>
  2. #define SIZE 100
  3.  
  4. int stack[SIZE],
  5. level,
  6. n,
  7. k;
  8.  
  9. void init(){
  10.  
  11. stack[level] = 0;
  12. };
  13.  
  14. int valid() {
  15.  
  16. int i;
  17.  
  18. for(i = 1; i < level; i++)
  19.  
  20. if(stack[i] == stack[level]) return 0;
  21.  
  22. return 1;
  23. };
  24.  
  25. int succ() {
  26.  
  27. if(stack[level]<n){
  28.  
  29. stack[level]++;
  30.  
  31. return 1;
  32. }
  33. return 0;
  34. };
  35.  
  36. int sol() {
  37.  
  38. return level == k;
  39. };
  40.  
  41. void print() {
  42.  
  43. int i;
  44.  
  45. printf("x | ");
  46. for(i = 1; i <= k; i++) printf("%d ", i);
  47. printf("\n----------------\n");
  48. printf("f(x) | ");
  49. for(i = 1; i <= k; i++) printf("%d ", stack[i]);
  50. printf("\n\n");
  51. };
  52.  
  53. void bt() {
  54.  
  55. int hs,is;
  56.  
  57. level = 1;
  58.  
  59. while(level > 0) {
  60.  
  61. hs = 1; is = 0;
  62.  
  63. while(hs && !is) {
  64.  
  65. hs = succ();
  66.  
  67. if(hs) is = valid();
  68. }
  69.  
  70. if(hs) {
  71.  
  72. if(sol()) print();
  73.  
  74. else {level++;init();}
  75.  
  76. } else level--;
  77. }
  78.  
  79. };
  80.  
  81. void printSet(char set, int n, int flag) {
  82.  
  83. int i;
  84.  
  85. if( flag ) printf("f : A -> B\n\n");
  86.  
  87. printf("%c = {",set);
  88.  
  89. for(i = 1; i <= n; i++) {
  90.  
  91. printf("%d ",i);
  92. }
  93.  
  94. printf("}\n\n");
  95. };
  96.  
  97. int main() {
  98.  
  99. //freopen("genAllInjective.out","w",stdout);
  100.  
  101. printf("%s\n\n","Generating All The Injective Functions");
  102. //f : A -> B
  103. //A = {1,2}, B = {1,2,3}
  104. scanf("%d",&n);
  105. scanf("%d",&k);
  106.  
  107. printSet('A',k, 1);
  108. printSet('B',n, 0);
  109.  
  110. bt();
  111.  
  112. return(0);
  113. };
Success #stdin #stdout 0s 5304KB
stdin
4
3
stdout
Generating All The Injective Functions

f : A -> B

A = {1 2 3 }

B = {1 2 3 4 }

x    | 1 2 3 
----------------
f(x) | 1 2 3 

x    | 1 2 3 
----------------
f(x) | 1 2 4 

x    | 1 2 3 
----------------
f(x) | 1 3 2 

x    | 1 2 3 
----------------
f(x) | 1 3 4 

x    | 1 2 3 
----------------
f(x) | 1 4 2 

x    | 1 2 3 
----------------
f(x) | 1 4 3 

x    | 1 2 3 
----------------
f(x) | 2 1 3 

x    | 1 2 3 
----------------
f(x) | 2 1 4 

x    | 1 2 3 
----------------
f(x) | 2 3 1 

x    | 1 2 3 
----------------
f(x) | 2 3 4 

x    | 1 2 3 
----------------
f(x) | 2 4 1 

x    | 1 2 3 
----------------
f(x) | 2 4 3 

x    | 1 2 3 
----------------
f(x) | 3 1 2 

x    | 1 2 3 
----------------
f(x) | 3 1 4 

x    | 1 2 3 
----------------
f(x) | 3 2 1 

x    | 1 2 3 
----------------
f(x) | 3 2 4 

x    | 1 2 3 
----------------
f(x) | 3 4 1 

x    | 1 2 3 
----------------
f(x) | 3 4 2 

x    | 1 2 3 
----------------
f(x) | 4 1 2 

x    | 1 2 3 
----------------
f(x) | 4 1 3 

x    | 1 2 3 
----------------
f(x) | 4 2 1 

x    | 1 2 3 
----------------
f(x) | 4 2 3 

x    | 1 2 3 
----------------
f(x) | 4 3 1 

x    | 1 2 3 
----------------
f(x) | 4 3 2