鱼C论坛

 找回密码
 立即注册
查看: 3051|回复: 1

题目185:猜数字游戏

[复制链接]
发表于 2016-10-4 00:56:31 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
Number Mind

The game Number Mind is a variant of the well known game Master Mind.

Instead of coloured pegs, you have to guess a secret sequence of digits. After each guess you're only told in how many places you've guessed the correct digit. So, if the sequence was 1234 and you guessed 2036, you'd be told that you have one correct digit; however, you would NOT be told that you also have another digit in the wrong place.

For instance, given the following guesses for a 5-digit secret sequence,

90342 ;2 correct
70794 ;0 correct
39458 ;2 correct
34109 ;1 correct
51545 ;2 correct
12531 ;1 correct


The correct sequence 39542 is unique.

Based on the following guesses,

5616185650518293 ;2 correct
3847439647293047 ;1 correct
5855462940810587 ;3 correct
9742855507068353 ;3 correct
4296849643607543 ;3 correct
3174248439465858 ;1 correct
4513559094146117 ;2 correct
7890971548908067 ;3 correct
8157356344118483 ;1 correct
2615250744386899 ;2 correct
8690095851526254 ;3 correct
6375711915077050 ;1 correct
6913859173121360 ;1 correct
6442889055042768 ;2 correct
2321386104303845 ;0 correct
2326509471271448 ;2 correct
5251583379644322 ;2 correct
1748270476758276 ;3 correct
4895722652190306 ;1 correct
3041631117224635 ;3 correct
1841236454324589 ;3 correct
2659862637316867 ;2 correct


Find the unique 16-digit secret sequence.


题目:

猜数字游戏 Number Mind 是著名游戏 Master Mind 的一个变种。

与着色的柱子不同,你现在必须猜测一个秘密的数的序列。每次猜测之后,你只能得知你猜中多少个正确位置。因此,如果序列是 1234,你猜是 2036,则它会告诉你,你猜对了一个数字;但是,你在错误位置上放了另一个数字时,它是不会告诉你的。

比如,给出一个 5 位的数字序列的猜测结果,

90342 ;2 位正确
70794 ;0 位正确
39458 ;2 位正确
34109 ;1 位正确
51545 ;2 位正确
12531 ;1 位正确


可以得到唯一正确的序列 39542。

基于下面的猜测结果,

5616185650518293 ;2 位正确
3847439647293047 ;1 位正确
5855462940810587 ;3 位正确
9742855507068353 ;3 位正确
4296849643607543 ;3 位正确
3174248439465858 ;1 位正确
4513559094146117 ;2 位正确
7890971548908067 ;3 位正确
8157356344118483 ;1 位正确
2615250744386899 ;2 位正确
8690095851526254 ;3 位正确
6375711915077050 ;1 位正确
6913859173121360 ;1 位正确
6442889055042768 ;2 位正确
2321386104303845 ;0 位正确
2326509471271448 ;2 位正确
5251583379644322 ;2 位正确
1748270476758276 ;3 位正确
4895722652190306 ;1 位正确
3041631117224635 ;3 位正确
1841236454324589 ;3 位正确
2659862637316867 ;2 位正确

请给出唯一正确的 16 位秘密序列。

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-2-10 13:35:44 | 显示全部楼层
  1. import itertools as it
  2. t = '''2659862637316867 ;2
  3. 1841236454324589 ;3
  4. 3041631117224635 ;3
  5. 4895722652190306 ;1
  6. 1748270476758276 ;3
  7. 5251583379644322 ;2
  8. 2326509471271448 ;2
  9. 2321386104303845 ;0
  10. 6442889055042768 ;2
  11. 6913859173121360 ;1
  12. 6375711915077050 ;1
  13. 8690095851526254 ;3
  14. 2615250744386899 ;2
  15. 8157356344118483 ;1
  16. 7890971548908067 ;3
  17. 4513559094146117 ;2
  18. 3174248439465858 ;1
  19. 4296849643607543 ;3
  20. 9742855507068353 ;3
  21. 5855462940810587 ;3
  22. 3847439647293047 ;1
  23. 5616185650518293 ;2'''

  24. s = []
  25. for i in t.split('\n'):
  26.     f = i.strip()
  27.     m = []
  28.     for j in f.split(';'):
  29.         j = j.strip()
  30.         m.append(j)
  31.     s.append(m)

  32. s1 =[]
  33. for each in s:
  34.     m = []
  35.     for i in each[0]:
  36.         m.append(int(i))
  37.     s1.append((m,int(each[1])))

  38. t = s1[7]
  39. s1.remove(t)

  40. def shai_1():
  41.     weizhi1 = []
  42.     for i in s1:
  43.         m = []
  44.         for j in range(16):
  45.             if i[0][j] != t[0][j]:
  46.                 m.append(j)
  47.         weizhi1.append(m)
  48.    
  49.     return weizhi1

  50. weizhi = shai_1()


  51. def sheng():
  52.     S = []
  53.     for t in it.combinations(weizhi[0],s1[0][1]):
  54.         s_t = ['x']*16
  55.         for x1 in range(s1[0][1]):
  56.             s_t[t[x1]] = s1[0][0][t[x1]]
  57.         s_x = s_t[:]
  58.         S.append((s_x,t))
  59.     return S
  60.         

  61. def shai_2(S, t, c, weizhi):

  62.     if len(t)==16:
  63.         V = 1
  64.         for a in range(21):
  65.             count = 0
  66.             for each in range(16):
  67.                 if S[each] == s1[a][0][each]:
  68.                     count +=1
  69.             if count != s1[a][1]:
  70.                 V =0
  71.                 break
  72.         if V == 1:
  73.             print(S)
  74.                     
  75.         return 1
  76.     for i in range(c,21):
  77.         s_t = S[:]
  78.         flag = 1
  79.         for j in weizhi[i]:
  80.             if s_t[j] == s1[i][0][j]:
  81.                 flag = 0
  82.                 break
  83.         if flag == 1:
  84.             t1 = [k for k in weizhi[i] if k not in t]
  85.             for x in it.combinations(t1,s1[i][1]):
  86.                 t2 = t
  87.                 s_y = s_t[:]
  88.                 for y in range(s1[i][1]):
  89.                     s_y[x[y]] = s1[i][0][x[y]]
  90.                 s_z = s_y[:]
  91.                 t2 += x
  92.                 c = i
  93.                 shai_2(s_z, t2, c, weizhi)


  94. def main():
  95.     M = sheng()
  96.     for i in M:
  97.         S,t = i[0],i[1]
  98.         shai_2(S,t,1,weizhi)

  99. main()
  100.    
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 0 反对 1

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-4 16:33

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表