|
发表于 2018-3-9 13:43:30
|
显示全部楼层
洗了个消消乐的最优决策,太乱了,还是不写注释了。
- class joy():
- def __init__(self,lst):
- self.fac = lst
- self.itm = []
- self.sav = {3:[]}
- self.step = [(0,1),(1,0),(-1,0),(0,-1)]
- self.com = []
- self.right = {0:[]}
-
- def check_1(self):
- for line in range(5):
- sam = self.fac[line][0]
- cot = 1
- self.sav[3] = [(line,0)]
- for each in range(1,6):
- sam,cot = self.condition(sam,cot,line,each,sign=False)
- def check_2(self):
- for ver in range(6):
- sam = self.fac[0][ver]
- cot = 1
- self.sav[3] = [(0,ver)]
- for each in range(1,5):
- sam,cot = self.condition(sam,cot,each,ver,sign=True)
-
- def condition(self,sam,cot,line,each,sign= False):
- if self.fac[line][each] == sam and sam != 0:
- cot += 1
- self.sav[3].append((line,each))
- if not sign:
- if each == 5 and cot >= 3:
- temp = self.sav[3][:]
- self.itm.append(temp)
- else:
- if line == 4 and cot >=3:
- temp = self.sav[3][:]
- self.itm.append(temp)
- else:
- if cot >= 3:
- temp = self.sav[3][:]
- self.itm.append(temp)
- sam = self.fac[line][each]
- cot = 1
- self.sav[3] = [(line,each)]
- return sam,cot
- def sove(self):
- for line in self.itm:
- for L in line:
- self.fac[L[0]][L[1]] = 0
- for i in range(5):
- for j in range(6):
- if not self.fac[i][j] and i > 0:
- t = i
- while t:
- self.fac[t][j] = self.fac[t-1][j]
- self.fac[t-1][j] = 0
- t -= 1
-
- def show(self):
- for each in self.fac:
- print(' ',end='')
- for i in each:
- print(i,end=' ')
- print('')
- def move(self):
- self.right = {}
- for i in range(5):
- for j in range(6):
- self.comb(i,j)
- def comb(self,a,b):
- for i in self.step:
- j = a + i[0]
- k = b + i[1]
- if 0 <= j < 5 and 0 <= k < 6:
- if self.fac[j][k] != self.fac[a][b]and [(j,k),(a,b)] not in self.com:
- self.fac[a][b],self.fac[j][k] = self.fac[j][k],self.fac[a][b]
- self.check_1()
- self.check_2()
- if len(self.itm) > 0:
- lst = [tuple(m) for m in self.fac]
- self.combmax(a,b,j,k)
- self.fac = [list(n) for n in lst]
- self.com.append([(a,b),(j,k)])
- self.fac[j][k],self.fac[a][b] = self.fac[a][b],self.fac[j][k]
- def combmax(self,a,b,j,k):
- count = 0
- item = []
- while True:
- self.sove()
- item += self.itm[:]
- self.itm = []
- self.check_1()
- self.check_2()
- if len(self.itm) == 0:
- break
- for i in item:
- if len(i) == 3: count += 1
- elif len(i) == 4: count += 2
- elif len(i) == 5: count += 3
- else: count += 4
- self.right[count] = [(a,b),(j,k)]
- def zuizhong(self,a,b,j,k):
- item = []
- while True:
- self.sove()
- self.show()
- print()
- item += self.itm[:]
- self.itm = []
- self.check_1()
- self.check_2()
- if len(self.itm) == 0:
- break
-
- st = '''3 5 2 1 1 2
- 3 2 6 5 1 5
- 2 6 5 6 5 1
- 4 2 1 5 6 5
- 6 1 4 1 1 2'''
- lst = [each.split(' ') for each in st.split('\n')]
- lst = [[int(j) for j in i] for i in lst]
- J = joy(lst)
- J.show()
- J.move()
- print('产生最高comb数的交换路径是')
- print(J.right[max(J.right)],'产生的comb数是%s'%max(J.right))
- print('移动后的界面:')
- [(a,b),(c,d)] = J.right[max(J.right)]
- J.fac[a][b],J.fac[c][d] =J.fac[c][d],J.fac[a][b]
- J.zuizhong(a,b,c,d)
复制代码 |
|