有谁可以帮我代做这个的流程图和Python程序吗

img


真的非常感谢,我被这个折磨死了,很不理解,如果有人能帮我,我会非常谢谢他,太难过了

代码写出来了,折磨了我2个多小时。流程图你自己看懂了代码自己画


#所有的相邻组合
def zh(list_list):
    zh_list = []
    for i in range(len(list_list)):
        #外层list中相邻的内层list组合
        list1 = []
        if 0 < i:
            list1.append(list_list[i-1])
        list1.append(list_list[i])
        if (i+1) < len(list_list):
            list1.append(list_list[i+1])

        for j in range(len(list1)):
            #内层list
            for k in range(len(list1[j])):
                zh = ''
                #内层list相邻元素组合
                if 0 < k:
                    zh += str(list1[j][k-1])
                zh += str(list1[j][k])
                if (k+1) < len(list1[j]):
                    zh += str(list1[j][k+1])
                #内层list元素与相邻list元素组合
                if 0 < j:
                    zh += str(list1[j-1][k])
                if (j+1) < len(list1):
                    zh += str(list1[j+1][k])
                zh_list.append(zh)
    ys_list = [list(i) for i in zh_list]
    return ys_list

#相邻组合的字符是否能组成_s的单词
def Judgment(ys_list,_s):
    for i in ys_list:
        s = list(_s)
        while True:
            try:
                j = s[0]
                x = i.index(j)
                del i[x]
                del s[0]
            except:
                break
        if len(s) == 0:
            return True
    else:
        return False

_s = '2B2CD2'
list_list = [
    ["A1","A2","A3"],
    ["B1","B2","B3"],
    ["C1","C2","C3"],
    ["D1","D2","D3"],
]
ys_list = zh(list_list)
Judgment(ys_list,_s)

你题目的解答代码如下:

hs = set()
dt = ( (-1,0),(0,-1),(1,0),(0,1) )

def fp(li,word):
    for i in range(len(li)):
        for j in range(len(li[i])):
            hs.clear()
            if dnp(li,i,j,word):
                return True
    return False

def dnp(li,x,y,word):
    if len(word)==0:
        return True
    if (x,y) in hs or x<0 or x>=len(li) or y<0 or y>=len(li[x]) or li[x][y]!=word[0]:
        return False
    hs.add((x,y))
    for xt,yt in dt:
        if dnp(li,x+xt,y+yt,word[1:]):
            return True
    hs.remove((x,y))
    return False

print(fp(["ABCE","SFCS","ADEE"],"ABCCED"))

如有帮助,请点击我的回答下方的【采纳该答案】按钮帮忙采纳下,谢谢!

img

这个是很经典的回溯算法的题目,网上样例超级多,甚至一模一样
大体的分析过程就是先判断单词首字母在不在board里面,如果不在直接false 如果在就再看是否能在board里面进行连线。这里首先要看该字母在board里面有几个位置,因为board里面是有重复字母的。再就是给他们找合适的连线方向。每找到一个字母,在单词字符串中删除一个字母,这样当len()为0的时候,就终止循环
主要还是分步的问题,一个字母一个字母的找,如果该方向找不到下一个字母,就回溯到上一个位置,改变寻找方向。回溯和递归动态规划很像,可以参考一下

def find_path(matrix, tar, path):
    x, y = path[-1]
    if matrix[x][y]==tar[0]:
        if len(tar)==1: return True
        direction = [(0,-1),(1,0),(0,1),(-1,0)]
        for d in direction:
            m, n = x+d[0], y+d[1]
            if 0<=m<len(matrix) and 0<=n<len(matrix[0]) and (m,n) not in path:
                if find_path(matrix, tar[1:], path+[(m,n)]): return True
    else:
        return False

matrix = eval(input())
tar = input()
x = len(matrix)
y = len(matrix[0])
for i in range(x*y):
    if find_path(matrix, tar, [(i%x,i//y)]):
        print("true")
        break
else:
    print("false")