二维矩阵遍历 - 最优路径记忆

时间:2013-05-31 18:29:49

标签: c matrix stack traversal

我有一个函数,用于在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);
}

1 个答案:

答案 0 :(得分:0)

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

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

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

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

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()。每次找到“潜在”解决方案时,答案堆都会更新。我也没有时间进行参数传递。临时堆栈“必须”是本地的。使用答案堆栈。