1

我有一个函数用于在 2D 矩阵(整数、值和总和的结构)中找到最佳路径,但它不记住最佳值,它只返回遍历的最低成本,一旦它下降到底部的矩阵。我们应该使用堆栈以某种方式记住这些最佳值,但不幸的是我不知道如何做到这一点。它是一种递归算法,因此很难分析。值用伪随机数(1 到 10)填充,并且 sum 被初始化为INT_MAX. 这似乎有点类似于三叉树。

堆栈函数原型是:

stack_t stack_new(); // already done in main
void stack_delete(stack_t stack); // -||-
void stack_push(stack_t stack, stack_element_t elem);
stack_element_t stack_pop(stack_t stack);
stack_element_t stack_top(stack_t stack);
int stack_is_empty(stack_t stack);

/* recursively seeks the optimal path in a 2D matrix */

void traverse(struct path **matrix, unsigned row, unsigned col, int path_cost, int *min_cost, int *cnt, stack_t stack, FILE *f)
{
    char buffer[16];
    path_cost += matrix[row][col].value;
    matrix[row][col].sum = path_cost;
    (*cnt)++; // counting calls
    fprintf(f, "call_counter: %d, min_cost: %s, path_cost: %d, value: %d, sum: %d\n", *cnt, *min_cost == INT_MAX ? "Inf" : itoa(*min_cost, buffer, 10), path_cost, matrix[row][col].value, matrix[row][col].sum); // logging
    if(matrix[row][col].sum > *min_cost) // if we've been here before and it wasn't as costly, return
    {
        return;
    }
    if(row == MATRIX_ROW - 1) // if we're at the bottom of the matrix
    {
        if(path_cost < *min_cost)
        {
            *min_cost = path_cost;
        }
        return;
    }
    if (col < MATRIX_COL - 1) // go down and right
        traverse(matrix, row + 1, col + 1, path_cost, min_cost, cnt, stack, f);

    traverse(matrix, row + 1, col, path_cost, min_cost, cnt, stack, f); // go down

    if (col > 0) // go down and left
        traverse(matrix, row + 1, col - 1, path_cost, min_cost, cnt, stack, f);
}
4

1 回答 1

0

您可以使用两个堆栈查找路径。一个是临时堆栈,另一个是ANSWER堆栈。

基本的想法是,当你移动时,你将你进入的方向推入堆栈。同样在那个特定移动的“函数调用()”之后,你弹出()那个方向。然后,到达底部时的步骤,如果以下条件为真:

if(path_cost < *min_cost)
{
  *min_cost = path_cost;
}

您清空 Answer stack ,并将 Temporary stack 的内容复制到其中。

stack answer ;
stack temporary;

/* recursively seeks the optimal path in a 2D matrix */



void traverse(struct path **matrix, unsigned row, unsigned col, int path_cost, int *min_cost, int *cnt, stack_t stack, FILE *f)
{
    char buffer[16];
    path_cost += matrix[row][col].value;
    matrix[row][col].sum = path_cost;
    (*cnt)++; // counting calls
    fprintf(f, "call_counter: %d, min_cost: %s, path_cost: %d, value: %d, sum: %d\n", *cnt, *min_cost == INT_MAX ? "Inf" : itoa(*min_cost, buffer, 10), path_cost, matrix[row][col].value, matrix[row][col].sum); // logging
    if(matrix[row][col].sum > *min_cost) // if we've been here before and it wasn't as costly, return
    {
        return;
    }
    if(row == MATRIX_ROW - 1) // if we're at the bottom of the matrix
    {
        if(path_cost < *min_cost)
        {
            *min_cost = path_cost;
            
            /*
            
            just empyty teh Answer stack here;
            then copy the Temp stack into answer stack
            
            */
            
        }
        return;
    }
    
    // 1 for (row +1 ,col +1 )
    // 2 for (row +1 ,col )
    // 3 for (row +1 ,col -1 )
    
    if (col < MATRIX_COL - 1) // go down and right
    {
        temporary.push(1);
        traverse(matrix, row + 1, col + 1, path_cost, min_cost, cnt, stack, f);
        temporary.pop(1);
    }
    
    temporary.push(2);  
    traverse(matrix, row + 1, col, path_cost, min_cost, cnt, stack, f); // go down
    temporary.pop(2);
    
    if (col > 0) // go down and left
    {
        temporary.push(3);      
       traverse(matrix, row + 1, col - 1, path_cost, min_cost, cnt, stack, f);
       temporary.pop(3);
    }
}

编辑:

pop() 和 push() 从临时堆栈。每次找到“潜在”解决方案时都会更新答案堆栈。我也没有时间进行参数传递。临时堆栈“必须”是本地的。根据需要使用答案堆栈。

于 2013-05-31T18:49:16.357 回答