-
Notifications
You must be signed in to change notification settings - Fork 297
/
exhaustionAlbertEinstein.kt
508 lines (441 loc) · 13.5 KB
/
exhaustionAlbertEinstein.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
package com.wangpos.datastructure.algorithm
/**
* (1)英国人住在红色的房子里
(2)瑞典人养狗作为宠物
(3)丹麦人喝茶
(4)绿房子紧挨着白房子,在白房子的左边
(5)绿房子的主人喝咖啡
(6)抽 Pall Mall 牌香烟的人养鸟
(7)黄色房子里的人抽 Dunhill 牌香烟
(8)住在中间那个房子里的人喝牛奶
(9)挪威人住在第一个房子里面
(10)抽 Blends 牌香烟的人和养猫的人相邻
(11)养马的人和抽 Dunhill 牌香烟的人相邻
(12)抽 BlueMaster 牌香烟的人喝啤酒
(13)德国人抽 Prince 牌香烟
(14)挪威人和住在蓝房子的人相邻
(15)抽 Blends 牌香烟的人和喝矿泉水的人相邻
*/
val yellow = 1
val blue = 2
val red = 3
val green = 4
val white = 5
val water = 1
val tea = 2
val milk = 3
val coffee = 4
val beer = 5
val nuowei = 1
val danmai = 2
val yingguo = 3
val deguo = 4
val ruidian = 5
val cat = 1
val horse = 2
val bird = 3
val fish = 4
val dog = 5
val Dunhill = 1
val Blends = 2
val PallMall = 3
val Prince = 4
val BlueMaster = 5
var roomColors = arrayOf(1, 2, 3, 4, 5)
var drinks = arrayOf(1, 2, 3, 4, 5)
var countrys = arrayOf(1, 2, 3, 4, 5)
var pets = arrayOf(1, 2, 3, 4, 5)
var tobaccos = arrayOf(1, 2, 3, 4, 5)
var cnt: Long = 0
/**
* 先填表,最后在赛选,也可以用递归实现
*/
fun main() {
var groups = arrayOf(Person_E(), Person_E(), Person_E(), Person_E(), Person_E())
assignedColor(roomColors, groups)
}
/**
* 分配颜色
*/
private fun assignedColor(
roomColors: Array<Int>,
groups: Array<Person_E>
) {
for (i in roomColors.indices) {
for (j in roomColors.indices) {
if (i == j) continue
for (k in roomColors.indices) {
if (k == i || k == j) continue
for (l in roomColors.indices) {
if (l == i || l == j || l == k) continue
for (m in roomColors.indices) {
if (m == i || m == j || m == k || m == l) continue
groups[0].room_color = roomColors[i]
groups[1].room_color = roomColors[j]
groups[2].room_color = roomColors[k]
groups[3].room_color = roomColors[l]
groups[4].room_color = roomColors[m]
// 剪枝操作
// 再分配国家
for (groupIdx in groups.indices) {
if (groupIdx + 1 < groups.size) {
var ad = "sd"
ad.isEmpty()
if (groups[groupIdx].room_color === green
&& groups[groupIdx + 1].room_color == white
) {
assignedCountry(groups)
}
}
}
}
}
}
}
}
}
/**
* 分配国家
*/
fun assignedCountry(groups: Array<Person_E>) {
/*应用规则(9):挪威人住在第一个房子里面;*/
groups[0].country = nuowei;
for (i in countrys.indices) {
for (j in countrys.indices) {
if (i == j) continue
for (k in countrys.indices) {
if (k == i || k == j) continue
for (l in countrys.indices) {
if (l == k || l == j || l == i) continue
for (m in countrys.indices) {
if (m == i || m == j || m == k || m == l) continue
groups[1].country = countrys[j]
groups[2].country = countrys[k]
groups[3].country = countrys[l]
groups[4].country = countrys[m]
assigndDrink(groups)
}
}
}
}
}
}
/**
* 分配喝的
*/
fun assigndDrink(groups: Array<Person_E>) {
/*应用规则(8):住在中间那个房子里的人喝牛奶;*/
groups[2].drink = milk
for (i in drinks.indices) {
for (j in drinks.indices) {
if (i == j) continue
for (k in drinks.indices) {
if (k == i || k == j) continue
for (l in drinks.indices) {
if (l == k || l == j || l == i) continue
for (m in drinks.indices) {
if (m == i || m == j || m == k || m == l) continue
groups[0].drink = drinks[i]
groups[1].drink = drinks[j]
groups[3].drink = drinks[l]
groups[4].drink = drinks[m]
assigndPet(groups)
}
}
}
}
}
}
/**
* 分配宠物
*/
fun assigndPet(groups: Array<Person_E>) {
for (i in pets.indices) {
for (j in pets.indices) {
if (i == j) continue
for (k in pets.indices) {
if (k == i || k == j) continue
for (l in pets.indices) {
if (l == k || l == j || l == i) continue
for (m in pets.indices) {
if (m == i || m == j || m == k || m == l) continue
groups[0].pet = pets[i]
groups[1].pet = pets[j]
groups[2].pet = pets[k]
groups[3].pet = pets[l]
groups[4].pet = pets[m]
assignedTabacco(groups)
}
}
}
}
}
}
fun assignedTabacco(groups: Array<Person_E>) {
for (i in tobaccos.indices) {
for (j in tobaccos.indices) {
if (i == j) continue
for (k in tobaccos.indices) {
if (k == i || k == j) continue
for (l in tobaccos.indices) {
if (l == k || l == j || l == i) continue
for (m in tobaccos.indices) {
if (m == i || m == j || m == k || m == l) continue
groups[0].tobacco = tobaccos[i]
groups[1].tobacco = tobaccos[j]
groups[2].tobacco = tobaccos[k]
groups[3].tobacco = tobaccos[l]
groups[4].tobacco = tobaccos[m]
DoGroupsfinalCheck(groups)
}
}
}
}
}
}
/**
*
*/
fun DoGroupsfinalCheck(groups: Array<Person_E>) {
// cnt++
// println(cnt)
if (checkResult(groups)) {
PrintAllGroupsResult(groups)
println()
}
}
/**
*
*
* * (1)英国人住在红色的房子里
(2)瑞典人养狗作为宠物
(3)丹麦人喝茶
(4)绿房子紧挨着白房子,在白房子的左边
(5)绿房子的主人喝咖啡
(6)抽 Pall Mall 牌香烟的人养鸟
(7)黄色房子里的人抽 Dunhill 牌香烟
(10)抽 Blends 牌香烟的人和养猫的人相邻
(11)养马的人和抽 Dunhill 牌香烟的人相邻
(12)抽 BlueMaster 牌香烟的人喝啤酒
(13)德国人抽 Prince 牌香烟
(14)挪威人和住在蓝房子的人相邻
(15)抽 Blends 牌香烟的人和喝矿泉水的人相邻
*/
fun checkResult(groups: Array<Person_E>): Boolean {
for (i in groups.indices) {
var it = groups[i]
//(1)英国人住在红色的房子里
if (it.room_color == red) {
if (it.country != yingguo) {
return false
}
}
//(2)瑞典人养狗作为宠物
if (it.country == ruidian) {
if (it.pet != dog) {
return false
}
}
//(3)丹麦人喝茶
if (it.country == danmai) {
if (it.drink != tea) {
return false
}
}
//(5)绿房子的主人喝咖啡鸟
if (it.room_color == green) {
if (it.drink != coffee) {
return false
}
}
//(6)抽 Pall Mall 牌香烟的人养鸟
if (it.pet == bird) {
if (it.tobacco != PallMall) {
return false
}
}
//(7)黄色房子里的人抽 Dunhill 牌香烟
if (it.room_color == yellow) {
if (it.tobacco != Dunhill) {
return false
}
}
//(12)抽 BlueMaster 牌香烟的人喝啤酒
if (it.tobacco == BlueMaster) {
if (it.drink != beer) {
return false
}
}
//13 德国人抽 Prince 牌香烟
if (it.country == deguo) {
if (it.tobacco != Prince) {
return false
}
}
//(14)挪威人和住在蓝房子的人相邻
if (it.country == nuowei) {
var first = if ((i - 1) < 0) {
false
} else {
groups[i - 1].room_color == blue
}
var second = if ((i + 1) >= groups.size) {
false
} else {
groups[i + 1].room_color == blue
}
if (!first && !second) {
return false
}
}
// 15)抽 Blends 牌香烟的人和喝矿泉水的人相邻
if (it.tobacco == Blends) {
var first = if ((i - 1) < 0) {
false
} else {
groups[i - 1].drink == water
}
var second = if ((i + 1) >= groups.size) {
false
} else {
groups[i + 1].drink == water
}
if (!first && !second) {
return false
}
}
// (10)抽 Blends 牌香烟的人和养猫的人相邻
if (it.tobacco == Blends) {
var first = if ((i - 1) < 0) {
false
} else {
groups[i - 1].pet == cat
}
var second = if ((i + 1) >= groups.size) {
false
} else {
groups[i + 1].pet == cat
}
if (!first && !second) {
return false
}
}
//(11)养马的人和抽 Dunhill 牌香烟的人相邻
if (it.tobacco == Dunhill) {
var first = if ((i - 1) < 0) {
false
} else {
groups[i - 1].pet == horse
}
var second = if ((i + 1) >= groups.size) {
false
} else {
groups[i + 1].pet == horse
}
if (!first && !second) {
return false
}
}
//(4)绿房子紧挨着白房子,在白房子的左边
if ((i + 1) < groups.size) {
if (groups[i].room_color == green && groups[i + 1].room_color != white) {
return false
}
}
}
return true
}
/**
*
*
国家 房子 宠物 饮料 香烟
挪威 黄色 猫 矿泉水 Dunhill
丹麦 蓝色 马 茶 Blends
英国 红色 鸟 牛奶 PallMall
德国 绿色 鱼 咖啡 Prince
瑞典 白色 狗 啤酒 BlueMaster
房间颜色:黄 国家:挪威 宠物:猫 饮料:水 抽烟:Dunhill
房间颜色:蓝 国家:丹麦 宠物:马 饮料:茶 抽烟:Blends
房间颜色:红 国家:英国 宠物:鸟 饮料:牛奶 抽烟:PallMall
房间颜色:绿 国家:德国 宠物:鱼 饮料:咖啡 抽烟:Prince
房间颜色:白 国家:瑞典 宠物:狗 饮料:啤酒 抽烟:BlueMaster
*/
fun PrintAllGroupsResult(groups: Array<Person_E>) {
groups.forEach {
print("房间颜色:${getColorName(it.room_color)} ")
print("国家:${getCountryName(it.country)} ")
print("宠物:${getPetName(it.pet)} ")
print("饮料:${getDrinkName(it.drink)} ")
println("抽烟:${getTabaccoName(it.tobacco)} ")
}
}
fun getColorName(room_color: Int): String {
return when (room_color) {
1 -> "黄"
2 -> "蓝"
3 -> "红"
4 -> "绿"
5 -> "白"
else -> {
" "
}
}
}
fun getCountryName(room_color: Int): String {
return when (room_color) {
1 -> "挪威"
2 -> "丹麦"
3 -> "英国"
4 -> "德国"
5 -> "瑞典"
else -> {
" "
}
}
}
fun getPetName(room_color: Int): String {
return when (room_color) {
1 -> "猫"
2 -> "马"
3 -> "鸟"
4 -> "鱼"
5 -> "狗"
else -> {
" "
}
}
}
fun getDrinkName(room_color: Int): String {
return when (room_color) {
1 -> "水"
2 -> "茶"
3 -> "牛奶"
4 -> "咖啡"
5 -> "啤酒"
else -> {
" "
}
}
}
fun getTabaccoName(room_color: Int): String {
return when (room_color) {
1 -> "Dunhill"
2 -> "Blends"
3 -> "PallMall"
4 -> "Prince"
5 -> "BlueMaster"
else -> {
" "
}
}
}
/**
* 房子颜色 国籍 饮料 宠物 烟
*/
class Person_E(
var room_color: Int = -1,
var country: Int = -1,
var drink: Int = -1,
var pet: Int = -1,
var tobacco: Int = -1
)