चौड़ाई पहली खोज अनंत लूप

0

सवाल

मैं पैदा कर रहा हूँ 8 पहेली ऐ खेल में bfs और यह हमेशा समाप्त होता है एक अनंत लूप में , मैं का उपयोग कर रहा हूँ एक कतार में स्टोर करने के लिए पता लगाया (नहीं अभी तक का दौरा किया नोड्स) , और एक सूची स्टोर करने के लिए पता लगाया और का दौरा किया नोड्स में आने से बचने के लिए एक ही राज्य के लिए कई बार , यह भी का उपयोग कर im getNextStates() समारोह के सभी पाने के लिए संभव अगले राज्यों की स्थिति के आधार पर "0" , स्विच() बनाने के लिए जिम्मेदार है अगले राज्य , अपने कोड :

goalState=[0, 1, 2, 3, 4, 5, 6, 7, 8]
def switch(list,index1,index2):
    newList=[]
    for i in range(len(list)):
        newList.append(list[i])

    temp=newList[index1]
    newList[index1]=newList[index2]
    newList[index2]=temp
    return newList

def getNextStates(state):
    nextStates=[]
    length=len(state)
    emptyTile=0
    for i in range(length):
        if state[i]==0:
            emptyTile=i
            #    1
            # 0  3
    print('empty tile in position : ' , emptyTile)
    if emptyTile==0:
        nextStates.append(switch(state,0,1))
        nextStates.append(switch(state, 0, 3))
    elif emptyTile==1:
        nextStates.append(switch(state, 1, 0))
        nextStates.append(switch(state, 1, 4))
        nextStates.append(switch(state, 1, 2))
    elif emptyTile==2:
        nextStates.append(switch(state, 2, 1))
        nextStates.append(switch(state, 2, 5))
    elif emptyTile==3:
        nextStates.append(switch(state, 3, 0))
        nextStates.append(switch(state, 3, 4))
        nextStates.append(switch(state, 3, 6))
    elif emptyTile==4:
        nextStates.append(switch(state, 4, 3))
        nextStates.append(switch(state, 4, 1))
        nextStates.append(switch(state, 4, 5))
        nextStates.append(switch(state, 4, 7))
    elif emptyTile==5:
        nextStates.append(switch(state, 5, 2))
        nextStates.append(switch(state, 5, 4))
        nextStates.append(switch(state, 5, 8))
    elif emptyTile==6:
        nextStates.append(switch(state, 6, 3))
        nextStates.append(switch(state, 6, 7))
    elif emptyTile==7:
        nextStates.append(switch(state, 7, 6))
        nextStates.append(switch(state, 7, 4))
        nextStates.append(switch(state, 7, 8))
    else:
        nextStates.append(switch(state, 8, 7))
        nextStates.append(switch(state, 8, 5))

    return nextStates


def breadthFirst(initialState,goal):
    global exploredCount , visitedCount
    exploredCount = 1
    visitedCount = 0
    frontier = []
    frontier.append(initialState)
    explored=[]
    print("Staring Dequing....")
    while len(frontier) > 0:
        print(len(frontier))
        state=frontier.pop(0)
        print("dequed : " , state)
        explored.append(state)
        print("appended in explored and visitedCount incremented")
        visitedCount += 1
        if state==goal:
            print("State Is Accomplished")
            return state
        nextStates=getNextStates(state)
        print("possible Next States : " , nextStates)
        for i in range(len(nextStates)):
            print('Checking Child states , current : ' , nextStates[i])
            if  not nextStates[i] in explored:
                if not nextStates[i] in frontier:
                    print("not in visited or explored , enqueue")
                    frontier.append(nextStates[i])
                    exploredCount += 1
 
    return initialState
1

सबसे अच्छा जवाब

1

मुद्दा यह था कि वहाँ प्रारंभिक अमेरिका जो कर रहे हैं न सुलझा हुआ , अगर इस मामले में एल्गोरिथ्म रखना होगा की खोज में एक अनंत लूप. समाधान था गिनती करने के लिए संख्या का व्युत्क्रम (खाली टाइल) शामिल नहीं है और अगर इसकी अजीब है, तो इसकी न सुलझा हुआ , तो इसकी भी तो इसकी व्याख्या करने योग्य है , इस संसाधन में मदद मिली मुझे समझ में : https://www.cs.princeton.edu/courses/archive/fall12/cos226/assignments/8puzzle.html

2021-11-24 13:01:43

अन्य भाषाओं में

यह पृष्ठ अन्य भाषाओं में है

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................

इस श्रेणी में लोकप्रिय

लोकप्रिय सवाल इस श्रेणी में