8-难题-使用bfs解决但无法循环

时间:2019-02-21 04:51:48

标签: python-3.x breadth-first-search

我的代码甚至都没有通过bfs函数。
我做错了吗?
我正在从文件(val.txt
读取数据后,我一次处理每一行。
我不确定是什么问题。
我正试图从文件中的给定数据中获取[0,1,2,3,4,5,6,7,8]
我什么都不能打印。 请帮忙-谢谢

from numpy import*
import numpy as np
import queue
import gc
import time
#setting up the final move
GOAL = [0,1,2,3,4,5,6,7]
#to track the parent
parent = 0;

#to swap the state
def swap(state, index1, index2):
    temp = state[index1]
    state[index1] = state[index2]
    state[index2] = temp
    return state
#getting moves and thier state (zero)
def moveState(state, move):
    #copying the state
    nextS = list(state)
    #getting the index of 0
    index = state.index(0)
    #if move is right
    if move == 'R':
        if index % 3 ==2:
            return None
        nextS = swap(nextS, index, index +1)
    #if move is Left
    elif move == 'L':
        if index % 3 == 0:
            return None
        nextS = swap(nextS, index, index - 1)
    #if move is up
    elif move == 'U':
        if index < 3:
            return None
        nextS = swap(nextS, index, index - 3)
    #if move is down
        elif move == 'D':
            if index > 5:
                return None
            nextS = swap(nextS, index, index + 3)
        #other moves 
        else:
            return None
        return nextS
    #to track the move 
    def move(state):
        #to store the move
        nextS = []
        global parent
        #if the request is R 
        tryS = moveState(state, 'R')
        if tryS is not None:
            nextS.append(tryS)
         #if the request is L 
        tryS = moveState(state, 'L')
        if tryS is not None:
            nextS.append(tryS)

         #if the request is U
        tryS = moveState(state, 'U')
        if tryS is not None:
            nextS.append(tryS)
         #if the request is D
        tryS = moveState(state, 'D')
    if tryS is not None:
        nextS.append(tryS)
    return nextS
    #using bfs search to find the path on bothside
    def bfs(list_input):
        #using list 
        puzzle = list()
        #puzzlecp is a set
        puzzle_cp = set()
        #adding to the data 
        puzzle_cp.add(tuple(list_input))

        #appending the list
        puzzle.append(list_input)
        #to hold the visited data
        explored = set()
        t = 0
        #using while loop to go throuh the flie
        while puzzle:
            #popping data off
            c_state = puzzle.pop()
            #removing the data
            puzzle_cp.remove(tuple(c_state))
            #adding to explored
            explored.add(tuple(c_state)

)
        #if goal reached then memory free
        if c_state == GOAL:
            print("step", len(explored))
            del puzzle
            del explored 
            del puzzle_cp
            return c_state
        #using for loop to chekc the visited item
        for move_state in reversed(move(c_state)):
            if tuple(move_state) not in explored and tuple(move_state) not in puzzle_cp:
                puzzle.append(move_state)
                puzzle_cp.add(tuple(move_state))

        return None
#read file 
f = open('val.txt', 'r')
out = f.readlines()
i = 0
#reading line by line 
#then proccessing the each line
for line in out:
    i = i + 1
    print("Count", i)
    x = line.split(',')      #seperating data by comma
    v = [int(xx) for xx in x]
    state = bfs(v)
print("MOve", state)
while state is None:
    gc.collect()
    print('False')
    break

gc.collect()

从val.txt中读取flie
其中contian
4,3,1,0,2,6,7,5,8
4,3,6,0,2,1,7,5,8
4,5,1,0,2,6,7,3,8

0 个答案:

没有答案