zoukankan      html  css  js  c++  java
  • 庞果网(最小操作数)

    从庞果网上,看到一题,是求最小操作数的,具体题目如下:

    现用python代码实现如下:

      1 #!usr/bin/env python
      2 #coding:utf-8
      3 
      4 def compareWord(A,B):
      5     wordLen = len(A)
      6     diff = 0
      7     for i in range(wordLen):
      8         if A[i] != B[i]:
      9             diff += 1
     10     return diff
     11 
     12 
     13 def findpath(startD, endD, Dict, dictMatrix):
     14     paths = dict()
     15     minDis = min(min([dictMatrix[Dict.index(si)][Dict.index(ei)] for si in startD] for ei in endD))
     16 
     17     if minDis == 0:
     18         for si in startD:
     19             for ei in endD:
     20                 bdInd = Dict.index(si)
     21                 edInd = Dict.index(ei)
     22                 if dictMatrix[bdInd][edInd] == 0:
     23                     paths[si] = ei
     24         return paths
     25     elif minDis == 1:
     26         for si in startD:
     27             for ei in endD:
     28                 bdInd = Dict.index(si)
     29                 edInd = Dict.index(ei)
     30                 if dictMatrix[bdInd][edInd] == 1:
     31                     if si not in paths:
     32                         paths.setdefault(si, {})
     33                     paths[si] = ei
     34         return paths
     35     else:
     36         for si in startD:
     37             bdInd = Dict.index(si)
     38             temp = list()
     39             tempArray = dictMatrix[bdInd]
     40             for t in range(len(tempArray)):
     41                 if tempArray[t] == 1:
     42                     temp.append(Dict[t])
     43             path = findpath(temp, endD, Dict, dictMatrix)
     44             paths[si] = path
     45     return paths
     46 
     47 
     48 def translate(A, B , Dict):
     49     diff = compareWord(A,B)
     50     if diff == 0:
     51         return []
     52     paths = list()
     53     #找到入口
     54     startD = list()
     55     #出口
     56     endD = list()
     57     for di in Dict:
     58         diff = compareWord(A, di)
     59         if diff == 1:
     60             startD.append(di)
     61         elif diff == 0:
     62             startD=[di]
     63             break
     64     for di in Dict:
     65         diff = compareWord(B, di)
     66         if diff == 1:
     67             endD.append(di)
     68         elif diff == 0:
     69             endD=[di]
     70             break
     71     #构建单词集合的矩阵
     72     dictMatrix = list()
     73     for di in Dict:
     74         temp = list()
     75         for dj in Dict:
     76             if di == dj:
     77                 temp.append(0)
     78             else:
     79                 temp.append(compareWord(di,dj))
     80         dictMatrix.append(temp)
     81 
     82     for di in dictMatrix:
     83         print di
     84 
     85     #寻找startD到endD的最短路径
     86     print 'startD:', A, startD
     87     print 'endD:', B, endD
     88     paths = findpath(startD, endD, Dict, dictMatrix)
     89     print paths
     90     pathSt = [A]
     91     pathSt = printDict(paths, pathSt)
     92     pathSt = [pi+'->'+B for pi in pathSt]
     93     return  pathSt
     94 
     95 def printDict(paths, pathSt):
     96     nextpath = paths.keys()
     97     pathSt = [ps+'->' for ps in pathSt]
     98     pathSt = pathSt*len(nextpath)
     99     for ni in range(len(nextpath)):
    100         pathSt[ni] += nextpath[ni]
    101         if type(paths[nextpath[ni]]) is not type(dict()):
    102             if not pathSt[ni].endswith(paths[nextpath[ni]]):
    103                 pathSt[ni] += '->'+paths[nextpath[ni]]
    104             #return pathSt
    105         else:
    106             pathSt = printDict(paths[nextpath[ni]], pathSt)
    107     return pathSt
    108 
    109 if __name__=='__main__':
    110     A = 'hog'
    111     B = 'hog'
    112     Dict = ['hot', 'dot', 'dog', 'lot', 'log','tog','tit']
    113     paths = translate(A,B, Dict)
    114     print paths
  • 相关阅读:
    【已解决】github中git push origin master出错:error: failed to push some refs to
    好记心不如烂笔头,ssh登录 The authenticity of host 192.168.0.xxx can't be established. 的问题
    THINKPHP 5.0目录结构
    thinkphp5.0入口文件
    thinkphp5.0 生命周期
    thinkphp5.0 架构
    Django template
    Django queryset
    Django model
    Python unittest
  • 原文地址:https://www.cnblogs.com/AlgorithmDot/p/3396260.html
Copyright © 2011-2022 走看看