深度优先搜索和广度优先搜索

#!/usr/bin/python

 

ary = [0 for x in range(9)]
box = [0 for x in range(9)]
def dfs(stp):
    if (stp +1 == 11):
        print(box);    
        return
    for i in range(1, 10):

        if(ary[i - 1] == 0):
            
            box[stp -1] = i
            ary[i - 1] = 1
            dfs(stp + 1)
            ary[i - 1] = 0
def equalData(step):
    if (step  == 10):
        if(box[0] * 100 + box[1] * 10 + box[2]  + box[3] * 100 +box[4] * 10 + box[5] == box[6] * 100 + box[7] * 10 + box[8]):
            print(box)
        return
    
    for i in range(1, 10):
        if (ary[i - 1] == 0):
            box[step - 1] = i
            ary[i - 1] = 1
            equalData(step + 1)
            ary[i - 1] = 0


ary = [[0, 0, 1, 0], [0, 0, 0, 0], [0, 0, 1, 0], [0, 1, 0, 0], [0, 0 ,0,1]]
m = 5
n = 4
dx = 4
dy = 2
mark = [[0, 0, 0, 0] for x in range(5)]
minStep  = 100

def  findRoad(x, y, step):
    global minStep
    
    stpNext = [[0, 1], [1, 0], [0, -1], [-1, 0]]

    if (x == dx) and (y == dy):
        
        if step < minStep:
            minStep = step
            print(minStep)
        return
    for i in range(4):
        
        nX = x + stpNext[i][0]
        nY = y + stpNext[i][1]
        
        if (nX < 1 ) or (nY < 1) or (nX >= m) or (nY >= n):
            continue
        if (mark[nX][nY] == 0) and (ary[nX][nY] == 0):
            mark[nX][nY] = 1
            findRoad(nX, nY, step + 1)
            mark[nX][nY] = 0
        
    print(x, y)

class note:
    def __init__(self, px, py, pStp):
        self.x = px
        self.y = py
        self.stp = pStp

    def set(self, px, py, pStp):
        self.x = px
        self.y = py
        self.stp = pStp
    def show(self):
        print(self.x, self.y, self.stp)
def main() :
    data = [2, 1, 3, 4, 5, 6, 7]
    node = ListNode(data[0])
    head = node
#    equalData(1)
    
    qeue = [note(0, 0, 0) for i in range(100)]
    head = 1
    tail = 1
    qeue[tail].set(1, 1, 0)
    tail += 1
    print("xxxxxxxxxxxx")

    
    flag = 0
    stpDir = [[1, 0], [0, 1], [-1 , 0], [0, -1]]
    while  head != tail:
        for drn in stpDir:
            x = qeue[head].x +drn[0]
            y = qeue[head].y + drn[1]
            if (x < 1) or (y < 1) or (x >= m) or (y >= n):
                continue
            print(x, y)
            if (mark[x][y] == 0) and (ary[x][y] == 0) :
                qeue[tail].set(x, y, qeue[head].stp + 1)
                mark[x][y] = 1
                tail += 1
            if (x == dx) and (y == dy):
                flag = 1
                break
            
        if (flag == 1):
            break
        head += 1
    
#    findRoad(1, 1, 0)
    print("step is %d" %(qeue[tail - 1].stp))
    

if __name__ == "__main__":
    main()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值