搜索算法的形式化描述:状态、动作、状态转移、路径、测试目标
状态:从原问题转化出的问题描述;
动作:从当前时刻所处状态转移到下一时刻所处状态所进行操作,一般而言这些操作都是离散的;
状态转移:对某一时刻对应状态进行某一种操作后,所能够到达状态;
路径:一个状态序列,该状态序列被一系列操作所连接;
测试目标:评估当前状态是否为所求解的目标状态;
问题:寻找从Arad到Bucharest的一条最短路径
辅助信息:任意一个城市与Bucharest的直线距离
代码实现
import sys
from collections import deque
class Romania:
"""The abstract class for a formal problem. You should subclass
this and implement the methods actions and result, and possibly
__init__, goal_test, and path_cost. Then you will create instances
of your subclass and solve them with the various search functions."""
neighbor_map = {'Arad': ['Zerind', 'Sibiu', 'Timisoara'], 'Bucharest': ['Urziceni', 'Pitesti', 'Giurgiu', 'Fagaras'],
'Craiova': ['Drobeta', 'Rimnicu', 'Pitesti'], 'Drobeta': ['Mehadia'], 'Eforie': ['Hirsova'],
'Fagaras': ['Sibiu'], 'Hirsova': ['Urziceni'],
'Iasi': ['Vaslui', 'Neamt'],'Lugoj': ['Timisoara', 'Mehadia'],
'Oradea': ['Zerind', 'Sibiu'], 'Pitesti': ['Rimnicu'],
'Rimnicu': ['Sibiu'], 'Urziceni': ['Vaslui']
}
straight_l