How to improve performance of this code?

前端 未结 7 767
一生所求
一生所求 2020-11-21 06:54

Thanks to some help from people here, I was able to get my code for Tasmanian camels puzzle working. However, it is horribly slow (I think. I\'m not sure because this is my

7条回答
  •  南笙
    南笙 (楼主)
    2020-11-21 07:23

    My other answer is rather long, so I decided to list this as a separate answer. This problem is really better suited to doing a depth-first search. I made a depth-first search solution and it is much, much faster than the optimized A-star method made with the changes outlined in my other answer (which is much, much faster than the OP code). For instance, here are the results for running both my A-star and my depth-first search methods on the 17 camels per side case.

    A-star:  14.76 seconds
    Depth-first search: 1.30 seconds
    

    Here's my depth-first method code if you are interested:

    from sys import argv
    
    fCamel = 'F'
    bCamel = 'B'
    gap = 'G'
    
    def issolution(formlen):
        def solution(formation):
            if formation[formlen2] == gap:
                return formation.index(fCamel) == x
            return 0
        x = formlen/2 + 1
        formlen2 = formlen/2
        return solution
    
    def solve(formation):
        def depthfirst(form, g):
            if checksolution(form):
                return [tuple(form)], g + 1
            else:
                igap = form.index(gap)
                if(igap > 1 and form[igap-2] == fCamel):
                    form[igap-2],form[igap] = form[igap],form[igap-2]
                    res = depthfirst(form,g+1)
                    form[igap-2],form[igap] = form[igap],form[igap-2]
                    if res != 0:
                        return [tuple(form)]+res[0],res[1]
                if (igap < flen - 2) and form[igap + 2] == bCamel:
                    form[igap+2],form[igap] = form[igap],form[igap+2]
                    res = depthfirst(form,g+1)
                    form[igap+2],form[igap] = form[igap],form[igap+2]
                    if res != 0:
                        return [tuple(form)]+res[0],res[1]
                if(igap > 0 and form[igap-1] == fCamel):                
                    form[igap-1],form[igap] = form[igap],form[igap-1]
                    res = depthfirst(form,g+1)
                    form[igap-1],form[igap] = form[igap],form[igap-1]
                    if res != 0:
                        return [tuple(form)]+res[0],res[1]               
                if (igap < flen - 1) and form[igap+1] == bCamel:
                    form[igap+1],form[igap] = form[igap],form[igap+1]
                    res = depthfirst(form,g+1)
                    form[igap+1],form[igap] = form[igap],form[igap+1]
                    if res != 0:
                        return [tuple(form)]+res[0],res[1]                
                return 0
        flen = len(formation)
        checksolution = issolution(flen)
        res = depthfirst(list(formation), 0)
        return res
    
    L = lambda x: tuple([fCamel]*x + [gap] + [bCamel]*x)
    print solve(L(int(argv[1])))
    

提交回复
热议问题