fork(18) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define N 5
  5.  
  6. int i;
  7. vector<string> comb;
  8. int ans[300];
  9. bool flag[300];
  10. bool found;
  11.  
  12. bool diff_one(string a, string b) {
  13. int i;
  14. int cnt = 0;
  15. for (i = 0; i < N; i++) {
  16. if (a[i] != b[i]) cnt++;
  17. }
  18. if (cnt <= 1) return true;
  19. return false;
  20. }
  21.  
  22. void generate(string s, int x) {
  23. if (x > 9) {
  24. if (s.length() == N) {
  25. comb.push_back(s);
  26. }
  27. } else {
  28. generate(s, x+1);
  29. generate(s + (char)(x + '0'), x+1);
  30. }
  31. }
  32.  
  33. void brute(int taken) {
  34. int i;
  35. if (found) return;
  36. if (taken == comb.size()) {
  37. for (i = 0; i < comb.size(); i++) cout << i+1 << ": " << comb[ans[i]] << endl;
  38. found = true;
  39. return;
  40. }
  41. for (i = 0; i < comb.size(); i++) if (!flag[i] && diff_one(comb[ans[taken-1]], comb[i])) {
  42. flag[i] = true;
  43. ans[taken] = i;
  44. brute(taken + 1);
  45. flag[i] = false;
  46. }
  47. }
  48.  
  49. int main() {
  50. generate("", 0);
  51.  
  52. ans[0] = 0;
  53. flag[0] = true;
  54. brute(1);
  55. }
  56.  
Success #stdin #stdout 0s 4380KB
stdin
Standard input is empty
stdout
1: 56789
2: 46789
3: 45789
4: 45689
5: 45679
6: 45678
7: 35678
8: 35679
9: 35689
10: 35789
11: 36789
12: 34789
13: 34689
14: 34679
15: 34678
16: 34578
17: 34579
18: 34589
19: 34569
20: 34568
21: 34567
22: 24567
23: 24569
24: 24589
25: 24789
26: 26789
27: 25789
28: 25689
29: 25679
30: 25678
31: 24678
32: 24679
33: 24689
34: 23689
35: 23789
36: 23589
37: 23579
38: 24579
39: 24578
40: 24568
41: 23568
42: 23578
43: 23678
44: 23679
45: 23479
46: 23489
47: 23469
48: 23569
49: 23567
50: 23467
51: 23468
52: 23478
53: 23458
54: 23459
55: 23457
56: 23456
57: 13456
58: 13459
59: 13489
60: 13789
61: 16789
62: 15789
63: 15689
64: 15679
65: 15678
66: 14678
67: 14679
68: 14689
69: 14789
70: 14589
71: 14579
72: 14578
73: 14568
74: 14569
75: 14567
76: 13567
77: 13569
78: 13589
79: 13689
80: 13679
81: 13678
82: 13578
83: 13579
84: 13479
85: 13478
86: 13468
87: 13568
88: 12568
89: 12578
90: 12678
91: 12679
92: 12689
93: 12789
94: 12589
95: 12579
96: 12569
97: 12567
98: 12467
99: 13467
100: 13469
101: 12469
102: 12489
103: 12479
104: 12478
105: 12468
106: 12458
107: 13458
108: 13457
109: 12457
110: 12459
111: 12456
112: 12356
113: 12359
114: 12389
115: 12379
116: 12378
117: 12368
118: 12369
119: 12367
120: 12357
121: 12358
122: 12348
123: 12349
124: 12347
125: 12346
126: 12345
127: 02345
128: 02349
129: 02389
130: 02789
131: 06789
132: 05789
133: 05689
134: 05679
135: 05678
136: 04678
137: 04679
138: 04689
139: 04789
140: 04589
141: 04579
142: 04578
143: 04568
144: 04569
145: 04567
146: 03567
147: 03569
148: 03589
149: 03789
150: 03689
151: 03679
152: 03678
153: 03578
154: 03579
155: 03479
156: 03489
157: 03469
158: 03468
159: 03568
160: 02568
161: 02578
162: 02678
163: 02679
164: 02689
165: 02589
166: 02579
167: 02569
168: 02567
169: 02467
170: 03467
171: 03457
172: 03459
173: 03458
174: 03478
175: 02478
176: 02479
177: 02489
178: 02469
179: 02468
180: 02458
181: 02459
182: 02457
183: 02456
184: 03456
185: 01456
186: 01459
187: 01489
188: 01789
189: 01689
190: 01679
191: 01678
192: 01578
193: 01579
194: 01589
195: 01569
196: 01568
197: 01567
198: 01467
199: 01469
200: 01479
201: 01478
202: 01468
203: 01458
204: 01457
205: 01357
206: 02357
207: 02367
208: 02369
209: 02379
210: 02378
211: 02368
212: 02358
213: 02359
214: 02356
215: 02346
216: 02348
217: 02347
218: 01347
219: 01367
220: 01369
221: 01389
222: 01379
223: 01378
224: 01368
225: 01358
226: 01359
227: 01356
228: 01346
229: 01349
230: 01348
231: 01345
232: 01245
233: 01249
234: 01289
235: 01279
236: 01278
237: 01268
238: 01269
239: 01267
240: 01257
241: 01259
242: 01258
243: 01256
244: 01246
245: 01248
246: 01247
247: 01237
248: 01239
249: 01238
250: 01236
251: 01235
252: 01234