コード例 #1
0
ファイル: problem_a.py プロジェクト: NilsBarlaug/TDT4136
def main(args):

    # Read the board from standard in
    board = sys.stdin.read().splitlines()
    for i in range(len(board)):
        board[i] = list(board[i])

    # Define the heuristics
    def manhatten_heuristic(state, goal_state):
        dr = abs(state[0] - goal_state[0])
        dc = abs(state[1] - goal_state[1])
        return dr + dc

    def no_heuristic(state, goal_state):
        return 0

    # A*
    astar_problem = GridProblem(board, manhatten_heuristic)
    astar_result = uniform_cost.search(astar_problem)
    make_graphics('astar_', args.image_destination, args.postfix,
                  board, astar_result)

    # Dijsktra
    dijkstra_problem = GridProblem(board, no_heuristic)
    dijkstra_result = uniform_cost.search(dijkstra_problem)
    make_graphics('dijkstra_', args.image_destination, args.postfix,
                  board, dijkstra_result)

    # BFS
    bfs_problem = GridProblem(board, no_heuristic)
    bfs_result = breadth_first.search(bfs_problem)
    make_graphics('bfs_', args.image_destination, args.postfix,
                  board, bfs_result)
コード例 #2
0
def solve(game_class):
    layers = breadth_first.search([game_class.parse_pretty(sys.stdin.read())], game_class.wins)

    data = []
    for layer in layers[1:]:
        layer_data = []
        for origins, destination in layer:
            for origin in origins:
                layer_data.append([origin.format_pretty(), destination.format_pretty()])
        data.append(sorted(layer_data))
    json.dump(data, sys.stdout)