python拼图识别_Python中的拼图

我正在尝试使用Python中的广度优先搜索构建一个N-Puzzle问题的解决方案。在

我的解决方案擅长于在除零之外的所有数字都正确的情况下找到答案。e、 ginitial_state = [1,2,3,4,0,5,6,7,8]

或者

^{pr2}$

但是失败了initial_state = [1,2,5,3,4,0,6,7,8]

请在下面找到我的实现。如果有人能指出我逻辑上的缺陷,我将不胜感激。在

提前谢谢!在def right(state):

items = list(state)

i = items.index(0)

n = int(math.sqrt(len(items)))

if (i+1) % n != 0:

del items[i]

items.insert(i+1, 0)

return tuple(items)

else:

pass

def left(state):

items = list(state)

i = items.index(0)

n = int(math.sqrt(len(items)))

if i % n != 0:

del items[i]

items.insert(i-1, 0)

return tuple(items)

else:

pass

def up(state):

items = list(state)

i = items.index(0)

n = int(math.sqrt(len(items)))

if n**2 < i <= (n**2 - n):

del items[i]

items.insert(i+n, 0)

return tuple(items)

else:

pass

def down(state):

items = list(state)

i = items.index(0)

n = int(math.sqrt(len(items)))

if i > n:

del items[i]

items.insert(i-n, 0)

return tuple(items)

else:

pass

class Problem(object):

def __init__(self, initial, goal=None):

self.initial = initial

self.goal = goal

self.n = len(initial)

self.size = int(math.sqrt(self.n))

self.blank = self.initial.index(0)

self.top_row = [i for i in range(self.n) if i < self.size]

self.bottom_row = [i for i in range(self.n) if self.n - (self.size) <= i < self.n]

self.left_column = [i for i in range(self.n) if i % self.size == 0]

self.right_column = [i for i in range(self.n) if (i + 1) % self.size == 0]

def actions(self):

result_list = ["UP","DOWN","LEFT","RIGHT"]

return result_list

def result(self, state, action):

if action == "RIGHT":

return right(state)

if action == "LEFT":

return left(state)

if action == "UP":

return up(state)

if action == "DOWN":

return down(state)

def goal_test(self, state):

return state == self.goal

def path_cost(self, c):

return c + 1

class Node:

def __init__(self, state, parent=None, action=None, path_cost=0):

self.state = state

self.parent = parent

self.action = action

self.path_cost = path_cost

self.depth = 0

if parent:

self.depth = parent.depth + 1

def __repr__(self):

return "" % (self.state,)

def __lt__(self, node):

return self.state < node.state

def expand(self, problem):

return [self.child_node(problem, action)

for action in problem.actions() if self.child_node(problem,action) is not None]

def child_node(self, problem, action):

next = problem.result(self.state, action)

if next:

return Node(next, self, action,

problem.path_cost(self.path_cost))

else:

pass

def solution(self):

return [node.action for node in self.path()[1:]]

def path(self):

node, path_back = self, []

while node:

path_back.append(node)

node = node.parent

return list(reversed(path_back))

def __eq__(self, other):

return isinstance(other, Node) and self.state == other.state

def __hash__(self):

return hash(self.state)

def bfs(problem):

node = Node(problem.initial)

frontier = deque([node])

explored = set()

while frontier:

node = frontier.pop()

explored.add(node.state)

if problem.goal_test(node.state):

return node

for child in node.expand(problem):

if child.state not in explored and child not in frontier:

frontier.append(child)

return [child for child in explored]

p = Problem((1,2,5,3,4,0,6,7,8), (0,1,2,3,4,5,6,7,8))

bfs(p)

#returns

"""[(1, 2, 5, 3, 4, 0, 6, 7, 8),

(1, 2, 0, 5, 3, 4, 6, 7, 8),

(0, 1, 2, 5, 3, 4, 6, 7, 8),

(1, 2, 5, 3, 0, 4, 6, 7, 8),

(1, 2, 5, 0, 3, 4, 6, 7, 8),

(1, 0, 2, 5, 3, 4, 6, 7, 8)]"""

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值