使用DFS和BFS解決修道士和野人過河問題

描述過幾天再寫,先放代碼

DFS

#!/usr/bin/python
# -*- coding: utf-8 -*-

'''
狀態表示方式(M,C,B)
M:修道士(Missionaries)在左岸的數量
C:野人(Cannibals)在左岸的數量
B:船的狀態,左岸還是右岸
初始狀態(3,3,1)
終結狀態(0,0,0)
'''


open_list = []
close_list = []
str_end = "0-0-0"
global time
time = 0



def init_list(list_path):
    for i in range(0, 1000):
        list_path.append(0)


def hund(M, C, boat):
    return (M+1) * 100 + (C+1) * 10 + (boat+1)

def re_back(n):
    a = n // 100
    b = n // 10 % 10
    c = n % 10
    return[a - 1, b - 1, c - 1]



def print_list(list_path, n):
    if list_path[n] == 442:
        print(re_back(442))
        print(re_back(n))
        return
    else:
        print_list(list_path, list_path[n])
        print(re_back(n))


def DFS():
    global time
    time += 1
    if len(open_list) == 0:
        return
    list_temp = open_list.pop()
    M = list_temp[0]
    C = list_temp[1]
    boat = list_temp[2]
    str_temp = str(M) + '-' + str(C) + '-' + str(boat)
    close_list.append(list_temp)
    if str_temp == str_end:
        print("OK")
        print_list(list_path, 111)
        return

    if (M < 0) | (C < 0) | (M > 3) | (C > 3):
        return
    if (M & (M < C)) | ((3 - M) & ((3 - M) < (3 - C))):
        return

    #print(time)
    #print(list_temp)
    fa_node = hund(M, C, boat)
    if boat == 1:  # 左岸,人應該減少
        boat = 0
        if ([M - 2, C, boat] not in open_list) & ([M - 2, C, boat] not in close_list):
            open_list.append([M - 2, C, boat])
            list_path[hund(M - 2, C, boat)] = fa_node
            DFS()
        if ([M, C - 2, boat] not in open_list) & ([M, C - 2, boat] not in close_list):
            open_list.append([M, C - 2, boat])
            list_path[hund(M, C - 2, boat)] = fa_node
            DFS()

        if ([M - 1, C, boat] not in open_list) & ([M - 1, C, boat] not in close_list):
            open_list.append([M - 1, C, boat])
            list_path[hund(M - 1, C, boat)] = fa_node
            DFS()

        if ([M - 1, C - 1, boat] not in open_list) & ([M - 1, C - 1, boat] not in close_list):
            open_list.append([M - 1, C - 1, boat])
            list_path[hund(M - 1, C - 1, boat)] = fa_node
            DFS()

        if ([M, C - 1, boat] not in open_list) & ([M, C - 1, boat] not in close_list):
            open_list.append([M, C - 1, boat])
            list_path[hund(M, C - 1, boat)] = fa_node
            DFS()

    else:
        boat = 1  # 右岸,人應該增加
        if ([M + 2, C, boat] not in open_list) & ([M + 2, C, boat] not in close_list):
            open_list.append([M + 2, C, boat])
            list_path[hund(M + 2, C, boat)] = fa_node
            DFS()

        if ([M, C + 2, boat] not in open_list) & ([M, C + 2, boat] not in close_list):
            open_list.append([M, C + 2, boat])
            list_path[hund(M, C + 2, boat)] = fa_node
            DFS()

        if ([M + 1, C, boat] not in open_list) & ([M + 1, C, boat] not in close_list):
            open_list.append([M + 1, C, boat])
            list_path[hund(M + 1, C, boat)] = fa_node
            DFS()

        if ([M + 1, C + 1, boat] not in open_list) & ([M + 1, C + 1, boat] not in close_list):
            open_list.append([M + 1, C + 1, boat])
            list_path[hund(M + 1, C + 1, boat)] = fa_node
            DFS()

        if ([M, C + 1, boat] not in open_list) & ([M, C + 1, boat] not in close_list):
            open_list.append([M, C + 1, boat])
            list_path[hund(M, C + 1, boat)] = fa_node
            DFS()




if __name__ == "__main__":
    list_path = []
    init_list(list_path)
    str_first = str(3) + '-' + str(3) + '-' + str(1)
    #print("初始狀態:" + str_first)
    open_list.append([3, 3, 1])
    DFS()
    #print(time)

BFS

#!/usr/bin/python
# -*- coding: utf-8 -*-

'''
狀態表示方式(M,C,B)
M:修道士(Missionaries)在左岸的數量
C:野人(Cannibals)在左岸的數量
B:船的狀態,左岸還是右岸
初始狀態(3,3,1)
終結狀態(0,0,0)
'''


def init_list(list_path):
    for i in range(0, 1000):
        list_path.append(0)


def hund(M, C, boat):
    return (M+1) * 100 + (C+1) * 10 + (boat+1)

def re_back(n):
    a = n // 100
    b = n // 10 % 10
    c = n % 10
    return[a - 1, b - 1, c - 1]



def print_list(list_path, n):
    if list_path[n] == 442:
        print(re_back(442))
        print(re_back(n))
        return
    else:
        print_list(list_path, list_path[n])
        print(re_back(n))

if __name__ == "__main__":
    open_list = []
    close_list = []
    list_path = []
    time = 0  # 步驟計數
    str_end = "0-0-0"
    str_first = str(3) + '-' + str(3) + '-' + str(1)
    #print("初始狀態:" + str_first)
    open_list.append([3,3,1])

    time += 1
    init_list(list_path)
    while len(open_list) != 0:
        #print(time)
        time += 1
        list_temp = open_list.pop(0)
        M = list_temp[0]
        C = list_temp[1]
        boat = list_temp[2]
        str_temp = str(M) + '-' + str(C) + '-' + str(boat)
        close_list.append(list_temp)
        if str_temp == str_end:
            print("OK")
            print_list(list_path, 111)
            break

        if (M < 0) | (C < 0) | (M > 3) | (C > 3):
            continue
        if (M & (M < C)) | ((3 - M) & ((3 - M) < (3 - C))):
            continue

        #print(list_temp)

        fa_node = hund(M, C, boat)
        if boat == 1:  # 左岸,人應該減少
            boat = 0
            if ([M-2,C,boat] not in open_list) & ([M-2,C,boat] not in close_list):
                open_list.append([M-2,C,boat])
                list_path[hund(M-2, C, boat)] = fa_node

            if ([M,C-2,boat] not in open_list) & ([M,C-2,boat] not in close_list):
                open_list.append([M,C-2,boat])
                list_path[hund(M, C - 2, boat)] = fa_node

            if ([M-1,C,boat] not in open_list) & ([M-1,C,boat] not in close_list):
                open_list.append([M-1,C,boat])
                list_path[hund(M - 1, C, boat)] = fa_node

            if ([M-1,C-1,boat] not in open_list) & ([M-1,C-1,boat] not in close_list):
                open_list.append([M-1,C-1,boat])
                list_path[hund(M - 1, C - 1, boat)] = fa_node

            if ([M,C-1,boat] not in open_list) & ([M,C-1,boat] not in close_list):
                open_list.append([M,C-1,boat])
                list_path[hund(M, C - 1, boat)] = fa_node

        else:
            boat = 1   #右岸,人應該增加
            if ([M+2,C,boat] not in open_list) & ([M+2,C,boat] not in close_list):
                open_list.append([M+2,C,boat])
                list_path[hund(M + 2, C, boat)] = fa_node

            if ([M,C+2,boat] not in open_list) & ([M,C+2,boat] not in close_list):
                open_list.append([M,C+2,boat])
                list_path[hund(M, C + 2, boat)] = fa_node

            if ([M+1,C,boat] not in open_list) & ([M+1,C,boat] not in close_list):
                open_list.append([M+1,C,boat])
                list_path[hund(M + 1, C, boat)] = fa_node

            if ([M+1,C+1,boat] not in open_list) & ([M+1,C+1,boat] not in close_list):
                open_list.append([M+1,C+1,boat])
                list_path[hund(M + 1, C + 1, boat)] = fa_node

            if ([M,C+1,boat] not in open_list) & ([M,C+1,boat] not in close_list):
                open_list.append([M,C+1,boat])
                list_path[hund(M, C + 1, boat)] = fa_node

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章