Esempio n. 1
0
def tilegame_heuristic(state):
    """
  Produces a real number for the given tile game state representing
  an estimate of the cost to get to the goal state.
  """
    state_list = TileGame.tuple_to_list(state)
    count = 0
    d = len(state_list)
    for i in range(d):
        for j in range(len(state_list[i])):
            row = (state_list[i][j] - 1) / d
            col = (state_list[i][j] - 1) % d
            count += abs(row - i) + abs(col - j)
    return count / 2
Esempio n. 2
0
def tilegame_heuristic(state):
    """
    Produces a number for the given tile game state representing
    an estimate of the cost to get to the goal state.

    Input:
        state - the tilegame state to evaluate. Consult handout for how the tilegame state is represented

    Output: a number (int, float, etc.)

    """
    list = TileGame.tuple_to_list(state)
    dim = len(list)
    step = 0
    for i in range(dim):
        for j in range(dim):
            correct_i = int((list[i][j] - 1) / dim)
            correct_j = (list[i][j] - 1) % dim
            step += (abs(i - correct_i) + abs(j - correct_j))
    return int(step / 2)